Repository navigation
Value slots, preencoded control flow, and bytecode codegen - #149
Merged
Merged
Conversation
Contributor
|
Please run |
BernardoPe
force-pushed
the
perf/value-slots
branch
2 times, most recently
from
September 18, 2026 22:55
e1846d3 to
44d37f0
Compare
BernardoPe
force-pushed
the
perf/value-slots
branch
from
September 23, 2026 18:47
44d37f0 to
56b3dc6
Compare
Contributor
|
Please run |
BernardoPe
force-pushed
the
perf/value-slots
branch
from
October 9, 2026 07:44
56b3dc6 to
b284a8f
Compare
Contributor
|
Please run |
BernardoPe
force-pushed
the
perf/value-slots
branch
2 times, most recently
from
October 9, 2026 09:18
af7a98c to
1ecd4dc
Compare
BernardoPe
force-pushed
the
perf/value-slots
branch
from
October 19, 2026 22:20
e15c682 to
00a950f
Compare
textOf, rawOf, intOf, longOf, doubleOf, boolOf, attrOf and attrOfNullable read one value from the model without opening a dynamic() block. The preprocessor records each slot as a node in the chain it already builds, and the markup around the slot stays in the static blocks on either side. A dynamic block writes its own indentation on every render, so the static block after it is trimmed. A slot writes none, so the preprocessor keeps the whitespace after a slot.
Loops and conditionals whose bodies the preprocessor records once, each into its own sub-chain, and replays at render time. Before, the only way to loop or branch on the model was a dynamic() block, which runs the template again on every render. Recording a body saves and restores the whole recording state, so loops and conditionals nest.
A chain made only of static blocks and value slots now compiles once into a generated class, which drops the interpretation loop. Loop and conditional bodies compile the same way. The class has two methods with the same body. render appends to a StringBuilder, where numeric slots go in without boxing. write takes any other Appendable, which only accepts chars, so numbers go through String.valueOf there. Neither one buffers. The class file is written by hand, so there is no new dependency. The generated methods never branch, so they need no StackMapTable. A chain that holds any other node keeps using the linked chain.
The async chain hands off to the same sync nodes that value slots and forEachOf use, so they need no async code of their own. These tests check that: slots on both sides of an await, and a render that matches the equivalent dynamic() block byte for byte.
HtmlViewVisitorHot threw UnsupportedOperationException for every slot, forEachOf and whenOf. It has no preencoded chain, so a slot now reads the current model and writes the value at once, through the same format helper the chain uses. forEachOf and whenOf set the model for the length of the body, the way visitDynamic already does for a block.
HtmlContinuation implements the new HtmlResolver interface, so a visitor can hold either a continuation chain or a compiled chain behind one type. execute(Object) becomes resolve(Object) in every continuation and caller, including the Kotlin suspendable continuations.
ChainCompiler.compile returns an HtmlResolver: the generated class, or the linked chain itself when the chain does not compile. Continuations no longer hold a compiled field, and HtmlViewVisitor just copies whichever resolver preprocessing returned, so there is no null check. The generated class extends HtmlResolverCompiled and implements a single write(Appendable, Object). The base class wraps the IOException, since the generated method has no branches and cannot catch it. Like a continuation, each visitor gets its own copy, a shallow clone that writes to the visitor's output. Renderer and HtmlContinuationSyncCompiled are gone. Async views no longer compile their chain, which HtmlViewVisitorAsync never used.
With a single write(Appendable, Object) method, every number went through String.valueOf, which allocated a String per number before appending it. Numbers now go through small append helpers in HtmlContinuationSyncValue that call StringBuilder.append(int|long|double) when the output is a StringBuilder, and Integer/Long/Double.toString otherwise. The generated code stays branchless. On the stocks benchmark this brings the single method back to the speed of the old StringBuilder-specific render method (234.2 vs 233.1 ops/ms), and the allocation per 20-row render back to 6,192 B from 7,360 B.
Booleans and raw values get append helpers too, so the generated code handles every slot except text and nullable attributes the same way. The linked chain and the hot reload visitor now append through the same helpers (appendValue replaces format), so numbers skip the String copy there as well and each kind is formatted in one place. Also: the reflective helper check runs once instead of per compiled chain, the dead invokeVirtual opcode is gone, both HtmlFlow.view overloads share one preencoded branch for MFE and plain views, and two HtmlViewVisitor comments now describe the resolver.
Boolean.toString returns a constant and StringBuilder.append(Object) calls String.valueOf anyway, so the instanceof check only pays off for numbers.
A hot async view preprocesses on every render and runs that chain without copying it, so a compiled forEachOf or whenOf body still had no visitor and threw a NullPointerException. recordBlock now returns the body copied for the preprocessing visitor, as it did before the HtmlResolver change.
fmcarvalho
force-pushed
the
perf/value-slots
branch
from
October 22, 2026 16:28
00a950f to
1f61dd5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds value slots and preencoded control flow as a faster alternative to
dynamic(), and adds bytecode generation for simple chains.Value slots (
textOf,rawOf,intOf,longOf,doubleOf,boolOf,attrOf,attrOfNullable) let a view read a single value from the model without creating adynamic()block. The preprocessor stores each slot directly in the chain, so rendering does not need to resolve it as a dynamic block each time.The same idea is used for
forEachOfandwhenOf. Their bodies are preencoded once as separate sub-chains, then reused during rendering.forEachIndexedOfworks likeforEachOfand also gives each item its position, starting at 0, so a view can number rows or alternate row classes without building a wrapper list.For chains made up of static blocks and value slots, HtmlFlow can also generate a small class that writes directly to the output buffer. This removes the interpretation loop and avoids boxing for numeric slots. The generated bytecode is written directly, so this does not add a bytecode library dependency. If a chain cannot be compiled, it keeps using the interpreted version. Both paths produce the same output.
The changes also work with async views and hot reload.
template-benchmark when adapting the
presentationsandstocksviews to use these features (tested locally):Here is what the views look like:
Depends on
This depends on xmlet/xsd2poet#2, which adds the slot methods,
Slot<M>andforEachIndexedOfto the generated element API.For now, this branch uses
xsd2poet-java:1.0.11-SNAPSHOT, built locally.