Change proxy generation to use volatile fields rather than synchronized blocks

Details

Description

As currently coded, the service proxies used for Tapestry services use a synchronized block to a) check to see if the Registry has shut down and b) obtain (if needed) the realized service implementation (wrapped by interceptors, etc.).

It seems that with some juggling, these could largely be replaced with AtomicBoolean and AtomicReferences.

Activity

Of course, creating some benchmarks (for before and after comparisons) would be useful, to see if there's any real benefit to this. Supposedly, uncontested synchronized blocks are very cheap (and cheaper in Java 6), and access to the volatile fields inside an AtomicBoolean are going to be more expensive than ordinary field access, so there may not be any real advantage.

Howard M. Lewis Ship
added a comment - 09/May/08 20:20 Of course, creating some benchmarks (for before and after comparisons) would be useful, to see if there's any real benefit to this. Supposedly, uncontested synchronized blocks are very cheap (and cheaper in Java 6), and access to the volatile fields inside an AtomicBoolean are going to be more expensive than ordinary field access, so there may not be any real advantage.

After a brief look, using simple volatile fields seems to be the way to go, with a double-check (which is ok for volatile fields). I also found a way to eliminate the need for a volatile shutdown flag, sort of merging that back into the ObjectCreator.

Still its hard to tell if there's a performance difference especially under JDK 1.6. Supposedly uncontested synchronized locks are super cheap in JDK 1.6 and volatiles are always more expensive than normal field access.

Howard M. Lewis Ship
added a comment - 04/Mar/09 17:27 After a brief look, using simple volatile fields seems to be the way to go, with a double-check (which is ok for volatile fields). I also found a way to eliminate the need for a volatile shutdown flag, sort of merging that back into the ObjectCreator.
Still its hard to tell if there's a performance difference especially under JDK 1.6. Supposedly uncontested synchronized locks are super cheap in JDK 1.6 and volatiles are always more expensive than normal field access.