jruby/jruby — pull request #794 by dmarcotte (2013-06-07 02:05:45+00:00)
Title: Fix String performance regression

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:

```bash
$ jruby bench/bench_threaded_reverse.rb 1
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
Thread 0 done
Time: 2.476
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
Thread 0 done
Time: 1.899
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
Thread 0 done
Time: 1.965
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
Thread 0 done
Time: 1.93
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
another 10 in #
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](https://travis-ci.org/jruby/jruby/builds/7857680) vs. [after](https://travis-ci.org/dmarcotte/jruby/builds/7862805)

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