zig icon indicating copy to clipboard operation
zig copied to clipboard

Update std to use pointer subtraction when appropriate

Open SeanTUT opened this issue 1 year ago • 4 comments

In many placed in std, there are a few instances where we take the results of two @intFromPtr values and subtract them to perform pointer subtraction, which is now unnecessary (and suboptimal given @intFromPtr is not usable at comptime) with the addition of pointer subtraction as a language feature in #20089. This PR aims to replace instances of @intFromPtr subtraction with pointer subtraction.

SeanTUT avatar Jul 21 '24 17:07 SeanTUT

I also aim to generally reduce unnecessary @intFromPtr usage or to at least use alternative methods at comptime where appropriate

SeanTUT avatar Jul 21 '24 18:07 SeanTUT

Closing for now as I realize that I am not well versed enough in wasm to make the zig1 changes necessary for bootstrapping zig with pointer subtraction in the standard library

SeanTUT avatar Jul 21 '24 18:07 SeanTUT

zig1.wasm isn't manually written! It's a WASM binary which can be generated by running zig build update-zig1. Feel free to re-open this PR and I can commit a zig1 update for you (a Zig core team member has to be the one to commit those updates for security reasons).

mlugg avatar Jul 21 '24 19:07 mlugg

zig1.wasm isn't manually written! It's a WASM binary which can be generated by running zig build update-zig1. Feel free to re-open this PR and I can commit a zig1 update for you (a Zig core team member has to be the one to commit those updates for security reasons).

Thank you, that would be perfect. Sorry for the misunderstanding

SeanTUT avatar Jul 22 '24 00:07 SeanTUT

This has covered pretty much all of the obvious pointer subtraction cases I could find on a cursory look of the standard library. Given that this PR is just for the cases in the standard library and not the compiler, I will be submitting this for review.

The one bit of code I've added here that I'm iffy on, is in heap.zig, where I provide a comptime implementation of sliceContainsPtr and sliceContainsSlice, making FixedBufferAllocator comptime available. Due to a lack of pointer comparison, the comptime solution had to be implemented with a loop, giving it an O(n) time complexity.

SeanTUT avatar Jul 30 '24 02:07 SeanTUT

Sorry, it bitrotted before I got around to it.

andrewrk avatar Jul 17 '25 03:07 andrewrk