Repository navigation
Improve hot path virtual-thread and codec performance - #15828
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes two hot paths in Grails 8: (1) GrailsHttpSession synchronization to be more virtual-thread friendly, and (2) common codec extension methods to reduce Groovy dynamic-dispatch overhead while keeping the public extension surface stable.
Changes:
- Replace
synchronized (this)session wrapper serialization with a privateReentrantLockand lock-protected lazy session creation. - Add a new spec asserting lazy servlet-session creation and correct delegation for attribute operations.
- Static-compile and refactor codecs (
Hex,Base64,MD5,SHA-1,SHA-256) to use direct helper calls and shared byte-conversion logic.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| grails-web-common/src/main/groovy/grails/web/servlet/mvc/GrailsHttpSession.java | Switch wrapper synchronization to ReentrantLock and lock-protect lazy session creation/access. |
| grails-web-common/src/test/groovy/grails/web/servlet/mvc/GrailsHttpSessionSpec.groovy | Add coverage for lazy session creation + delegation for attribute operations. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/DigestUtils.groovy | Static-compile and centralize Object→byte[] conversion to support codec hot paths. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/HexCodecExtensionMethods.groovy | Static-compile + rewrite encode/decode loops for performance; preserve HEXDIGITS shape. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/Base64CodecExtensionMethods.groovy | Static-compile and route Byte[]/byte[] paths through shared byte conversion. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/MD5CodecExtensionMethods.groovy | Static-compile and replace dynamic chaining with direct helper calls. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/MD5BytesCodecExtensionMethods.groovy | Static-compile and keep digest behavior via DigestUtils. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/SHA1CodecExtensionMethods.groovy | Static-compile and replace dynamic chaining with direct helper calls. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/SHA1BytesCodecExtensionMethods.groovy | Static-compile and keep digest behavior via DigestUtils. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/SHA256CodecExtensionMethods.groovy | Static-compile and replace dynamic chaining with direct helper calls. |
| grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/SHA256BytesCodecExtensionMethods.groovy | Static-compile and keep digest behavior via DigestUtils. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The current implementation of grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/HexCodecExtensionMethods.groovy |
jdaugherty
left a comment
There was a problem hiding this comment.
I don't see anything major with this at first glance. I asked Fabled to take a look and the results are attached to the review.
The GrailsHttpSession lock conversion looks correct — the remaining synchronized (this) blocks are all in commented-out dead code, lazy session creation now happens under the same lock as access (an improvement over the old racy pattern), and the new spec covers the lazy-create/delegate path. The codec static compilation is a nice win too; my comments are mostly about a few places where the rewrite narrows the accepted input types compared to the old dynamic coercion, plus some test-convention and hot-loop nits.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #15828 +/- ##
==================================================
+ Coverage 57.3794% 57.4112% +0.0318%
- Complexity 22498 22539 +41
==================================================
Files 2129 2129
Lines 103776 103816 +40
Branches 18625 18638 +13
==================================================
+ Hits 59546 59602 +56
+ Misses 35967 35954 -13
+ Partials 8263 8260 -3
🚀 New features to boost your workflow:
|
|
Thanks for the detailed pass (and the Fabled review). These read as review-then-commit nits rather than blockers, and they're all reasonable - I'll take them:
I'll push these fixes. On the |
This comment has been minimized.
This comment has been minimized.
Use a ReentrantLock inside GrailsHttpSession so wrapper-level session access no longer pins virtual-thread carrier threads on the object monitor. Static-compile the codecs-core extension helpers while preserving existing Groovy extension method descriptors. Assisted-by: Hephaestus:openai/gpt-5.5 codex-review
Preserve the pre-existing Groovy truth guard under static compilation by using Groovy's runtime truth conversion helper. Add Hex codec regression coverage for falsy inputs. Assisted-by: Hephaestus:openai/gpt-5.5 codex-review
Fix Base64 charset handling, empty hex decoding, preserve DigestUtils visibility, and document codec notes. Assisted-by: Sisyphus:xai/grok-4.5 [gpt-coding]
Completes the review items accepted on the codec/virtual-thread pass and documents the behavior changes that shipped with it. - Drop the charset parameter from decodeBase64. It was declared but never read, so the overload silently ignored its argument. Base64 text is always ASCII, so the decode side has nothing to apply a charset to. - Mark DigestUtils.toByteArray @PackageScope instead of protected, so the package-access intent is explicit rather than a side effect of protected on a public abstract class. - Document that an empty CharSequence decodes to an empty byte array while other Groovy-falsy values decode to null, and cover the Grails code path with StringBuilder and GString receivers. A String receiver binds to Groovy's own String.decodeHex(), so the previous '' assertion never exercised this class. - Document that the hexadecimal digest codecs no longer route through the metaclass, so replacing encodeAsMD5Bytes and friends at runtime does not change what they return. - Cover the Iterator and primitive-array branches of toByteArray.
ad5d5c5 to
1cce663
Compare
Summary
This PR makes two Grails 8 hot paths friendlier to the JDK 21 runtime without changing the public API surface:
GrailsHttpSessionwrapper monitor synchronization with a privateReentrantLock.grails-codecs-corecodec extension helpers and removes dynamic self-dispatch inside the codec implementations.Objectreturn types and the existing mutableHEXDIGITSproperty shape.Motivation
Virtual-thread friendliness
GrailsHttpSessionpreviously serialized wrapper access withsynchronized (this). That object monitor can pin a virtual-thread carrier if the servlet container or request/session access blocks while the monitor is held. A privateReentrantLockpreserves the wrapper-level serialization semantics while avoiding monitor pinning, and it also makes lazy session creation occur under the same lock as subsequent session access.Codec hot-path static compilation
The codec extension methods are small, frequently used helpers. Static compilation removes runtime Groovy dispatch inside MD5, SHA-1, SHA-256, Base64, Hex, and shared digest conversion paths. The implementation still keeps the extension methods compatible for existing compiled callers by preserving the original public method descriptors.
Compatibility notes
decodeHexkeeps the previous Groovy truth behavior for falsy targets by using Groovy's runtime truth conversion helper under@CompileStatic.String, primitive byte arrays, wrapper byte arrays, number lists, and null/NullObjectcases remain covered.Review feedback addressed
decodeHexGroovy truth guard behavior for falsy inputs after Copilot review feedback.Verification
./gradlew :grails-codecs-core:test --tests "org.grails.web.codecs.HexCodecTests" :grails-codecs-core:codeStyle --rerun-tasks./gradlew :grails-codecs-core:test :grails-codecs-core:codeStyle --rerun-tasks./gradlew :grails-codecs-core:test :grails-web-common:test --tests "grails.web.servlet.mvc.GrailsHttpSessionSpec" :grails-codecs-core:codeStyle :grails-web-common:codeStyle --rerun-tasks./gradlew clean aggregateViolations :grails-test-report:check --continueorigin/8.0.x:grails-validation:compileGroovy,grails-data-graphql-core:compileGroovy, and MongoDB/Testcontainers environment failures. The two compile failures reproduce in a separate cleanorigin/8.0.xworktree.Review gate
8102545e609fd576dfc616e73c59e4c7da847dbfwith both Oracle and Codex reviewers green.fcb2c9b25edffd17c4910e79aa23047e3169e608with both Oracle and Codex reviewers green.