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
container/list: insert and remove is re-used but contains inefficiencies #27747
Comments
I would be somewhat surprised if those lines have any performance effect at all: the compiler should be able to inline both (Number of statements and number of function calls do not necessarily correlate to performance.) |
Beyond that, I would expect that list usage in practice is pretty much completely dominated by cache effects, and redundant stores have very little impact on the cache hierarchy. Unoptimized dead stores may make a big difference in microbenchmarks of very small lists, but I would expect that they're in the noise in the vast majority of real programs. |
This did actually make some a performance impact on my system: Before:
After:
This is suffciently simple to implement that I think we should include it. Your right that the performance of this probably doesn't make a difference in most applications. Though it may matter for LRU's. |
@ValarDragon you can use benchstat to get a better comparison |
Sure:
|
…lements within a list. Methods MoveToFront, MoveToBack, MoveBefore and MoveAfter remove the given element from the list and later add the same element at a desired position. This has resulted in some inefficiencies, with remove method making 4 unncessary operations and insert method making 2. The commit merges these two methods into a method called move. Fixes golang#27747
Change https://golang.org/cl/144998 mentions this issue: |
Benchmark results:
|
Within list, currently MoveToFront is implemented as follows:
Because we're inserting the node back in immediately, the following 4 lines in
remove
are unnecessary, as their effect is undone by insert:(similarly the lines in insert that set e.list and increase l.len are also unnecessary)
This pattern of
l.insert(l.remove(e), node)
is used a couple of times in this function. The efficiency could be improved by making a single combined function to do both remove from one location and insert elsewhere, which doesn't perform these unnecessary lines.This would increase efficiency by reducing the number of statements, and by reducing function call overhead.
I would be happy to write a benchmark for this, and submit a CL for this if it seems valuable.
The text was updated successfully, but these errors were encountered: