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

Reduce to 0 the required StringBuilder/String allocations on PropertiesUtil::isPropertyInRoot #35853

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,37 @@ public boolean nextSegmentEquals(String other, int offs, int len) {
}
}

public boolean moveBothToNextSegmentIfEquals(NameIterator other) {
if (other == this) {
if (!hasNext()) {
throw new NoSuchElementException();
}
next();
return true;
}
int cookie = initIteration();
int otherCookie = other.initIteration();
for (;;) {
cookie = nextPos(cookie);
otherCookie = other.nextPos(otherCookie);
if (isSegmentDelimiter(cookie)) {
if (!other.isSegmentDelimiter(otherCookie)) {
return false;
}
// move both to next
pos = getPosition(cookie);
other.pos = other.getPosition(otherCookie);
return true;
}
if (other.isSegmentDelimiter(otherCookie)) {
return false;
}
if (charAt(cookie) != other.charAt(otherCookie)) {
return false;
}
}
}

public String getNextSegment() {
final StringBuilder b = new StringBuilder();
int cookie = initIteration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@ public static boolean isPropertyInRoot(Set<String> roots, NameIterator propertyN
final NameIterator rootNi = new NameIterator(root);
// compare segments
while (rootNi.hasNext()) {
String segment = rootNi.getNextSegment();
if (!propertyName.hasNext()) {
propertyName.goToStart();
break;
}

final String nextSegment = propertyName.getNextSegment();
if (!segment.equals(nextSegment)) {
if (!propertyName.moveBothToNextSegmentIfEquals(rootNi)) {
propertyName.goToStart();
break;
}

rootNi.next();
propertyName.next();

// root has no more segments, and we reached this far so everything matched.
// on top, property still has more segments to do the mapping.
if (!rootNi.hasNext() && propertyName.hasNext()) {
Expand Down