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

Add Slice#hexdump(io : IO) overload #10496

Merged
merged 1 commit into from
Apr 14, 2021

Conversation

HertzDevil
Copy link
Contributor

Resolves #10495.

The private method hexdump_line refactors most of the original overload and the IO overload's bodies together; this leads to a slight speedup even for the overload that returns a String. The new overload flushes the IO and returns the number of bytes written, similar to Base64.encode(data, io : IO).

Benchmark and results. (The oddity with N = 0 seems to be that the benchmark program doesn't inline bytes.hexdump into the first report block's body.)

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

I would probably prefer to reduce the implementation of #hexdump to calling hexdump(io). String size calculation still makes sense, of course. But we can save two separate loop implementations. The overhead is an additional IO allocation, but that's negligible and IMO more concise code is more worth than a slight optimization in this piece.

Comment on lines +283 to +285
io = IO::Memory.new
Bytes.empty.hexdump(io).should eq(0)
io.to_s.should eq("")
Copy link
Member

Choose a reason for hiding this comment

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

Using String.build might make these examples a bit more concise.

@asterite
Copy link
Member

I think that it might actually make sense to have hexdump not allocate an IO if it's not too hard (well, it's already in this PR). We do a similar optimization for converting numbers into strings.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Okay, it's fine to go with the implementation.

@asterite asterite added this to the 1.1.0 milestone Apr 14, 2021
@asterite asterite merged commit 5f79cfc into crystal-lang:master Apr 14, 2021
@HertzDevil HertzDevil deleted the feature/slice-hexdump-io branch April 14, 2021 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Slice#hexdump with an IO
3 participants