Skip to content

Conversation

@travisstaloch
Copy link
Contributor

@travisstaloch travisstaloch commented Aug 27, 2025

this PR makes it easier to write to an aligned array list. previously you needed an adapter.

@travisstaloch
Copy link
Contributor Author

realized this should likely be named AllocatingAligned and reference array_list.Aligned instead of deprecated ArrayListAlignedUnmanaged. will change after the rest of the ci jobs finish.

@travisstaloch
Copy link
Contributor Author

travisstaloch commented Aug 28, 2025

I'll leave this as-is since all ci checks passed and I'd prefer to have feedback before triggering another run. But I have those 2 name changes ready to go if wanted.

this commit makes it easier to write to an aligned array list.
previously you needed to use an adapter such as
`aligned_list.writer(allocator).adaptToNewApi(&.{});`.

`Io.Writer.Allocating` becomes an `AllocatingAligned(.fromByteUnits(1))`
similar to how `std.ArrayList(u8)` is a
`std.array_list.Aligned(u8, null)`.

* I've turned off zig fmt to make this diff easier to read.  Next commit
will be correctly formatted.
added a test to check for compile errors and one to check a few
alignments.  adding the second test exposed the deinit error i.e.
Allocation alignment X does not match free alignment 1.
@travisstaloch travisstaloch force-pushed the io-writer-aligned-allocating branch from 8084f56 to 9b9ac5d Compare August 29, 2025 15:57
@travisstaloch travisstaloch changed the title add Io.Writer.AlignedAllocating add Io.Writer.AllocatingAligned Aug 29, 2025
@travisstaloch
Copy link
Contributor Author

This needed a rebase anyway so just pushed the name changes and added a couple extra tests with different alignments. The new test errored with 'Allocation alignment X doesn't match free alignment 1' and showed that buffer needed an @alignCast in deinit().

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending this! I agree we need something like this, although there are a few different ways of tackling the problem.

Since the buffer field will continue to be alignment-erased, perhaps a runtime-known alignment API is the way to go on this one.

I'd like to explore that option before committing to this generic version.

return .{
.allocator = allocator,
.writer = .{
.buffer = try allocator.alloc(u8, capacity),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.buffer = try allocator.alloc(u8, capacity),
.buffer = try allocator.alignedAlloc(u8, alignment, capacity),

@travisstaloch
Copy link
Contributor Author

I'd like to explore that option before committing to this generic version.

Would you like me to work on that or would you like to? If me, do you have any suggestions about how to implement? I'm imagining perhaps an alignment field instead of a comptime parameter and type erased methods. Is that the direction you'd like to go?

@andrewrk
Copy link
Member

I'm trying it locally now. I think it's going to be a nice way to go

@andrewrk
Copy link
Member

Looks like you might have seen the relevant changes in #25077 already. Do you think it can address your use case as well?

@travisstaloch
Copy link
Contributor Author

travisstaloch commented Aug 30, 2025

Yes my use case is covered by Allocating.initOwnedSliceAligned and toArrayListAligned. Thank you! Closing this in favor of #25077.

@travisstaloch
Copy link
Contributor Author

@andrewrk I think I'll open a follow up PR with tests that cover the new *Aligned methods with alignment greater than 1 unless you're confident they are covered elsewhere.

@travisstaloch travisstaloch deleted the io-writer-aligned-allocating branch August 30, 2025 06:48
@andrewrk
Copy link
Member

I'd appreciate that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants