diff --git a/.claude/tasks/optimization.md b/.claude/tasks/optimization.md deleted file mode 100644 index 3367c003d5..0000000000 --- a/.claude/tasks/optimization.md +++ /dev/null @@ -1,68 +0,0 @@ -# oap-template -1. the generated code should be extremely fast -2. duplication is allowed to increase speed -3. AbstractTemplateEngineTest#listener allows you to get the generated code - -TemplateEngineWithTest#testBlockWithMultipleFields - -old code: -``` - // --- with ( child ) START BODY - // field - if ( with_1 != null ) { - java.lang.String s_with_1_field = with_1.field; - if ( s_with_1_field != null ) { - acc.accept( s_with_1_field ); - } else { - acc.acceptNull( java.lang.String.class ); - } - } else { - acc.acceptNull( java.lang.String.class ); - } - acc.acceptText( "-" ); - // field2 - if ( with_1 != null ) { - java.lang.String s_with_1_field_ = with_1.field2; - if ( s_with_1_field_ != null ) { - acc.accept( s_with_1_field_ ); - } else { - acc.acceptNull( java.lang.String.class ); - } - } else { - acc.acceptNull( java.lang.String.class ); - } - // --- with ( child ) END body - -``` - -optimized code: - -``` - // --- with ( child ) START BODY - // field - if ( with_1 != null ) { - java.lang.String s_with_1_field = with_1.field; - if ( s_with_1_field != null ) { - acc.accept( s_with_1_field ); - } else { - acc.acceptNull( java.lang.String.class ); - } - - acc.acceptText( "-" ); - - java.lang.String s_with_2_field_ = with_1.field2; - if ( s_with_2_field_ != null ) { - acc.accept( s_with_2_field_ ); - } else { - acc.acceptNull( java.lang.String.class ); - } - - } else { - acc.acceptNull( java.lang.String.class ); - - acc.acceptText( "-" ); - - acc.acceptNull( java.lang.String.class ); - } - // --- with ( child ) END body -``` \ No newline at end of file diff --git a/oap-formats/oap-template/src/main/java/oap/template/render/AstRenderBlockWith.java b/oap-formats/oap-template/src/main/java/oap/template/render/AstRenderBlockWith.java index 4df6b4cb97..fd69ebcaf9 100644 --- a/oap-formats/oap-template/src/main/java/oap/template/render/AstRenderBlockWith.java +++ b/oap-formats/oap-template/src/main/java/oap/template/render/AstRenderBlockWith.java @@ -50,15 +50,22 @@ public AstRenderBlockWith( String scopePath, TemplateType type, AstRender scopeA @Override public void render( Render render ) { - String sv = render.newVariableWithCustomPrefix( "with_" ); - render - .ntab().append( "// --- with ( %s )", scopePath ) - .ntab().append( "%s %s = null;", scopeType.getTypeName(), sv ); - scopeAst.render( render.withScopeVar( sv ) ); + render.ntab().append( "// --- with ( %s )", scopePath ); - render.ntab().append( "// --- with ( %s ) START BODY ", scopePath ); + String sv; + if( isSingleLevelField( scopeAst ) ) { + AstRenderField sf = ( AstRenderField ) scopeAst; + new AstRenderField( sf.fieldName, sf.type, sf.forceCast, sf.castType ).render( render ); + sv = render.newVariable( sf.fieldName ).name; + } else { + sv = render.newVariableWithCustomPrefix( "with_" ); + render.ntab().append( "%s %s = null;", scopeType.getTypeName(), sv ); + scopeAst.render( render.withScopeVar( sv ) ); + } + + render.ntab().append( "// --- with ( %s ) START BODY", scopePath ); - Render bodyRender = render.newBlock().withField( sv ).withParentType( scopeType ); + Render bodyRender = render.newBlock().withFieldDirect( sv ).withParentType( scopeType ); boolean hasNullable = bodyChildren.stream().anyMatch( c -> extractNullable( c ) != null ); if( hasNullable ) { @@ -93,6 +100,14 @@ public void render( Render render ) { render.ntab().append( "// --- with ( %s ) END body ", scopePath ).n(); } + private static boolean isSingleLevelField( AstRender scopeAst ) { + if( !( scopeAst instanceof AstRenderField sf ) ) return false; + if( sf.children.size() != 1 ) return false; + AstRender child = sf.children.getFirst(); + if( !( child instanceof AstRenderNullable ) ) return false; + return child.children.size() == 1 && child.children.getFirst() instanceof AstRenderCaptureScope; + } + private static AstRenderNullable extractNullable( AstRender child ) { if( child instanceof AstRenderNullable n ) return n; if( child instanceof AstRenderComment c && c.children.size() == 1 && c.children.getFirst() instanceof AstRenderNullable n ) return n; diff --git a/oap-formats/oap-template/src/main/java/oap/template/render/Render.java b/oap-formats/oap-template/src/main/java/oap/template/render/Render.java index 3e8530782b..4659c95c79 100644 --- a/oap-formats/oap-template/src/main/java/oap/template/render/Render.java +++ b/oap-formats/oap-template/src/main/java/oap/template/render/Render.java @@ -208,6 +208,11 @@ public Render withScopeVar( String scopeVar ) { this.templateAccumulatorName, this.tab, ids, tryVariable, booleanIfVar, scopeVar, rootField, prefix, rangeVarMap, variables ); } + public Render withFieldDirect( String field ) { + return new Render( this.sb, this.templateName, this.content, this.parentType, this.templateAccumulator, field, + this.templateAccumulatorName, this.tab, ids, tryVariable, booleanIfVar, scopeVar, rootField, field, rangeVarMap, variables ); + } + public Render withRootField( String rootField ) { return new Render( this.sb, this.templateName, this.content, this.parentType, this.templateAccumulator, this.field, this.templateAccumulatorName, this.tab, ids, tryVariable, booleanIfVar, scopeVar, rootField, prefix, rangeVarMap, variables ); diff --git a/oap-formats/oap-template/src/test/java/oap/template/TemplateEngineOptimizationTest.java b/oap-formats/oap-template/src/test/java/oap/template/TemplateEngineOptimizationTest.java index fa00bca7ec..510b315c6e 100644 --- a/oap-formats/oap-template/src/test/java/oap/template/TemplateEngineOptimizationTest.java +++ b/oap-formats/oap-template/src/test/java/oap/template/TemplateEngineOptimizationTest.java @@ -17,6 +17,11 @@ public void testBlockWithMultipleFields() { "{{% with child }}{{ field }}-{{ field2 }}{{% end }}", STRING, null ).render( c ).get() ) .isEqualTo( "f1-f2" ); - assertThat( listener.javaCode ).containsOnlyOnce( "if ( with_1 != null ) {" ); + assertThat( listener.javaCode ) + .containsOnlyOnce( """ + oap.template.TestTemplateClass s_child = s.child; + // --- with ( child ) START BODY + if ( s_child != null ) { + """ ); } } diff --git a/pom.xml b/pom.xml index 5672de9aa4..8a9d806b2b 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - 25.6.1 + 25.6.2 25.0.1 25.0.0