-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Use LinkedList in scope locals #1329
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
Conversation
If it's doubly-linked doesn't this break tail sharing? (Ie. the clone requires a copy of the list?) This code is still the wrong structure, so I don't think we should be making this kind of small optimisation until we've fixed the big problems. |
To be more precise load_global should be able to skip over the list of scopes and just access the globals. The call to last should go away. |
I think I should have stated that one of the main goals of this change is to use
You are correct but as can be seen from the performance benchmarks this does not have a negative effect. We have a list of referenced counted object creating a copy of the list is very cheap.
Even without the fast access to last I think the simplification to the code is worth the change. |
It's nice to see the code reduction, but I'm not sure it's worth using the wrong data structure for. I'm guessing there's no standard library implementation of RcList?
The benchmarks don't use nested scopes at all. mandelbrot doesn't even have a local scope. That said, very deeply nesting scopes isn't common in python, so the list should usually be short. (The slow down would be on calling closures, I think.) |
I propose to merge this after #1333 gets merged. I like the usage of a standard linked list implementation, to reduce code. |
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.
Looks good to me!
Too bad we do no longer share the tail of the list, but I think this is okay for now, and can be added back later on, if required. |
Trying out a more relevant benchmark suggests that this branch isn't significantly slower. (Confirming the suggestion above that the short linked list copy is cheap.) Master: This branch: I still worry that the copy here will get forgotten / need undoing, especially as the list elements may not be simple reference counted objects once local scopes are implemented properly. |
1c06e3e
to
630fbd9
Compare
What about just a plain |
Now that we do not access the last value |
vm/src/scope.rs
Outdated
} | ||
|
||
pub fn new_child_scope_with_locals(&self, locals: PyDictRef) -> Scope { | ||
let mut new_locals = Vec::with_capacity(self.locals.len() + 1); | ||
new_locals.push(locals); | ||
new_locals.append(&mut self.locals.clone()); |
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.
new_locals.append(&mut self.locals.clone()); | |
new_locals.extend_from_slice(&self.locals); | |
new_locals.shrink_to_fit(); |
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 changed for extend_from_slice
but I don't think I need to call shrink_to_fit
as I create the vector with the exact needed capacity.
I hoped this change will increase the speed a little by accessing the last element without iterating but
from checking the benchmarks this does not have an effect on the speed.
Before:
After: