Improve D&D registry normalization efficiency
This commit is contained in:
@@ -241,20 +241,15 @@ func normalizeRecord(input dnd.Item, order shared.SourceRefOrder) (dnd.Item, boo
|
||||
|
||||
func comparisonNameGroups(records []normalizedRecord) [][]int {
|
||||
groups := make([][]int, 0, len(records))
|
||||
groupPositions := make(map[string]int, len(records))
|
||||
for index, record := range records {
|
||||
key := identity.ComparisonKey(record.item.Name)
|
||||
found := false
|
||||
for groupIndex, members := range groups {
|
||||
first := records[members[0]]
|
||||
if identity.ComparisonKey(first.item.Name) == key {
|
||||
groups[groupIndex] = append(groups[groupIndex], index)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
groups = append(groups, []int{index})
|
||||
if groupIndex, found := groupPositions[key]; found {
|
||||
groups[groupIndex] = append(groups[groupIndex], index)
|
||||
continue
|
||||
}
|
||||
groupPositions[key] = len(groups)
|
||||
groups = append(groups, []int{index})
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
@@ -85,6 +85,20 @@ func TestNormalizeConsolidatesEqualNamesAcrossEvidenceWithoutMutation(t *testing
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkComparisonNameGroupsManyDistinct(b *testing.B) {
|
||||
records := make([]normalizedRecord, 1_000)
|
||||
for index := range records {
|
||||
records[index] = normalizedRecord{item: dnd.Item{Name: "Item " + strconv.Itoa(index)}}
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for iteration := 0; iteration < b.N; iteration++ {
|
||||
if groups := comparisonNameGroups(records); len(groups) != len(records) {
|
||||
b.Fatalf("group count = %d, want %d", len(groups), len(records))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeAppliesSafeAliasProposal(t *testing.T) {
|
||||
doc := semanticDocument()
|
||||
input := dnd.ItemRegistry{Items: []dnd.Item{
|
||||
|
||||
Reference in New Issue
Block a user