From 342216bb71a118107034ea774af24d46487a99d0 Mon Sep 17 00:00:00 2001 From: iamdanfox Date: Tue, 22 Jun 2021 15:27:33 +0100 Subject: [PATCH] [break] Stop using Matcher.group() which allocates a string (#466) Parsing all types of sls version no longer involves allocating strings just to parse them as integers again. --- build.gradle | 2 +- changelog/@unreleased/pr-466.v2.yml | 6 ++++++ .../java/com/palantir/sls/versions/MatchResult.java | 13 +++++++++++-- .../java/com/palantir/sls/versions/RegexParser.java | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 changelog/@unreleased/pr-466.v2.yml diff --git a/build.gradle b/build.gradle index c1bda35a..3e316249 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ subprojects { apply plugin: 'org.inferred.processors' tasks.check.dependsOn(javadoc) - sourceCompatibility = 1.8 + sourceCompatibility = 11 tasks.withType(JavaCompile) { options.compilerArgs += ['-Werror'] diff --git a/changelog/@unreleased/pr-466.v2.yml b/changelog/@unreleased/pr-466.v2.yml new file mode 100644 index 00000000..47d7719a --- /dev/null +++ b/changelog/@unreleased/pr-466.v2.yml @@ -0,0 +1,6 @@ +type: break +break: + description: This library now requires Java 11+ (previously it could run on Java + 8+). This allows us to eliminate some String allocations from parsing SLS versions. + links: + - https://github.com/palantir/sls-version-java/pull/466 diff --git a/sls-versions/src/main/java/com/palantir/sls/versions/MatchResult.java b/sls-versions/src/main/java/com/palantir/sls/versions/MatchResult.java index 66e2afe7..6ab0cc06 100644 --- a/sls-versions/src/main/java/com/palantir/sls/versions/MatchResult.java +++ b/sls-versions/src/main/java/com/palantir/sls/versions/MatchResult.java @@ -26,15 +26,24 @@ interface MatchResult { int groupCount(); final class RegexMatchResult implements MatchResult { + private static final int RADIX = 10; + private final String string; private final Matcher matcher; - RegexMatchResult(Matcher matcher) { + RegexMatchResult(String string, Matcher matcher) { + this.string = string; this.matcher = matcher; } @Override public int groupAsInt(int group) { - return Integer.parseInt(matcher.group(group)); + int groupStart = matcher.start(group); + int groupEnd = matcher.end(group); + if (groupStart == -1) { + throw new NumberFormatException(); + } + + return Integer.parseUnsignedInt(string, groupStart, groupEnd, RADIX); } @Override diff --git a/sls-versions/src/main/java/com/palantir/sls/versions/RegexParser.java b/sls-versions/src/main/java/com/palantir/sls/versions/RegexParser.java index e9df1324..d1912a3c 100644 --- a/sls-versions/src/main/java/com/palantir/sls/versions/RegexParser.java +++ b/sls-versions/src/main/java/com/palantir/sls/versions/RegexParser.java @@ -43,7 +43,7 @@ static RegexParser of(String regex) { @Nullable public MatchResult tryParse(String string) { Matcher matcher = pattern.matcher(string); - return matcher.matches() ? new MatchResult.RegexMatchResult(matcher) : null; + return matcher.matches() ? new MatchResult.RegexMatchResult(string, matcher) : null; } @Override