JAVAFICATION: Cache filter callsite and dedup some method names

Fixes #9752
This commit is contained in:
Armin 2018-06-16 17:44:17 +02:00 committed by Armin Braun
parent 52e4f49177
commit c56d62f4ae
4 changed files with 21 additions and 9 deletions

View file

@ -21,6 +21,8 @@ import org.logstash.instrument.metrics.counter.LongCounter;
@JRubyClass(name = "AbstractOutputDelegator")
public abstract class AbstractOutputDelegatorExt extends RubyObject {
public static final String OUTPUT_METHOD_NAME = "multi_receive";
private AbstractMetricExt metric;
protected AbstractNamespacedMetricExt namespacedMetric;
@ -86,7 +88,7 @@ public abstract class AbstractOutputDelegatorExt extends RubyObject {
return metricEvents;
}
@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = OUTPUT_METHOD_NAME)
public IRubyObject multiReceive(final IRubyObject events) {
final RubyArray batch = (RubyArray) events;
final int count = batch.size();

View file

@ -11,6 +11,7 @@ import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.logstash.RubyUtil;
@ -23,9 +24,11 @@ import org.logstash.instrument.metrics.counter.LongCounter;
@JRubyClass(name = "JavaFilterDelegator")
public final class FilterDelegatorExt extends RubyObject {
private static final String FILTER_METHOD_NAME = "multi_filter";
private static final long serialVersionUID = 1L;
private IRubyObject filterClass;
private RubyClass filterClass;
private IRubyObject filter;
@ -37,6 +40,8 @@ public final class FilterDelegatorExt extends RubyObject {
private LongCounter eventMetricIn;
private DynamicMethod filterMethod;
private LongCounter eventMetricTime;
private boolean flushes;
@ -46,7 +51,8 @@ public final class FilterDelegatorExt extends RubyObject {
final IRubyObject id) {
this.id = (RubyString) id;
this.filter = filter;
this.filterClass = filter.getSingletonClass().getRealClass();
filterClass = filter.getSingletonClass().getRealClass();
filterMethod = filterClass.searchMethod(FILTER_METHOD_NAME);
final AbstractNamespacedMetricExt namespacedMetric =
(AbstractNamespacedMetricExt) filter.callMethod(context, "metric");
metricEvents = namespacedMetric.namespace(context, MetricKeys.EVENTS_KEY);
@ -66,6 +72,7 @@ public final class FilterDelegatorExt extends RubyObject {
eventMetricIn = LongCounter.DUMMY_COUNTER;
eventMetricTime = LongCounter.DUMMY_COUNTER;
this.filter = filter;
filterMethod = filter.getMetaClass().searchMethod(FILTER_METHOD_NAME);
flushes = filter.respondsTo("flush");
return this;
}
@ -118,8 +125,8 @@ public final class FilterDelegatorExt extends RubyObject {
public RubyArray multiFilter(final RubyArray batch) {
eventMetricIn.increment((long) batch.size());
final long start = System.nanoTime();
final RubyArray result = (RubyArray) filter.callMethod(
WorkerLoop.THREAD_CONTEXT.get(), "multi_filter", batch
final RubyArray result = (RubyArray) filterMethod.call(
WorkerLoop.THREAD_CONTEXT.get(), filter, filterClass, FILTER_METHOD_NAME, batch
);
eventMetricTime.increment(
TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)

View file

@ -108,20 +108,23 @@ public final class OutputStrategyExt {
return close(context);
}
@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME)
public final IRubyObject multiReceive(final ThreadContext context, final IRubyObject events)
throws InterruptedException {
return output(context, events);
}
protected final void initOutputCallsite(final RubyClass outputClass) {
outputMethod = outputClass.searchMethod("multi_receive");
outputMethod = outputClass.searchMethod(AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME);
this.outputClass = outputClass;
}
protected final void invokeOutput(final ThreadContext context, final IRubyObject batch,
final IRubyObject pluginInstance) {
outputMethod.call(context, pluginInstance, outputClass, "multi_receive", batch);
outputMethod.call(
context, pluginInstance, outputClass, AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME,
batch
);
}
protected abstract IRubyObject output(ThreadContext context, IRubyObject events)

View file

@ -70,7 +70,7 @@ public class FakeOutClass extends RubyObject {
return this;
}
@JRubyMethod(name = "multi_receive")
@JRubyMethod(name = AbstractOutputDelegatorExt.OUTPUT_METHOD_NAME)
public IRubyObject multiReceive(final IRubyObject args) {
multiReceiveCallCount++;
multiReceiveArgs = args;