seomoz/askari — pull request #86 by phs (2013-11-02 02:46:10+00:00)
Title: Zero-copy string decoding.

You can't make a jruby `RubyString` from a `ByteList`, in ruby, without making a copy. However you can totally do it in java.

Since we've already bitten the language-coupling bullet (we had `ByteList`s in our `Type` signature return types) just change them to `RubyString` and create the strings java-side. To do so we need a ruby runtime instance, which is now passed in (it is easily accessible from ruby.)

Check it:

```
$ bundle exec irb -Ilib -raskari -raskari/type
>> b = 'foo'.to_java_bytes
=> byte[102, 111, 111]@53481704
>> s = Askari::Type['raw'].decode(b); nil
=> nil
>> b[0] = 'g'.to_java_bytes[0]
=> 103
>> s
=> "goo"
>>
```

Notice I have a trailing `; nil`. That suppresses IRB from rendering the resulting string. Turns out doing so causes the associated `ByteList` to cache the string copy it creates, which would then not see the following mutation. This cache can be cleared with `ByteList#invalidate`.