[External] : [RFR] C2 optimization for String.format()

wenshao shaojin.wensj at alibaba-inc.com
Wed Feb 25 12:45:20 UTC 2026


Hi,
I'd like to request a review for a C2 optimization for String.format()
and String.formatted() with simple format strings:
https://github.com/openjdk/jdk/pull/29915 <https://github.com/openjdk/jdk/pull/29915 >
Summary
-------
When String.format() or formatted() is called with a compile-time constant format string containing simple specifiers (%s, %d, %x, %X), this optimization
bypasses the java.util.Formatter overhead and directly builds the result string.
The C2 compiler recognizes these calls during the Parse phase and redirects them to specialized fast-path methods in java.lang.String.
Performance
-----------
Benchmarks show 2.6x - 4.3x speedup for common patterns:
 "Hello %s".formatted(name) -> 3.75x faster
 "%s: %d".formatted(key, value) -> 2.62x faster
 "%s %s %s %s".formatted(...) -> 2.68x faster
Fallback
--------
The optimization only applies to simple cases. Complex format strings (width > 9, precision, most flags, non-constant format) fall back to the existing Formatter implementation.
Testing
-------
- JMH microbenchmarks verify performance improvement
- jtreg functional tests cover optimized and fallback paths
- IR tests verify correct C2 code generation
I would appreciate any feedback on the approach or implementation.
Thanks,
Wenshao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20260225/9513252d/attachment.htm>


More information about the core-libs-dev mailing list