Stream: helpdesk (published)

Topic: `take!` from `IOBuffer` copies


view this post on Zulip Expanding Man (Mar 08 2021 at 23:42):

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)

view this post on Zulip Expanding Man (Mar 08 2021 at 23:46):

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?

view this post on Zulip Takafumi Arakaki (tkf) (Mar 09 2021 at 00:08):

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

view this post on Zulip Expanding Man (Mar 09 2021 at 00:13):

so where are you saying it's creating an array here?

view this post on Zulip Takafumi Arakaki (tkf) (Mar 09 2021 at 00:26):

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

view this post on Zulip Expanding Man (Mar 09 2021 at 00:26):

Ok, I was so confused about the internal buffer being re-allocated I just kind of dind't believe it lol

view this post on Zulip Expanding Man (Mar 09 2021 at 00:26):

I mean, I guess it's for the same reason I said above, to preserve the data, it's just really confusing


Last updated: Dec 28 2024 at 04:38 UTC