PERFORMANCE: Constant RubyHash Visitor

Fixes #7898
This commit is contained in:
Armin 2017-08-03 23:36:32 +02:00 committed by Armin Braun
parent d12dddb018
commit ac65ced5a1
2 changed files with 20 additions and 9 deletions

View file

@ -4,12 +4,23 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.jruby.RubyHash;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public final class ConvertedMap extends HashMap<String, Object> {
private static final long serialVersionUID = -4651798808586901122L;
private static final RubyHash.VisitorWithState<ConvertedMap> RUBY_HASH_VISITOR =
new RubyHash.VisitorWithState<ConvertedMap>() {
@Override
public void visit(final ThreadContext context, final RubyHash self,
final IRubyObject key, final IRubyObject value,
final int index, final ConvertedMap state) {
state.put(key.toString(), Valuefier.convert(value));
}
};
ConvertedMap(final int size) {
super((size << 2) / 3 + 2);
}
@ -22,15 +33,13 @@ public final class ConvertedMap extends HashMap<String, Object> {
return cm;
}
public static ConvertedMap newFromRubyHash(RubyHash o) {
final ConvertedMap result = new ConvertedMap(o.size());
public static ConvertedMap newFromRubyHash(final RubyHash o) {
return newFromRubyHash(o.getRuntime().getCurrentContext(), o);
}
o.visitAll(o.getRuntime().getCurrentContext(), new RubyHash.Visitor() {
@Override
public void visit(IRubyObject key, IRubyObject value) {
result.put(key.toString(), Valuefier.convert(value));
}
}, null);
public static ConvertedMap newFromRubyHash(final ThreadContext context, final RubyHash o) {
final ConvertedMap result = new ConvertedMap(o.size());
o.visitAll(context, RUBY_HASH_VISITOR, result);
return result;
}

View file

@ -87,7 +87,9 @@ public class JrubyEventExtLibrary implements Library {
public IRubyObject ruby_initialize(ThreadContext context, IRubyObject[] args) {
final IRubyObject data = args.length > 0 ? args[0] : null;
if (data instanceof RubyHash) {
this.event = new Event(ConvertedMap.newFromRubyHash((RubyHash) data));
this.event = new Event(
ConvertedMap.newFromRubyHash(context, (RubyHash) data)
);
} else {
initializeFallback(context, data);
}