Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inefficient getDuration() parsing - Re-creation of Pattern #770

Open
baltiyskiy opened this issue Jun 14, 2022 · 0 comments
Open

Inefficient getDuration() parsing - Re-creation of Pattern #770

baltiyskiy opened this issue Jun 14, 2022 · 0 comments

Comments

@baltiyskiy
Copy link

baltiyskiy commented Jun 14, 2022

We've noticed when we are calling to Config.getDuration() often enough, it generates a lot of garbage. Allocation profiling shows the following picture:

void java.util.regex.Pattern$BitClass.<init>()	24139	97.3 %
Pattern$CharPredicate java.util.regex.Pattern.clazz(boolean)	24139	97.3 %
Pattern$Node java.util.regex.Pattern.sequence(Pattern$Node)	24139	97.3 %
Pattern$Node java.util.regex.Pattern.expr(Pattern$Node)	24139	97.3 %
void java.util.regex.Pattern.compile()	24139	97.3 %
void java.util.regex.Pattern.<init>(String, int)	24139	97.3 %
Pattern java.util.regex.Pattern.compile(String)	24139	97.3 %
boolean java.util.regex.Pattern.matches(String, CharSequence)	24139	97.3 %
boolean java.lang.String.matches(String)	24139	97.3 %
long com.typesafe.config.impl.SimpleConfig.parseDuration(String, ConfigOrigin, String)	24139	97.3 %
Duration com.typesafe.config.impl.SimpleConfig.getDuration(String)	24139	97.3 %

Also

void java.util.ArrayList.<init>(int)	2770	3.97 %
void java.util.regex.Pattern.compile()	2084	2.99 %
void java.util.regex.Pattern.<init>(String, int)	2084	2.99 %
Pattern java.util.regex.Pattern.compile(String)	2084	2.99 %
boolean java.util.regex.Pattern.matches(String, CharSequence)	2084	2.99 %
boolean java.lang.String.matches(String)	2084	2.99 %
long com.typesafe.config.impl.SimpleConfig.parseDuration(String, ConfigOrigin, String)	2084	2.99 %
Duration com.typesafe.config.impl.SimpleConfig.getDuration(String)	2084	2.99 %

Basically, SimpleConfig.getDuration() uses String.matches() API (https://github.com/lightbend/config/blob/main/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java#L781), which, internally, creates a new Pattern object just to throw it away.
A simple fix is to hoist Pattern object as a static field and use it to perform the matching.

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

No branches or pull requests

1 participant