Skip to content

Aligned slices are unusable #22858

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

Closed
ItsMeSamey opened this issue Feb 11, 2025 · 5 comments
Closed

Aligned slices are unusable #22858

ItsMeSamey opened this issue Feb 11, 2025 · 5 comments
Labels
question No questions on the issue tracker, please.

Comments

@ItsMeSamey
Copy link

Problem

slices with non-default alignment (like []align(1) u32) can't be used in mem.eql, sort.binarySearch, sort.partitionPoint, sort... and in many others

Proposed solution

add 'Context' variants to these function much like sort.pdqContext
or
make array argument anytype.
or
This could have been solved by #9260

Zig version:
0.14.0-dev.3187+d4c85079c

@andrewrk
Copy link
Member

no whining on the issue tracker

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2025
@ItsMeSamey
Copy link
Author

I didn't mean to to "whine", but to report a legitimate limitation i ran into it here and had to write this which isn't pretty.
It would be nicer if standard library accommodates such use cases

@andrewrk
Copy link
Member

why have you opted out of array alignment?

@ItsMeSamey
Copy link
Author

ItsMeSamey commented Feb 12, 2025

If you mean, 'why did i not use default alignment', that is because i'm reading file from disk which is guaranteed (in my use case) to Not be aligned.
Because a file read from disk is being treated as slice of structs and that file has 1 byte header + the array was written as
'packed'.

@andrewrk
Copy link
Member

in this case the solution is not to bloat the executable with a bunch of alternate std.mem functions for operating on unaligned arrays, it's to load your data into aligned arrays, or better yet actually parse the file for validity.

looks like another case of the restriction helping you write better code.

@andrewrk andrewrk added the question No questions on the issue tracker, please. label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

2 participants