Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Hex encoding can be significantly faster #171

@Kixunil

Description

@Kixunil

I figured if we encode into a temporary array first and then call a single write_str the performance improves quite a lot.
With reasonably simple unsafe we can squeeze out a bit better performance on top of that.

Relevant benchmarks - encoding 32B:

test tests::thirtytwo::trivial::sink        ... bench:       1,577 ns/iter (+/- 567)
test tests::thirtytwo::trivial::string      ... bench:       1,882 ns/iter (+/- 194)
test tests::thirtytwo::with_buf::sink       ... bench:          57 ns/iter (+/- 29)
test tests::thirtytwo::with_buf::string     ... bench:          92 ns/iter (+/- 3)
test tests::thirtytwo::with_unsafe::sink    ... bench:          47 ns/iter (+/- 21)
test tests::thirtytwo::with_unsafe::string  ... bench:          92 ns/iter (+/- 12)
test tests::thirtytwo::maybe_uninit::sink   ... bench:          43 ns/iter (+/- 6)
test tests::thirtytwo::maybe_uninit::string ... bench:          79 ns/iter (+/- 40)

trivial is the current impl, with_buf uses temporary buffer without unsafe, with_unsafe uses unsafe only to call from_utf8_unchecked, maybe_uninit also uses uninit buffer. (IOW "more unsafe")

sink only passes the buffer into black box, while string calls to_string(). The performance difference is larger here because a single write avoids reallocation.

Full bench code with other benchmarks

I intend to submit a PR with this together with other refactors mentioned earlier, my question is if I should use unsafe or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions