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

ljust and rjust are slow #3445

Closed
clayton-shopify opened this issue Feb 8, 2017 · 2 comments
Closed

ljust and rjust are slow #3445

clayton-shopify opened this issue Feb 8, 2017 · 2 comments

Comments

@clayton-shopify
Copy link
Contributor

@clayton-shopify clayton-shopify commented Feb 8, 2017

The implementations of String#ljust and String#rjust are slow (i.e. O(n^2) running time) when MRuby is built with MRB_UTF8_STRING. This is because they repeatedly call String#size, which calls utf8_strlen, which must iterate over the whole string.

$ echo '"".ljust(100000)' | time bin/mruby
       57.46 real        57.01 user         0.21 sys

$ echo '"".rjust(100000)' | time bin/mruby
       57.17 real        56.90 user         0.14 sys

This issue was reported by https://hackerone.com/ssarong

@texrg
Copy link

@texrg texrg commented Feb 9, 2017

Please run more than one times your benchmark

very similar
57.46+57.01+0.21=114.68
57.17+56.90+0.14=114.21

@dabroz
Copy link
Contributor

@dabroz dabroz commented Feb 10, 2017

Also, these functions with an empty padding parameter will stay in an infinite loop.

dabroz added a commit to dabroz/mruby that referenced this issue Feb 10, 2017
 - String#ljust and String#rjust are now C functions to improve performance
 - infinite loop because of an empty padding argument is now prevented (ArgumentError is raised)
 - extra tests for ljust/rjust added
matz added a commit that referenced this issue Feb 10, 2017
String#ljust and String#rjust reimplementation (fix #3445)
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

No branches or pull requests

3 participants