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

Refactor String#to_utf16 optimizing for ascii-only #8526

Merged

Conversation

straight-shoota
Copy link
Member

No description provided.

@jkthorne
Copy link
Contributor

Do you have any benchmarks for this?

src/string/utf16.cr Outdated Show resolved Hide resolved
@straight-shoota
Copy link
Member Author

Benchmark for 6a36aad

string length 2000:

ascii new 262.98k (  3.80µs) (± 7.44%)  4.5kB/op        fastest
ascii old 179.60k (  5.57µs) (±14.67%)  4.5kB/op   1.46× slower
non-ascii new  40.85k ( 24.48µs) (±10.17%)  4.5kB/op        fastest
non-ascii old  38.74k ( 25.81µs) (± 9.81%)  4.5kB/op   1.05× slower

string length 10:

ascii new  25.78M ( 38.78ns) (±15.99%)  16.0B/op        fastest
ascii old  21.17M ( 47.25ns) (±10.44%)  32.0B/op   1.22× slower
non-ascii new   6.24M (160.36ns) (±10.87%)  32.0B/op        fastest
non-ascii old   6.06M (165.09ns) (±13.33%)  32.0B/op   1.03× slower

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Looks great!

size = 0
if ascii_only?
# size == bytesize, so each char fits in one UInt16
return to_slice.map &.to_u16
Copy link
Member

Choose a reason for hiding this comment

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

Oh, actually, this is missing the trailing null. I totally forgot about that! Sorry, maybe the other way was fine, or we just need to copy the values but add a trailing null somehow.

Copy link
Member

Choose a reason for hiding this comment

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

Also, are there specs for these cases? They should have failed with this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, right. There should be a spec for that.

@RX14 RX14 added this to the 0.32.0 milestone Dec 6, 2019
@RX14 RX14 requested a review from asterite December 6, 2019 01:13
@asterite asterite merged commit 7fd3863 into crystal-lang:master Dec 6, 2019
@straight-shoota straight-shoota deleted the refactor/string-to_utf16 branch December 6, 2019 12:12
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.

None yet

5 participants