Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update number renamer to avoid collisions in parent scopes #3949

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions internal/renamer/renamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,22 @@ type nameUse uint8
const (
nameUnused nameUse = iota
nameUsed
nameUsedInSameScope
)

func (s *numberScope) findNameUse(name string) nameUse {
original := s
func (s *numberScope) findNameUse(name string) (nameUse, uint32) {
use := nameUnused
tries := uint32(1)
// Try to find an unused name by taking the maximum count across all scopes
for {
if _, ok := s.nameCounts[name]; ok {
if s == original {
return nameUsedInSameScope
if t, ok := s.nameCounts[name]; ok {
use = nameUsed
if t > tries {
tries = t
}
return nameUsed
}
s = s.parent
if s == nil {
return nameUnused
return use, tries
}
}
}
Expand All @@ -589,33 +590,19 @@ func (s *numberScope) findUnusedName(name string, ns ast.SlotNamespace) string {
}
}

if use := s.findNameUse(name); use != nameUnused {
// If the name is already in use, generate a new name by appending a number
tries := uint32(1)
if use == nameUsedInSameScope {
// To avoid O(n^2) behavior, the number must start off being the number
// that we used last time there was a collision with this name. Otherwise
// if there are many collisions with the same name, each name collision
// would have to increment the counter past all previous name collisions
// which is a O(n^2) time algorithm. Only do this if this symbol comes
// from the same scope as the previous one since sibling scopes can reuse
// the same name without problems.
tries = s.nameCounts[name]
}
use, tries := s.findNameUse(name)
if use != nameUnused {
prefix := name

// Keep incrementing the number until the name is unused
for {
tries++
name = prefix + strconv.Itoa(int(tries))

// Make sure this new name is unused
if s.findNameUse(name) == nameUnused {
if newUse, _ := s.findNameUse(name); newUse == nameUnused {
// Store the count so we can start here next time instead of starting
// from 1. This means we avoid O(n^2) behavior.
if use == nameUsedInSameScope {
s.nameCounts[prefix] = tries
}
s.nameCounts[prefix] = tries
break
}
}
Expand Down