I’m still in the midst of the JI refactoring, and I’m coming across a
few places where it seems like features represent enough complexity and
enough code that they should just be deleted and reconsidered. Usually
these are obscure cases not covered by any current tests. Here’s one
example…
Ruby Array can be converted to a Java array in a number of ways. Some of
them are the obvious ones you know…the 99% cases:
ary.to_java # creates an Object array
ary.to_java(type) # creates an array of the specified type
type can be a class name (string or symbol) or a shortcut like :int
But then there’s a few other cases that no code anywhere in our tests
appears to use:
ary.to_java(type, dimensions)
creates an array of the specified type and specified dimensions,
recursively converting contained arrays
ary.to_java(type, dimensions, fill)
creates an array of the specified type and specified dimensions,
filling extra spaces with the fill value to make it even
I think I’d argue these two options should probably be punted. The code
involved is rather complicated, complicated enough that I don’t want
to port it to the new, faster logic and I don’t want to maintain and
write tests for it where none exist now. And I think there’s a very
strong case to be made that there’s little gain having these built-in
but implemented in Ruby when a few lines of inject logic could do both
of them pretty tidily in user code. That is, if users were doing this.
Eliminating these two variants would allow me to delete several hundred
lines of Array-coercing code. And the payoff is pretty big from this
rework:
code:
require ‘java’
require ‘benchmark’
TIMES = (ARGV[0] || 5).to_i
TIMES.times do
Benchmark.bm(30) do |bm|
bm.report(“control”) {a = [1,2,3,4]; 100_000.times {a}}
bm.report(“ary.to_java”) {a = [1,2,3,4]; 100_000.times {a.to_java}}
bm.report(“ary.to_java :object”) {a = [1,2,3,4]; 100_000.times
{a.to_java :object}}
bm.report(“ary.to_java :string”) {a = [1,2,3,4]; 100_000.times
{a.to_java :string}}
end
end
JRuby 1.1.3:
user system total real
control 0.015000 0.000000 0.015000 ( 0.015103)
ary.to_java 9.614000 0.000000 9.614000 ( 9.614086)
ary.to_java :object 10.657000 0.000000 10.657000 ( 10.656700)
ary.to_java :string 11.272000 0.000000 11.272000 ( 11.272060)
JRuby trunk (1.1.4):
user system total real
control 0.012000 0.000000 0.012000 ( 0.011422)
ary.to_java 0.302000 0.000000 0.302000 ( 0.301836)
ary.to_java :object 0.315000 0.000000 0.315000 ( 0.315322)
ary.to_java :string 0.540000 0.000000 0.540000 ( 0.540372)
Thoughts? Is anyone using the multi-dimensional Array#to_java variants?
Perhaps you’d be interested in providing a 5-line version with tests?
- Charlie
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email