11package org .logstash .config .ir .compiler ;
22
33import java .util .ArrayList ;
4- import java .util .Collection ;
54import java .util .Collections ;
65import java .util .List ;
76import java .util .stream .Collectors ;
8- import org .jruby .Ruby ;
9- import org .jruby .runtime .ThreadContext ;
107
118/**
129 * A syntactic closure.
@@ -18,28 +15,6 @@ final class Closure implements MethodLevelSyntaxElement {
1815 */
1916 public static final Closure EMPTY = new Closure (Collections .emptyList ());
2017
21- /**
22- * Variable declaration for the Ruby thread-context,
23- * renders as {@code final ThreadContext context}.
24- */
25- private static final VariableDefinition RUBY_THREAD_CONTEXT =
26- new VariableDefinition (ThreadContext .class , "context" );
27-
28- /**
29- * Variable declaration for the Ruby thread-context,
30- * renders as {@code final ThreadContext context = RubyUtil.RUBY.getCurrentContext()}.
31- */
32- private static final MethodLevelSyntaxElement CACHE_RUBY_THREADCONTEXT =
33- SyntaxFactory .definition (
34- RUBY_THREAD_CONTEXT , ValueSyntaxElement .GET_RUBY_THREAD_CONTEXT
35- );
36-
37- /**
38- * Variable referencing the current Ruby thread context.
39- */
40- private static final ValueSyntaxElement CACHED_RUBY_THREADCONTEXT =
41- RUBY_THREAD_CONTEXT .access ();
42-
4318 private final List <MethodLevelSyntaxElement > statements ;
4419
4520 public static Closure wrap (final MethodLevelSyntaxElement ... statements ) {
@@ -78,50 +53,10 @@ public boolean empty() {
7853
7954 @ Override
8055 public String generateCode () {
81- final Collection <MethodLevelSyntaxElement > optimized =
82- this .optimizeRubyThreadContexts ().statements ;
83- return optimized .isEmpty () ? "" : SyntaxFactory .join (
84- optimized .stream ().map (MethodLevelSyntaxElement ::generateCode ).collect (
56+ return statements .isEmpty () ? "" : SyntaxFactory .join (
57+ statements .stream ().map (MethodLevelSyntaxElement ::generateCode ).collect (
8558 Collectors .joining (";\n " )
8659 ), ";"
8760 );
8861 }
89-
90- /**
91- * Removes duplicate calls to {@link Ruby#getCurrentContext()} by caching them to a variable.
92- * @return Copy of this Closure without redundant calls to {@link Ruby#getCurrentContext()}
93- */
94- private Closure optimizeRubyThreadContexts () {
95- final ArrayList <Integer > rubyCalls = new ArrayList <>();
96- for (int i = 0 ; i < statements .size (); ++i ) {
97- if (statements .get (i ).count (ValueSyntaxElement .GET_RUBY_THREAD_CONTEXT ) > 0 ) {
98- rubyCalls .add (i );
99- }
100- }
101- final Closure optimized ;
102- if (rubyCalls .size () > 1 ) {
103- optimized = (Closure ) new Closure ().add (this ).replace (
104- ValueSyntaxElement .GET_RUBY_THREAD_CONTEXT , CACHED_RUBY_THREADCONTEXT
105- );
106- optimized .statements .add (rubyCalls .get (0 ), CACHE_RUBY_THREADCONTEXT );
107- } else {
108- optimized = this ;
109- }
110- return optimized ;
111- }
112-
113- @ Override
114- public MethodLevelSyntaxElement replace (final MethodLevelSyntaxElement search ,
115- final MethodLevelSyntaxElement replacement ) {
116- final Closure result = new Closure ();
117- for (final MethodLevelSyntaxElement element : this .statements ) {
118- result .add (element .replace (search , replacement ));
119- }
120- return result ;
121- }
122-
123- @ Override
124- public int count (final MethodLevelSyntaxElement search ) {
125- return statements .stream ().mapToInt (child -> child .count (search )).sum ();
126- }
12762}
0 commit comments