[
https://issues.apache.org/jira/browse/HDDS-13516?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18022364#comment-18022364
]
Ivan Andika commented on HDDS-13516:
------------------------------------
> From the medium you shared, looks like *replace* actually gives better
> performance than pre-compile.
>From what I saw briefly, String#replace only performs better than
>String#replaceAll for Java version higher than 8 since String#replace still
>compiles the regex for Java version 8 and below.
> Which way do you like? pre-compile or using *replace* instead.
We need to some microbenchmarks (e.g. using JMH) on the performance of these
strings (both for JDK 8 and 11) - This is the tedious, but necessary part.
* String#replaceAll
* String#replace
* StringUtils#replace (this doesn't use regex)
* Our own implementation (e.g. in our internal StringUtils): Precompile the
pattern and use that to replace it.
Based on that, we can understand the performance difference and we can pick the
ones with better performance, but doesn't sacrifice a lot of usability. If the
performance difference is not meaningful or if the performance is worse (for
some reason), then it's not worth it.
> Compile regex in StringToSignProducer#urlEncode once
> ----------------------------------------------------
>
> Key: HDDS-13516
> URL: https://issues.apache.org/jira/browse/HDDS-13516
> Project: Apache Ozone
> Issue Type: Improvement
> Reporter: Ivan Andika
> Assignee: Bolin Lin
> Priority: Minor
> Attachments: image-2025-07-28-22-19-44-883.png
>
>
> StringToSignProducer#urlEncode uses Strings#replaceAll that will call
> Pattern#compile for every invocation. This adds some memory overhead.
> !image-2025-07-28-22-19-44-883.png|width=1109,height=169!
> This can be improved by compiling the regex once. Please also add a simple
> microbenchmark (before and after the optimization) to prove that performance
> is indeed improved.
> See:
> https://medium.com/javarevisited/micro-optimizations-in-java-string-replaceall-c6d0edf2ef6
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]