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

Performance improvements #2

Merged
merged 1 commit into from
Dec 22, 2020
Merged

Conversation

qhwa
Copy link
Contributor

@qhwa qhwa commented Feb 20, 2020

Hi, thank you for providing this library. I was seeking one like this and found it does exactly what I expect. Perfect!

Such a library may be used widely so the performance matters.

This pull request includes several performance improvements. The performance got noticeably improved, about 2~3x faster on my laptop. Please run mix run bench/bench.exs if you are interested.

For detail information, here is why it works:

  1. String interpolation is expensive, for they need temp strings. IO.iodata_to_binary/1 (or :erlang.list_to_binary/1 used by it) does almost the same thing but with better performance.
  2. cond is executed linearly, from top to bottom. On the other hand, pattern matching works at O(log n) cost to find a matching clause. Many conditions can be skipped.

Also, this fixes #1

Please have a review at it, thanks~

1: improve `suffix/1` by using pattern matching instead

2: improve `ordinalize/1` by using `IO.iodata_to_binary/1`

3. add some tests to cover more logic branches

4. type spec improved
Copy link
Owner

@andrewhao andrewhao left a comment

Choose a reason for hiding this comment

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

Thank you @qhwa!

@andrewhao andrewhao merged commit 8963889 into andrewhao:master Dec 22, 2020
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.

Any chance you could make "suffix" public?
2 participants