Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

append_chunk: do not make a copy if the whole chunk is appended. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fredrikekre
Copy link
Member

With

function perf(bytes)
    buf = BufferStream()
    @sync begin
        @async begin
            for i in 1:1024
                write(buf, bytes)
            end
            close(buf)
        end
        @async begin
            write(devnull, buf)
        end
    end
end

This goes from

julia> @btime perf($(rand(UInt8, 1024)));
  810.652 μs (19497 allocations: 2.69 MiB)

julia> @btime perf($(rand(UInt8, 1024^2)));
  349.328 ms (21545 allocations: 2.00 GiB)

to

julia> @btime perf($(rand(UInt8, 1024)));
  644.978 μs (17449 allocations: 1.52 MiB)

julia> @btime perf($(rand(UInt8, 1024^2)));
  119.063 ms (18473 allocations: 1.00 GiB)

@fredrikekre
Copy link
Member Author

With

diff --git a/src/BufferStream.jl b/src/BufferStream.jl
index e6ebdf4..546ab0e 100644
--- a/src/BufferStream.jl
+++ b/src/BufferStream.jl
@@ -86,7 +86,7 @@ end
 # Helper methods for read/write
 write(bs::BufferStream, x::UInt8) = write(bs, [x])
 function unsafe_write(bs::BufferStream, ref::Ptr{UInt8}, nbytes::UInt)
-    return append_chunk(bs, copy(unsafe_wrap(Array, ref, (nbytes,))))
+    return append_chunk(bs, unsafe_wrap(Array, ref, (nbytes,)))
 end
 
 # Read a single byte.

I get

julia> @btime perf($(rand(UInt8, 1024^2)));
  532.917 μs (16425 allocations: 386.08 KiB)

julia> @btime perf($(rand(UInt8, 1024^2)));
  532.686 μs (16425 allocations: 386.08 KiB)

but that seems too good to be true? Writing this to file and verifying the bytes give the correct thing though.

@fredrikekre
Copy link
Member Author

Perhaps writing to /dev/null is not a very good benchmark. Writing to a file instead:

function perf(bytes)
    buf = BufferStream()
    @sync begin
        @async begin
            for i in 1:1024
                write(buf, bytes)
            end
            close(buf)
        end
        @async begin
            open("rand.txt", "w") do io
                write(io, buf)
            end
        end
    end
end

seems to not show a big difference since the hardware is probably limiting here.

Still, with the diff in the previous comment:

julia> @btime perf($(rand(UInt8, 1024)));
  1.279 ms (16438 allocations: 386.81 KiB)

julia> @btime perf($(rand(UInt8, 1024^2)));
  2.260 s (16438 allocations: 386.81 KiB)

is pretty nice since basically nothing is allocated

@codecov
Copy link

codecov bot commented Jan 30, 2021

Codecov Report

Merging #7 (92fa5bc) into master (e592419) will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #7      +/-   ##
==========================================
+ Coverage   95.55%   95.65%   +0.09%     
==========================================
  Files           2        2              
  Lines          90       92       +2     
==========================================
+ Hits           86       88       +2     
  Misses          4        4              
Impacted Files Coverage Δ
src/BufferStream.jl 95.60% <100.00%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e592419...92fa5bc. Read the comment docs.

@fingolfin fingolfin closed this May 17, 2023
@fingolfin fingolfin reopened this May 17, 2023
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