#Is std.Io.Writer.fromArrayList a trap?

1 messages · Page 1 of 1 (latest)

slender pilot
#

It crashes on any write, and never resize the ArrayList. It seems like it is exactly the same as std.Io.Writer.fixed.
So 2 questions, how do I get Writer (with allocator) that does not crash on every write?
And what is the difference between fromArrayList(&al), and fixed(al.items)?

lone walrus
#

std.Io.Writer.Allocating is what you're looking for

#

and to answer the second question, the writer takes ownership of the data in the ArrayList when you call fromArrayList. you can't use that array list afterwards unless you call toArrayList

#

and fixed(al.items) means, it just uses the array list data as the buffer. the data in the array list will be overwritten and the buffer will not grow

slender pilot
lone walrus
#

well, yeah it's a different type

slender pilot
#

I still did not get used to that an "Interface" may never be mentioned, but still works...

#
var compressed_writer = std.Io.Writer.Allocating.init(self.a);
var x = std.compress.flate.Compress.init(&compressed_writer.writer, tmp, .zlib, .default)

🤔 should this code work? std.Io.Writer.Allocating.init does not create big enough buffer for std.compress.flate.Compress.init.

lone walrus
#

the writer i mean

slender pilot
#

But I have no clue, how much I will need, why std.Io.Writer.Allocating does not allocate more, if it have an allocator? Why I need to give it both allocator, and correct guess of size needed?

lone walrus
#

the buffer should grow. that's the point of Allocating

slender pilot
slender pilot
lucid violet
#

How is the compress code supposed to know it's dealing with an allocating writer?

#

It only has the Writer interface, not the AllocatingWriter

lone walrus
#

what's the value of compressed_writer.writer.buffer.len?

slender pilot
lone walrus
#

even after initializing it with initCapacity?

slender pilot
#

And I do not think it should be anything else, it should allocate as necessary

slender pilot
lone walrus
#

well sostd.compress.flate.Compress.init assumes it's a writer with a fixed buffer

#

looks like you can't use an Writer.Allocating with it

slender pilot
#

What if I will call initCapacity(9) , and for whatever reason in next version it will break because the condition will change to > 9 for whatever reason?
I think that assert is wrong, it should ask writer if it have enough space, not assert on it's internal state.

ornate radish
#

yeah, the Writer interface is missing something here

slender pilot
slender pilot
ornate radish
#

it focuses on implementation with fixed size buffer, it doesn't even have a function for ensureCapacity

#

it only has ensureUnusedCapacity

slender pilot
#

🤔 ok I am missing the detail that would make the second one wrong.

#

Why would you ever cared about used capacity?

ornate radish
#

if the Writer interface has ensureCapacity, then Compress.init can try to call ensureCapacity and assert on its result

ornate radish
#

so it only cares about the total capacity, not really about the unused capacity

#

since you can have the total capacity being 100 and the unused being 0

slender pilot
ornate radish
#

maybe, this is only an assumption, I haven't checked the implementation of Compression that closely

slender pilot
ornate radish
#

flush first before you pass it to the Compress writer?

slender pilot
ornate radish
slender pilot
#

Well that might be another bug, that I am not going to investigate right now.

ornate radish
#

not really sure though

slender pilot
#

me neither, but I am pretty sure, that what I reported is a bug.

ornate radish
#

could be, at least we'll hear from the devs directly

slender pilot
#

I do not believe, that it could be intended, that internal buffer of Writer needs to be prealocated to 9 bytes (or more)

lone walrus
#

did the buffer length increase past the initial capacity?

slender pilot
#

yes

lone walrus
#

i guess you're right. let's see what they say

fossil fulcrum
#

I seem to recall seeing that the compressor stuff have a fixed required minimum buffer size, at one point asserted but maybe broke moving to io.

slender pilot
fossil fulcrum
#

Just saw the bug in the tracker channel, and I agree.

gritty gulch
#

I also do think it is a legacy oversight for the compressed writer case as those asserts would make sense pre io where we all used fixed buffers and allocators as @fossil fulcrum mentioned but now with dynamic growth stuff I feel thats very limiting >~<

zealous dove
#

yes, its not an issue with the writer interface, rather the code that assumes the buffer is fixed in size.

slender pilot
fossil wyvern
# lone walrus `std.Io.Writer.Allocating` is what you're looking for

It always confuses the shit out of me when I see something like std.Io.Writer.Allocating contains an std.Io.Writer

I get it's a code organization thing, and Allocating is basically just "wrapping" Writer and giving some more functionality, but it still throws me every time I see code structured that way. I think my expectation would be to see something like std.Io.AllocatingWriter

drifting snow
#

i do my library this way

crystal shell
slender pilot
crystal shell
crystal shell
zealous dove
#

Writer.Allocating is just an implementation, it is not special.

dont assume there are no other possible use cases, even if writing to a growing buffer is the only usecase
Writer.Allocating is not the only possible way to do that, what if you want a different growth pattern
or different allocation strategy that changing the allocator alone cant provide.
what if your growing buffer is within a more complex allocation

slender pilot
zealous dove
#

you absolutely can write your own writer implementation

crystal shell
crystal shell
zealous dove
#

what?
APIs dont need to, and should not, assume a writer is growable (if possible).
I am just saying they shouldn't assume they are fixed in size either, you can write code that is compatible with both by just changing asserts on the buffer len to a ensureUnusedCapacity call.

#

that is the only issue I am aware of

slender pilot
crystal shell
zealous dove
#

it panics if it doesnt grow, which is equivalent behaviour with fixed buffer writers as a direct assert on its length.
but if it can grow, it will, or should, if it doesnt for some reason that is up to the implementation

crystal shell
#

yeah, now i get it

zealous dove
#

I should say, non growing writers, instead of fixed buffer as that is its own thing

slender pilot