[MDSAL-498] Use VarHandles to manage mdsal-binding-dom-codec proxy state Created: 11/Dec/19  Updated: 11/Dec/19  Resolved: 11/Dec/19

Status: Resolved
Project: mdsal
Component/s: Binding runtime
Affects Version/s: None
Fix Version/s: 5.0.6

Type: Improvement Priority: Medium
Reporter: Robert Varga Assignee: Robert Varga
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

mdsal-binding-dom codec uses volatile fields in a number of places to implement thread-safe lazily-populated cache:

  1. CodecDataObject.cachedHashcode (double-checked locking)
  2. DataContainerCodecContext.eventStreamSerializer (concurrently-loaded cache)
  3. DataContainerCodecPrototype.instance (double-checked locking)

Accessing these fields causes synchronization actions and thus is subject to global synchronization order (https://docs.oracle.com/javase/specs/jls/se11/html/jls-17.html#jls-17.4.4) . This is strictly not necessary, as the objects being held by the field are immutable (#1), stateless (#2) or effectively-immutable (#3, with a documented quirk).

Of these, #3 is by far the most interesting, as it is accessed each time the corresponding CodecDataObject's property is filled – forcing these fills to occur  in program order. Each such property is effectively immutable, hence this constraint of execution is not really needed.

Java 9+ allows us to improve the situation here by loosening synchronization to acquire/release on the field. This retains the basic semantics (fill-once) while allowing the runtime to better reorder accesses.


Generated at Wed Feb 07 20:09:57 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.