Improve String lstrip and strip performance
This adds a fast path for lstrip_offset in the case of single byte optimizable strings. This optimization already exists in rstrip_offset but I noticed it was missing in rstrip.
I made sure to only remove ascii space, not '\0', to preserve backwards compatibility. (rstrip seems to remove '\0', but not lstrip)
s = " " * 10_000_000 time = Time.now p s.lstrip p s.strip puts Time.now - time s = " " * 100_000_000 time = Time.now p s.lstrip p s.strip puts Time.now - time
Times before: 0.195159 and 1.918969
Times after: 0.013684 and 0.143966
So a bit more than 10x faster.
By the way, Akira Matsuda-san gave a really nice closing keynote in RubyConf Brasil yesterday that encouraged all of us to use Ruby and contribute to it, so here is my first patch :-)
Updated by nobu (Nobuyoshi Nakada) almost 4 years ago
- Status changed from Open to Closed
Applied in changeset r56250.
string.c: fast path of lstrip_offset
- string.c (lstrip_offset): add a fast path in the case of single byte optimizable strings, as well as rstrip_offset. [ruby-core:77392] [Feature #12788]