dashorst opened a new issue, #1939:
URL: https://github.com/apache/wicket/issues/1939

   ## Problem
   
   Wicket's change signal is cooperative. `Component.addStateChange()` calls 
`Page.componentStateChanging()`, which calls `Page.dirty()`. A direct mutation 
of a model object (`person.setName(..)`) bypasses it. A developer who changes 
state in an Ajax listener and forgets to add the affected component to the 
`AjaxRequestTarget` gets no warning; the browser just shows stale markup.
   
   The spike asked two questions:
   
   1. Can Wicket detect, per component and without developer cooperation, whose 
serialized state changed during a request, cheaply enough to run on every Ajax 
request in development mode?
   2. What would a delta page store save, one that stores only the components 
that changed between versions?
   
   ## Spiked solution
   
   Branch: https://github.com/dashorst/wicket/tree/spike/state-delta
   
   - 
https://github.com/dashorst/wicket/commit/19dc096881f2c83cc6bfbcf530dca178d7a41459
 Spike: hash the serialized state of each component separately
   - 
https://github.com/dashorst/wicket/commit/653dcf81fbbe1b6c55f18c163767aec3dd5d7e7a
 Spike: measure the state change check on the Ajax examples
   
   `ComponentStateHasher` walks the page once and writes each component's own 
serializable fields through a subclass of `ObjectOutputStream`. The stream only 
digests (CRC32C) and counts bytes; it keeps none of them.
   
   - The parent is skipped and children count as their ids.
   - `Component#data` is unpacked into model, behaviors and meta data entries, 
each digested separately.
   - Any other component of the page the walk reaches is replaced in 
`replaceObject` by a path token and recorded as a dependency, classified as a 
lambda capture, an enclosing instance (`this$0`) or another reference.
   - The handle table is reset per field (or per component), so an object 
shared by two components counts toward both.
   
   This cannot be a by-product of the normal page serialization. In one 
continuous stream a shared object is written once and back-referenced 
afterwards, and a child is written nested inside its parent.
   
   `StateChangeCheck` is the development-mode check, opt-in and refused outside 
DEVELOPMENT. It takes the first snapshot in 
`IRequestCycleListener.onRequestHandlerResolved` for the 
`ListenerRequestHandler`, before the listener runs. It takes the second in 
`AjaxRequestTarget.IListener.onBeforeRespond`, after the listener and before 
the components are rendered. It then reports components whose state changed but 
that were neither added to the target (themselves or through an ancestor) nor 
submitted their own input.
   
   The stored page is not a usable "before" state. The first-level cache holds 
the same live instance the listener mutates, and the persistent copy would have 
to be deserialized.
   
   Tests are in `wicket-core-tests` and the accuracy harness is in 
`wicket-examples`. A JMH benchmark is in `wicket-benchmarks`.
   
   ## Results
   
   **Cost.** One snapshot costs 2–5× a page serialization, and the check takes 
two snapshots per Ajax request. JMH, JDK 21, µs/op:
   
   | page | components | `JavaSerializer` | snapshot |
   |---|---|---|---|
   | simple, 50 rows | 103 | 36 | 83 (2.3×) |
   | form, 50 rows | 204 | 125 | 497 (4.0×) |
   | simple, 500 rows | 1,003 | 329 | 843 (2.6×) |
   | form, 500 rows | 2,004 | 1,130 | 5,160 (4.6×) |
   
   At 2,000 form components that is about 10 ms and 6.7 MB allocated per Ajax 
request. Shared objects such as the `SerializedLambda` of a method reference 
get re-serialized in every component that reaches them, which is where most of 
the cost goes.
   
   **Noise.** Some state changes on every request without meaning anything, and 
has to be excluded:
   
   - `Page#renderCount`, `#numericId` and `#autoIndex`
   - the page's dirty flags
   - `Form#multiPart`, which changes on every Ajax request touching a form
   - the form's submitted flag
   - `FormComponent#rawInput`
   - feedback meta data
   - values the user just typed, which the browser already shows
   
   Repeater rebuilds happen during render, after the comparison.
   
   **Accuracy** on 16 Ajax example pages, 124 checked requests. Ground truth 
was a full render compared against a model of the browser's document that only 
Ajax responses update.
   
   - The examples add what they change. Using own-state changes only, the check 
still flagged 47 component markups that had not changed, and missed one that 
had. 32 of those 47 come from two RatingsPage components that hold a model 
their markup does not render.
   - To measure detection, each request was replayed with one added component 
left out of the response. Using own state only, the check caught 87 of the 102 
omissions that leave stale markup. Following references to other components 
caught 95, and following every dependency caught 97.
   - Following every dependency raises the false positives from 47 to 316. 
Lambdas and anonymous classes capture the page, so any change to a page field 
marks every component that captures it. Of 916 components on the example pages, 
106 depend on the page itself.
   
   **What it misses.** Five omissions stayed missed even with every dependency 
followed: three feedback panels and one list kept in a static, outside the 
page. The check cannot see:
   
   - state outside the page: a database behind a `LoadableDetachableModel`, the 
session, statics
   - time
   - feedback messages
   - markup derived from another component's validity, such as a form label 
that gets an `error` class
   
   **Versions.**
   
   - Ajax requests never create a page version: `WebPage.dirty()` returns early 
for Ajax. Of the 124 Ajax requests, 105 changed state and all 124 kept their 
page id.
   - On non-Ajax requests, 17 of 40 new page versions carried no state change. 
Most come from application code calling `addStateChange()` unconditionally; 
three come from feedback messages alone.
   - 11 non-Ajax requests changed state through direct mutation without 
creating a new version.
   
   **Delta storage**, measured only, over 120 consecutive versions of the 
example pages:
   
   - 89% of the per-component bytes sit in components that did not change.
   - The changed components average 15% of a full page serialization, provided 
class descriptors are shared across chunks. With descriptors repeated in every 
chunk, the chunks together are 2.3× the full page.
   - Reassembly would have to preserve object identity across chunks: write 
shared objects once, give them stable ids across versions, and resolve the 
cyclic component references with placeholders and a patching pass.
   
   **Unmeasured risk.** The second snapshot serializes the page while models 
are still attached. With JPA entities behind models, that can trigger lazy 
loading, a `LazyInitializationException` or a `NotSerializableException` that 
normal end-of-request serialization never sees. The examples have no ORM, so 
this was not measured.
   
   ## Conclusion
   
   Not pursued. The gain does not outweigh the complexity: Java serialization 
run twice per Ajax request, reflection into Wicket internals, and noise 
exclusions tied to flag bits. On top of that, lambdas and anonymous classes 
capturing the page make dependency following produce too many false positives, 
and without it a whole class of omissions goes unnoticed.
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to