Skip to content

Fix Unsafe string paths when -XX:-CompactStrings is used#1

Merged
merlimat merged 1 commit intomasterfrom
fix-compact-strings
Mar 17, 2026
Merged

Fix Unsafe string paths when -XX:-CompactStrings is used#1
merlimat merged 1 commit intomasterfrom
fix-compact-strings

Conversation

@merlimat
Copy link
Collaborator

Summary

  • Detect at class init whether JDK compact strings are enabled and guard all Unsafe string fast paths (writeString, writeRawString, readString) so they fall back to safe standard library methods when compact strings are disabled (-XX:-CompactStrings)
  • Add a surefire execution that re-runs all tests with -XX:-CompactStrings to prevent regressions

Context

Reported by @lhotari in apache/pulsar#25332 — the optimized String handling assumes compact strings (LATIN1 coder) are always enabled, which breaks when -XX:-CompactStrings is used and the internal String.value byte array uses UTF-16 encoding.

Test plan

  • All 189 existing tests pass with default JVM settings
  • All 189 tests pass when re-run with -XX:-CompactStrings

Detect at class init whether JDK compact strings are enabled by checking
the internal byte[] length of a known ASCII string. Guard all Unsafe
string fast paths (writeString, writeRawString, readString) with this
flag so they fall back to safe standard library methods when compact
strings are disabled and the internal representation is UTF-16.

Add a surefire execution that re-runs all tests with -XX:-CompactStrings.
@merlimat merlimat merged commit 52c13ac into master Mar 17, 2026
1 check passed
@merlimat merlimat deleted the fix-compact-strings branch March 17, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants