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 a fast path returning Cow::Borrowed for ASCII-only prepping #4

Merged
merged 1 commit into from
Jul 13, 2023

Conversation

linkmauve
Copy link
Contributor

In all three of nodeprep, nameprep and resourceprep, there was never a case where a Cow::Borrowed() was generated, so let’s simplify the API for everyone.

This is a breaking change in the API.

@sfackler
Copy link
Owner

I don't think there's any reason those functions couldn't have a Cow::Borrowed fast path, for lower case ASCII for example.

@linkmauve
Copy link
Contributor Author

linkmauve commented Jul 12, 2023

Indeed, I’ve added the fast paths instead. I will benchmark these changes, although we are already quite a bit faster than libidn for instance.

Edit: for ASCII-only JIDs already normalized in the jid crate, this reduces the parsing time between -69.6% for the shortest possible JIDs, to -98.5% for the longest possible JIDs. And for the ones which need normalization, I measured around +6% which is quite acceptable imo.

For already normalized JIDs, this reduces the parsing time by -69.6% to
-98.5% depending on the length of the JID, and only increases it for
other JIDs by +5.8% which is quite acceptable in that uncommon case.
@linkmauve linkmauve changed the title Replace Cow<'_, str> with String Add a fast path returning Cow::Borrowed for ASCII-only prepping Jul 12, 2023
@sfackler sfackler merged commit 8887482 into sfackler:master Jul 13, 2023
1 check passed
@sfackler
Copy link
Owner

Thanks!

@sfackler
Copy link
Owner

Want me to cut a release or are there more changes you're planning on making?

@linkmauve linkmauve deleted the remove-cows branch July 15, 2023 18:03
@linkmauve
Copy link
Contributor Author

After #5 I think you can make a release, I have implemented it in the jid crate and haven’t found any issue so far.

poliorcetics pushed a commit to JustRustThings/xmpp-rs that referenced this pull request Jul 24, 2023
stringprep can make transformations to a JID, the most well-known one is
making the nodepart and domainpart lowercase but it does much more than
that.

It is extremely common to have to validate already-normalised JIDs
though, and since sfackler/rust-stringprep#4
this is exactly what the stringprep crate does, by returning
Cow::Borrowed() for common ASCII-only cases.

This commit further reduces time spent by an additional -15%..-58% when
already using this stringprep improvement, in addition to the
89.5%..98.5% change brought by this improvement (and +1.3% total when
the JID isn’t normalised yet).

For instance, my own full JID parses in 1.83 µs before these changes,
132 ns with just the stringprep optimisation, and 46 ns with also this
commit, on an i7-8700K.
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.

None yet

2 participants