The documentation claims that take!(::IOBuffer)
does not copying, but it's clearly lying:
julia> io = IOBuffer(zeros(UInt8, 2^18));
julia> v = take!(io);
julia> io.data[1] = 0xff
0xff
julia> v[1]
0x00
julia> @btime take!($io);
17.486 μs (2 allocations: 256.14 KiB)
As far as I can tell this happens whenever the buffer is not writable. I guess because it assumes in those cases the data in the buffer has to be preserved at all costs?
take!
's docstring is correct. it just does not say that it will also create a new array
julia> io = IOBuffer();
julia> data = io.data;
julia> take!(io) === data
true
julia> io.data === data
false
so where are you saying it's creating an array here?
take!(io) === data
indicates that it returns the internal buffer as-is. io.data !== data
indicates that the internal buffer is re-allocated. You can also see the code here: https://github.com/JuliaLang/julia/blob/970edc7d7734ce0175177699e474ffc8f7b9eb27/base/iobuffer.jl#L391
Ok, I was so confused about the internal buffer being re-allocated I just kind of dind't believe it lol
I mean, I guess it's for the same reason I said above, to preserve the data, it's just really confusing
Last updated: Nov 06 2024 at 04:40 UTC