Issue #12788 has been updated by Akira Matsuda.


Ary Borenszweig wrote:
> Thank you Nobu-san for reviewing and accepting this. Matsuda-san also mentioned a "patch monster" in his talk, I think now I know what he means :-)



----------------------------------------
Feature #12788: Improve String lstrip and strip performance
https://bugs.ruby-lang.org/issues/12788#change-60662

* Author: Ary Borenszweig
* Status: Closed
* Priority: Normal
* Assignee: 
----------------------------------------
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)

A benchmark:

~~~ ruby
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 :-)

---Files--------------------------------
string_lstrip_improvement.patch (685 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>