-
Notifications
You must be signed in to change notification settings - Fork 1k
compiler: include the scope in the type code name #5183
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: dev
Are you sure you want to change the base?
Conversation
cec236b to
4fd15a3
Compare
|
Maybe add this to the compiler frontend tests ( |
4fd15a3 to
91c1d8a
Compare
|
I don't think I can put it in the golden |
|
Oh, we should not be using the pointer address for this. |
|
There doesn't appear to be anything else aside from pointer addresses to allow us to differentiate scopes. Even the |
|
Note also this the pointer address of the scope in the compiler, not the pointer at runtime on the device. |
|
If that is the case then we could loop through the SSA and assign numbers to them? Non-deterministic compilation is not acceptable. |
|
Ok. I'll see what I can come up with. |
|
PTAL |
91c1d8a to
b6aa203
Compare
|
(Oops, hadn't actually pushed my changes.... 🤦 ) |
f6cd265 to
f087248
Compare
f087248 to
7b86f95
Compare
compiler/interface.go
Outdated
| for scope != pkg { | ||
| parent := scope.Parent() | ||
| for i := range parent.NumChildren() { | ||
| if parent.Child(i) == scope { |
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.
This can result in O(n^2) overhead. Maybe build a map with everything in the package once and then look up from it?
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.
This code only runs for types declared in a local scope. I'll add a scope -> name cache for this function but walking the entire list of scopes looking for named types before we need them seems like overkill.
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.
You still have not fixed this issue. You are still iterating over every child of every parent for every child. This index is what you should be caching.
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.
Do you want the id cached for only the matching child (so I don't need to iterate next time), or for every child? (I'm concerned the "every child" case will just bloat the cache with entries that will never be queried. This is already only walking up the scopes from the scope the type is defined in up to the parent scope, so we're only looking at the children of parent nodes on the path to the root.)
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.
Commit e56e3e5 caches the index of each scope that we encounter on the path to the root. We might have to double scan a particular parent if there are multiple scopes with types defined in them. (I'm going to run this over the test corpus and see if I can quantify the loops -- the numbers we're dealing with are pretty small..)
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.
or for every child?
Yes. The cache does not actually solve anything unless you do this.
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.
I added some logging to getScopeID and this is the output for running the compiler over the entire test corpus (180 packages, ~1.5 million lines of Go code):
~/go/src/github.com/dgryski/tinygo-test-corpus $ rg === corpus.out |sed 's/.*===//'
scopeIdx: id=46:10: loops=58
scopeIdx: id=46:10: loops=0
scopeIdx: id=46:10: loops=0
scopeIdx: id=25:4: loops=31
scopeIdx: id=25:4: loops=0
scopeIdx: id=25:4: loops=0
scopeIdx: id=25:4: loops=0
scopeIdx: id=25:4: loops=0
scopeIdx: id=25:4: loops=0
scopeIdx: id=4:1: loops=7
scopeIdx: id=4:1: loops=0
scopeIdx: id=4:1: loops=0
scopeIdx: id=4:1: loops=0
scopeIdx: id=0:3: loops=5
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=5
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=0:3: loops=0
scopeIdx: id=8:0: loops=10
scopeIdx: id=8:0: loops=0
scopeIdx: id=8:0: loops=0
scopeIdx: id=10:0: loops=11
scopeIdx: id=10:0: loops=0
scopeIdx: id=10:0: loops=0
scopeIdx: id=0:9:0:5:1:0: loops=21
scopeIdx: id=0:9:0:5:1:0: loops=0
scopeIdx: id=0:9:0:5:1:0: loops=0
Even only adding in the matching child, the number of iterations is very small, and this code just isn't called that frequently. Adding in all the children will just increase the memory usage for no benefit, since none of them are likely to have named types in them that we're going to query.
|
This is better |
niaow
left a comment
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.
And again, this needs to be in the compiler testdata.
compiler/interface.go
Outdated
| scopeid map[*types.Scope]string | ||
| }{ | ||
| sync.Mutex{}, | ||
| make(map[*types.Scope]string), |
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.
Move this into the compiler context to get rid of the lock and the memory leak.
compiler/interface.go
Outdated
| for scope != pkg { | ||
| parent := scope.Parent() | ||
| for i := range parent.NumChildren() { | ||
| if parent.Child(i) == scope { |
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.
You still have not fixed this issue. You are still iterating over every child of every parent for every child. This index is what you should be caching.
|
Actually I just thought of a much simpler solution to this that I can try in a bit |
Fixes #5180