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

BoundedArray: add a len() function to get the length as a usize #18073

Closed
wants to merge 2 commits into from

Conversation

jedisct1
Copy link
Contributor

@jedisct1 jedisct1 commented Nov 22, 2023

The type of the active length was changed to the smallest possible type, which is nice to get a more compact representation.

However, that change introduced unexpected side effects. For example, a.len b.len can now trigger an integer overflow.

There's also an inconsistency between functions that set the size, which all use a usize, and the way to read the size, that uses a different type. This is also inconsistent with pretty much anything else that represents a slice length.

Replace .len with len(), a function that returns the length as a usize to minimize surprise and simplify application code.

Originally suggested by @matklad

@jedisct1 jedisct1 added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. standard library This issue involves writing Zig code for the standard library. labels Nov 22, 2023
@nektro
Copy link
Contributor

nektro commented Nov 22, 2023

not sure this is worth is worth the complexity for the common case given that unless the whole array fits in less than 8 bytes there will be a massive amount of struct padding the usize can fit into

@nektro
Copy link
Contributor

nektro commented Nov 22, 2023

oh the change i thought this was, it seems was made 5 months ago. my suggestion would be to make the .len field itself a usize

@jedisct1
Copy link
Contributor Author

oh the change i thought this was, it seems was made 5 months ago. my suggestion would be to make the .len field itself a usize

It used to be a usize. The change was made in #16299 .

Since the length is small, it can usually fit in 1 or 2 bytes. So, a handful bytes are frequently saved, especially when the element type is a u8.

@andrewrk andrewrk self-requested a review November 23, 2023 02:55
lib/std/bounded_array.zig Outdated Show resolved Hide resolved
lib/std/bounded_array.zig Outdated Show resolved Hide resolved
@jedisct1 jedisct1 force-pushed the boundedarray-len branch 2 times, most recently from 95a6d29 to 4db3c3e Compare November 24, 2023 12:18
@andrewrk
Copy link
Member

I don't like this change. I think it's a bandage over a language design flaw and I'd rather treat the wound directly. I'd like to let this sit for the time being.

jedisct1 and others added 2 commits May 9, 2024 16:17
The type of the active length was changed to the smallest possible
type, which is nice to get a more compact representation.

However, that change introduced unexpected side effects.
For example, `a.len   b.len` can now trigger an integer overflow.

There's also an inconsistency between functions that set the size,
which all use a `usize`, and the way to read the size, that uses a
different type. This is also inconsistent with pretty much anything
else that represents a slice length.

Replace `.len` with `len()`, a function that returns the length as
a `usize` to minimize surprise and simplify application code.
Co-authored-by: Philipp Lühmann <[email protected]>
@andrewrk
Copy link
Member

This is the wrong solution to the problem. This will be resolved in one of two ways instead:

@andrewrk andrewrk closed this Aug 24, 2024
@andrewrk
Copy link
Member

andrewrk commented Aug 24, 2024

I proceeded with the revert for the time being (85747b2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants