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

More optimizations #55

Merged
merged 7 commits into from
Sep 16, 2021
Merged

Conversation

asterite
Copy link
Contributor

@asterite asterite commented Sep 16, 2021

Follow up to #54

The old benchmark time (check #54) was this:

router 489.51k (  2.04µs) (± 4.33%)  2.02kB/op  fastest

Now it's this:

router 758.39k (  1.32µs) (± 3.57%)  1.33kB/op  fastest

After this, it seems most of the time is spent creating the params Hash of each match, and creating strings for each segment. There might be a chance to use Bytes for each segment to avoid those allocations, but maybe it would be too much (the overall API would change a lot.) Then, maybe the params Hash of a match could be lazily instantiated, but it might not be worth it if frameworks using the router will always put stuff in the params Hash. So this is probably a good stopping point :-)

It's immutable and one is created per route match. Benchmark is updated
as follows:

Before:
router 541.14k (  1.85µs) (± 3.28%)  2.02kB/op  fastest

After:
router 594.45k (  1.68µs) (± 1.06%)  1.83kB/op  fastest
The benchmark times are updated as follows:

Before:
router 590.63k (  1.69µs) (± 1.06%)  1.83kB/op  fastest

After:
router 720.56k (  1.39µs) (± 1.16%)  1.33kB/op  fastest
Given that we are only looking for the ascii characters '/' and '%', and
UTF-8 guarantees that these will always come as individual bytes in the
beginning of a character, we can simply traverse the string by bytes.
This avoids decoding UTF-8 and gives a small performance boost.

Benchmark times are updated as follows:

Before:
router 720.56k (  1.39µs) (± 1.16%)  1.33kB/op  fastest

After:
router 767.78k (  1.30µs) (± 3.48%)  1.33kB/op  fastest
This should be safer, and it seems performance isn't affected.
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

🚀 Thanks so much for doing this @asterite! I think this is a great stopping point too. It's doubled the speed. I think I'll cut a release for it so we can get it in to the hands of people to try out.

@jwoertink jwoertink merged commit 0c073c1 into luckyframework:master Sep 16, 2021
@asterite asterite deleted the more-optimizations branch September 16, 2021 15:10
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.

3 participants