-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Apply Array#push
's resizing heuristic to #unshift
#10750
Apply Array#push
's resizing heuristic to #unshift
#10750
Conversation
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.
LGTM
Hopefully this can make it into |
Array#shift
's resizing heuristic to #unshift
Array#push
's resizing heuristic to #unshift
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 is probably fine for a quick fix.
But I think it might be beneficial to take a closer look at the applied heuristics. I could see some room for improvement there.
A worst-case scenario with this is adding an element each to the front and back of an array that's filled to capacity (a typical example would be literals). Adding just these two elements quadruples the capacity: it's doubled by both push and unshift, regardless of their order. This means allocating a lot of unnecessary memory.
If the resize operations for push and unshift would not fill the buffer all the way to the front and back, respectively, this could be avoided.
I don't know what would be a better solution and I'm not even sure this needs to be changed, but it probably deserves an evaluation and discussion. Obviously, the best solution depends on a fair balance between different use cases.
I see what you did here 😄 😉 |
We should probably merge this since it's an important fix |
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.
Let's get this for 1.1.0. There's the unanswered question about the need for 10k iterations in tests, when probably a handful ones should suffice. But we can change that later (I'd like to hear @HertzDevil opinion on the matter).
Fixes #10748.
The following illustrates some examples of the heuristic in action: