Fix String performance regression #794

Merged
merged 1 commit into from Jun 7, 2013

Projects

None yet

3 participants

@dmarcotte
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!

@dmarcotte dmarcotte Fix String performance regression
Port the single byte COW heuristic from the 1.8 code path to 1.9.

This fixes #168.
110cf75
@jrubyci jrubyci merged commit 110cf75 into jruby:master Jun 7, 2013

1 check failed

default The Travis CI build failed
Details
@dmarcotte dmarcotte deleted the dmarcotte:string-optimize branch Jun 7, 2013
@headius
Member
headius commented Jun 10, 2013

Great find, btw! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment