Skip to content

Fix String performance regression#794

Merged
1 commit merged intojruby:masterfrom
dmarcotte:string-optimize
Jun 7, 2013
Merged

Fix String performance regression#794
1 commit merged intojruby:masterfrom
dmarcotte:string-optimize

Conversation

@dmarcotte
Copy link
Contributor

Had a peek at #168 and determined the problem is indeed a pathological copy-on-write case: the 1.9 code path was missing a heuristic that the 1.8 code uses around not sharing for single character substrings. For 1.9, each read of a character in bench_threaded_reverse.rb was marking the (very long) source string shared, causing an expensive copy of it for each character written in the bench. Lots of extra work.

This pull ports that heuristic to the 1.9 case, taking into account that the string is probably non-ascii encoded.

My before numbers looks very similar to the ones @headius posted in #168. Here are the after numbers on my machine:

$ jruby bench/bench_threaded_reverse.rb 1
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
another 10 in #<Thread:0x70b11164 run>
Thread 0 done
Time: 2.476
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
another 10 in #<Thread:0x235dc721 run>
Thread 0 done
Time: 1.899
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
another 10 in #<Thread:0x5b0010ec run>
Thread 0 done
Time: 1.965
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
another 10 in #<Thread:0x17be3bb2 run>
Thread 0 done
Time: 1.93
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
another 10 in #<Thread:0x224478fe run>
Thread 0 done
Time: 1.947

Natural questions would be "Is this optimization too localized to the benchmark?" and "How do we know this doesn't degrade performance elsewhere". Here are my thoughts:

  • skipping COW for single bytes doesn't have a lot of overhead at all and avoids risking a copy of a much larger string
  • a very similar optimization has lived for quite a while in 1.8 mode so we can reason that there shouldn't be any hidden pitfalls here
  • while not a perf environment by any means, Travis run times didn't take a hit from this, showing very comparable run times: before vs. after

Hopefully I haven't missed anything here... let me know if there's any questions!

Port the single byte COW heuristic from the 1.8 code path to 1.9.

This fixes jruby#168.
@ghost ghost merged commit 110cf75 into jruby:master Jun 7, 2013
@dmarcotte dmarcotte deleted the string-optimize branch June 7, 2013 13:27
@headius
Copy link
Member

headius commented Jun 10, 2013

Great find, btw! Thank you!

This pull request was closed.
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.

2 participants