-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
remove RingBuffer; remove BoundedArray; use @memmove
#24699
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
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.
Soon there will be one ring buffer to rule them all!
Losing I also have structs containing |
@@ -782,12 +798,29 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig | |||
assert(self.capacity >= new_len); | |||
const to_move = self.items[index..]; | |||
self.items.len = new_len; | |||
mem.copyBackwards(T, self.items[index + count ..], to_move); | |||
@memmove(self.items[index + count ..][0..to_move.len], to_move); |
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.
Why extra [0..to_move.len]? It provides no new asserts since memmove already checks for length equality
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.
the dest could be longer
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.
Here is napkin math to prove it can't be longer
const old_len = self.items.len;
const new_len = old_len + count;
const to_move_len = old_len - index;
self.items.len = new_len;
self.items[index + count ..].len == new_len - index - count == old_len + count - index - count == old_len - index == to_move_len == to_move.len
BoundedArray was safely copyable in the sense that it technically worked, but that was never really a wise operation to do since it pointlessly copied all the undefined items after len. |
This use case is handled by ArrayListUnmanaged via the "...Bounded" method variants, and it's more optimal to share machine code, versus generating multiple versions of each function for differing array lengths.
this function is wacky, should not have been merged
Progress towards #19231
Was it a problem? It was really intended for use with short arrays. I'm really going to miss the convenience of |
Yes, it was a problem in the sense that it reduced friction too much. In this diff you can see how it was used by the autodoc markdown parsing function to avoid allocations, but in exchange ended up doing a lot of unnecessary work to the point where heap allocation would have been better.
And you are not alone. I am aware this is a controversial change. I recommend to copy it into your application, or create a reusable package for the ecosystem. |
The When initialized using However, these functions make no sense in that context, which adds to the confusion. Having separate types, one for unbounded lists that use an allocator, and another for lists backed by a static buffer, would help reduce this ambiguity. Previously,
However, this puts Zig in an unfortunate situation, similar to what we’ve seen in the Rust ecosystem: where third-party dependencies are needed for extremely basic and common tasks. That, in turn, raises concerns about what happens when those dependencies break, change, become abandoned, or even get compromised. |
Well, we disagree on how basic and common the task is for this data structure, particularly when combined with other idiomatic practices. I personally would never add a dependency on that package, directly or transitively. While I understand and respect your opinion, and I recognize that mine is also just an opinion, ultimately I'm responsible for this stuff, I suffer the most consequences from these decisions, and so I must go with my conviction on this one. |
Although I still plan to merge this, I am happy to continue the discussion.
I apologize; this is a cop-out and not a true recommendation. To be honest, I recommend to look at your code carefully and categorize it based on where the limit comes from:
|
Progress towards #19231
Closes #19954
Upgrade Guide
ArrayListUnmanaged now has "Bounded" variants of all the "AssumeCapacity" methods: