-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Fix O(n²) in VarNameCleaner::findCleanName using per-base-name counter #16563
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
base: develop
Are you sure you want to change the base?
Changes from all commits
57675ba
6a50a04
ac91639
e7595e3
eb5f6c0
9626a26
c4eb333
c2d84fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,13 +55,16 @@ void VarNameCleaner::operator()(FunctionDefinition& _funDef) | |
| m_usedNames = m_namesToKeep; | ||
| std::map<YulName, YulName> globalTranslatedNames; | ||
| swap(globalTranslatedNames, m_translatedNames); | ||
| std::map<YulName, size_t> globalNextSuffix; | ||
| swap(globalNextSuffix, m_nextSuffix); | ||
|
|
||
| renameVariables(_funDef.parameters); | ||
| renameVariables(_funDef.returnVariables); | ||
| ASTModifier::operator()(_funDef); | ||
|
|
||
| swap(globalUsedNames, m_usedNames); | ||
| swap(globalTranslatedNames, m_translatedNames); | ||
| swap(globalNextSuffix, m_nextSuffix); | ||
|
|
||
| m_insideFunction = false; | ||
| } | ||
|
|
@@ -93,18 +96,25 @@ void VarNameCleaner::operator()(Identifier& _identifier) | |
| _identifier.name = name->second; | ||
| } | ||
|
|
||
| YulName VarNameCleaner::findCleanName(YulName const& _name) const | ||
| YulName VarNameCleaner::findCleanName(YulName const& _name) | ||
| { | ||
| auto newName = stripSuffix(_name); | ||
| if (!isUsedName(newName)) | ||
| return newName; | ||
|
|
||
| // create new name with suffix (by finding a free identifier) | ||
| for (size_t i = 1; i < std::numeric_limits<decltype(i)>::max(); ++i) | ||
| // Use a per-base-name counter to avoid O(n²) probing when many | ||
| // variables share the same stripped base name. | ||
| size_t& nextSuffix = m_nextSuffix[newName]; | ||
| if (nextSuffix == 0) | ||
| nextSuffix = 1; | ||
| for (; nextSuffix < std::numeric_limits<size_t>::max(); ++nextSuffix) | ||
| { | ||
| YulName newNameSuffixed = YulName{newName.str() + "_" + std::to_string(i)}; | ||
| YulName newNameSuffixed = YulName{newName.str() + "_" + std::to_string(nextSuffix)}; | ||
| if (!isUsedName(newNameSuffixed)) | ||
| { | ||
| ++nextSuffix; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if i interpret everything correctly, this is supposed to increment the suffix in the map, right? but I don't think it does, it's retrieved by value so the map value isnt updated, ever.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I'm sorry. It was written correctly and then broken. in 4b13837 Fixing now.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha some idiot probably suggested removing the reference. Wait a minute... |
||
| return newNameSuffixed; | ||
| } | ||
| } | ||
| yulAssert(false, "Exhausted by attempting to find an available suffix."); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| let x__1 := 1 | ||
| let x__2 := 2 | ||
| let x___3 := 3 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| let x_5 := 1 | ||
| let y_3 := 2 | ||
| let x_10 := 3 | ||
| let y_7 := 4 | ||
| let z_1 := 5 | ||
| let x_20 := 6 | ||
| let y_15 := 7 | ||
| let z_8 := 8 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let y := 2 | ||
| // let x_1 := 3 | ||
| // let y_1 := 4 | ||
| // let z := 5 | ||
| // let x_2 := 6 | ||
| // let y_2 := 7 | ||
| // let z_1 := 8 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| let x_10 := 1 | ||
| let x_20 := 2 | ||
| let x_30 := 3 | ||
| let x_40 := 4 | ||
| let x_50 := 5 | ||
| let x_60 := 6 | ||
| let x_70 := 7 | ||
| let x_80 := 8 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // let x_3 := 4 | ||
| // let x_4 := 5 | ||
| // let x_5 := 6 | ||
| // let x_6 := 7 | ||
| // let x_7 := 8 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| let x_1 := 1 | ||
| function f() | ||
| { | ||
| let x_10 := 1 | ||
| let x_20 := 2 | ||
| let x_30 := 3 | ||
| let x_40 := 4 | ||
| let x_50 := 5 | ||
| } | ||
| let x_2 := 2 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // } | ||
| // function f() | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // let x_3 := 4 | ||
| // let x_4 := 5 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| let x_1_2 := 1 | ||
| let x_3_4_5 := 2 | ||
| let x_6 := 3 | ||
| let x_1_2_3_4 := 4 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // let x_3 := 4 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| let x_5 := 42 | ||
| let y_3 := x_5 | ||
| let z_1 := add(x_5, y_3) | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 42 | ||
| // let y := x | ||
| // let z := add(x, y) | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| let x := 1 | ||
| let x_1 := 2 | ||
| let x_5 := 3 | ||
| let x_10 := 4 | ||
| let x_20 := 5 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // let x_3 := 4 | ||
| // let x_4 := 5 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| // x_1 is a function name, so the suffix counter must skip it | ||
| // when cleaning x_100 and x_200 which both strip to "x" | ||
| function x_1() {} | ||
| let x := 1 | ||
| let x_100 := 2 | ||
| let x_200 := 3 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_2 := 2 | ||
| // let x_3 := 3 | ||
| // } | ||
| // function x_1() | ||
| // { } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| let x := 1 | ||
| let x_2 := 2 | ||
| let x_100 := 3 | ||
| let x_200 := 4 | ||
| let x_300 := 5 | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // let x_3 := 4 | ||
| // let x_4 := 5 | ||
| // } | ||
| // } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| function f() | ||
| { | ||
| let x_10 := 1 | ||
| let x_20 := 2 | ||
| let x_30 := 3 | ||
| } | ||
| function g() | ||
| { | ||
| let x_40 := 4 | ||
| let x_50 := 5 | ||
| let x_60 := 6 | ||
| } | ||
| } | ||
| // ---- | ||
| // step: varNameCleaner | ||
| // | ||
| // { | ||
| // { } | ||
| // function f() | ||
| // { | ||
| // let x := 1 | ||
| // let x_1 := 2 | ||
| // let x_2 := 3 | ||
| // } | ||
| // function g() | ||
| // { | ||
| // let x := 4 | ||
| // let x_1 := 5 | ||
| // let x_2 := 6 | ||
| // } | ||
| // } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the cache is now mutable, you can put back the const :)