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

Optimize by avoiding intermediate structures. #159

Open
tkaitchuck opened this issue Nov 9, 2020 · 1 comment
Open

Optimize by avoiding intermediate structures. #159

tkaitchuck opened this issue Nov 9, 2020 · 1 comment

Comments

@tkaitchuck
Copy link

Often collections are used in loops where multiple updates/operations are performed at once.
If there were a way to perform multiple updates at once these could be made more efficient.

The most obvious way to do this would be to optimize this:

im-rs/src/ord/map.rs

Lines 1997 to 2001 in 3f4e01a

let mut m = OrdMap::default();
for (k, v) in i {
m.insert(From::from(k), From::from(v));
}
m

and similar methods internally. Then for users taking advantage of the higher performance is just a matter of making better use of the existing traits.

In the above function, a new OrdMap is being constructed, and won't be seen by the caller until all the inserts are completed, yet every intermediate map is created and thrown out.

@SimonSapin
Copy link
Contributor

every intermediate map is created and thrown out

Every mutating operation is based on make_mut. For example in insert:

let root = PoolRef::make_mut(&self.pool.0, &mut self.root);

This only clones when a value is shared. When however the reference count is 1, the same value can be reused and mutated in place. So there’s not much creating and throwing out as you seem to think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants