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

Comments

Projects
None yet
3 participants
@clayton-shopify
Contributor

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

This comment has been minimized.

Show comment
Hide comment
@texrg

texrg 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

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

This comment has been minimized.

Show comment
Hide comment
@dabroz

dabroz Feb 10, 2017

Contributor

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

Contributor

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 reimplementation (fix #3445)
 - 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 matz closed this in #3447 Feb 10, 2017

matz added a commit that referenced this issue Feb 10, 2017

Merge pull request #3447 from dabroz/fix-ljust
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