Repository navigation
perf(semantic): compute a feature's redefinition closure once per model #1353
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changes/unreleased/implicit-parameter-position-from-memo.fixed.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - The features a feature redefines, directly or through them, are computed once per model, and a parameter's implicit redefinitions take its position from its owner's memoized parameter list instead of rebuilding it on every query; same redefinitions, fewer allocations per query — and per execution root frame, which asks for every feature it holds. |
28 changes: 28 additions & 0 deletions
28
internal/semantic/semantics/implicit_parameter_memo_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package semantics | ||
|
|
||
| import "testing" | ||
|
|
||
| // The redefinition closure of a feature is computed once per model, and a | ||
| // parameter's implicit redefinitions take its position from the owner's | ||
| // memoized parameter list instead of rebuilding it: repeated queries allocate | ||
| // nothing beyond what the owner's implied generals cost. | ||
| func TestRedefinedFeaturesAreComputedOncePerSymbol(t *testing.T) { | ||
| m, root := buildModel(t, `package P { | ||
| action def Base { in x; in y; out z; } | ||
| action def Proc :> Base { in a; in b; out c; } | ||
| }`) | ||
| p := sym(t, root, "P") | ||
| proc := nested(t, p.Scope, "Proc") | ||
| a := nested(t, p.Scope, "Proc", "a") | ||
| wantX := nested(t, p.Scope, "Base", "x") | ||
| if got := m.AllRedefinedFeatures(a); len(got) != 1 || got[0] != wantX { | ||
| t.Fatalf("AllRedefinedFeatures(Proc::a) = %v, want [Base::x]", got) | ||
| } | ||
| if got := testing.AllocsPerRun(20, func() { m.AllRedefinedFeatures(a) }); got != 0 { | ||
| t.Fatalf("a repeated AllRedefinedFeatures query allocates %.0f times, want 0", got) | ||
| } | ||
| generals := testing.AllocsPerRun(20, func() { m.ImplicitGenerals(proc) }) | ||
| if got := testing.AllocsPerRun(20, func() { m.ImplicitParameterRedefinitions(a) }); got > generals+1 { | ||
| t.Fatalf("a repeated ImplicitParameterRedefinitions query allocates %.0f times, want at most its result beyond the owner's implied generals (%.0f)", got, generals) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package semantics | ||
|
|
||
| import "testing" | ||
|
|
||
| // A parameter list read while its owner's list is still being computed cuts | ||
| // the reading query short, so a closure built on it is not memoized either. | ||
| func TestParameterListGuardCutsDependentQueriesShort(t *testing.T) { | ||
| m, root := buildModel(t, `package P { | ||
| action def Base { in x; out z; } | ||
| action def Proc :> Base { in a; out c; } | ||
| }`) | ||
| p := sym(t, root, "P") | ||
| proc := nested(t, p.Scope, "Proc") | ||
| a := nested(t, p.Scope, "Proc", "a") | ||
| wantX := nested(t, p.Scope, "Base", "x") | ||
| // As parametersOf stands mid-computation for the owner. | ||
| m.params[proc] = behaviorParameters{} | ||
| m.computingParams[proc] = m.resolver.Enter() | ||
| if got := m.AllRedefinedFeatures(a); len(got) != 0 { | ||
| t.Fatalf("AllRedefinedFeatures(Proc::a) under the guard = %v, want the guard's empty answer", got) | ||
| } | ||
| m.resolver.Leave() | ||
| delete(m.params, proc) | ||
| delete(m.computingParams, proc) | ||
| if got := m.AllRedefinedFeatures(a); len(got) != 1 || got[0] != wantX { | ||
| t.Fatalf("AllRedefinedFeatures(Proc::a) after the guard = %v, want [Base::x]", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
internal/semantic/semantics/redefinition_closure_guard_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package semantics | ||
|
|
||
| import "testing" | ||
|
|
||
| // A redefinition closure walked while a guard cuts a query short is answered | ||
| // but not memoized: once the guard lifts, the next query sees the target. | ||
| func TestRedefinitionClosureIsNotCachedWhileAGuardCutsItShort(t *testing.T) { | ||
| m, root := buildModel(t, `package P { | ||
| part def W { attribute x; } | ||
| part def A { part w : W; } | ||
| part def B :> A { attribute z :>> w.x; } | ||
| }`) | ||
| p := sym(t, root, "P") | ||
| wantX := nested(t, p.Scope, "W", "x") | ||
| bz := nested(t, p.Scope, "B", "z") | ||
| // As RedefinedFeatures stands mid-computation: seeded, with its lookup open. | ||
| m.redefined[bz] = nil | ||
| m.computingRedefined[bz] = m.resolver.Enter() | ||
| if got := m.AllRedefinedFeatures(bz); len(got) != 0 { | ||
| t.Fatalf("AllRedefinedFeatures(B::z) under the guard = %v, want the guard's empty answer", got) | ||
| } | ||
| m.resolver.Leave() | ||
| delete(m.redefined, bz) | ||
| delete(m.computingRedefined, bz) | ||
| if got := m.AllRedefinedFeatures(bz); len(got) != 1 || got[0] != wantX { | ||
| t.Fatalf("AllRedefinedFeatures(B::z) after the guard = %v, want [W::x]", got) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.