Skip to content

Commit

Permalink
[break] Stop using Matcher.group() which allocates a string (#466)
Browse files Browse the repository at this point in the history
Parsing all types of sls version no longer involves allocating strings just to parse them as integers again.
  • Loading branch information
iamdanfox authored Jun 22, 2021
1 parent d9d00dd commit 342216b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-466.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 342216b

Please sign in to comment.