aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-03-27 18:28:27 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2023-03-27 18:28:27 +0200
commit507b58ef6fd297c0a961d3b2c15ab03e0e557813 (patch)
treeb0b18baa5315732ca4152d15c681c60c3bce69a2
parent5ec4d195cd2e0215da609abaaf26d5ca9c984153 (diff)
CompoundName.first(int n) is called multiple time from QueryProfileProperties methods.
Precompute, as we do for rest(int n), these instead of generating temporary CompoundNames on the fly.
-rw-r--r--container-core/src/main/java/com/yahoo/processing/request/CompoundName.java22
-rw-r--r--container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java13
2 files changed, 25 insertions, 10 deletions
diff --git a/container-core/src/main/java/com/yahoo/processing/request/CompoundName.java b/container-core/src/main/java/com/yahoo/processing/request/CompoundName.java
index 6af4811fa1b..66750b2943d 100644
--- a/container-core/src/main/java/com/yahoo/processing/request/CompoundName.java
+++ b/container-core/src/main/java/com/yahoo/processing/request/CompoundName.java
@@ -31,6 +31,8 @@ public final class CompoundName {
/** This name with the first component removed */
private final CompoundName rest;
+ /** This name with the last component removed */
+ private final CompoundName first;
/** The empty compound */
public static final CompoundName empty = new CompoundName("");
@@ -51,7 +53,7 @@ public final class CompoundName {
/** Constructs this from a list of compounds. */
public CompoundName(List<String> compounds) {
- this(compounds.toArray(new String[compounds.size()]));
+ this(compounds.toArray(new String[0]));
}
private CompoundName(String [] compounds) {
@@ -74,13 +76,18 @@ public final class CompoundName {
this.compounds = List.of();
this.hashCode = 0;
rest = this;
+ first = this;
return;
}
this.compounds = new ImmutableArrayList(compounds);
this.hashCode = this.compounds.hashCode();
- rest = compounds.length > 1 ? new CompoundName(name.substring(compounds[0].length()+1), Arrays.copyOfRange(compounds, 1, compounds.length))
- : empty;
+ rest = (compounds.length > 1)
+ ? new CompoundName(name.substring(compounds[0].length()+1), Arrays.copyOfRange(compounds, 1, compounds.length))
+ : empty;
+ first = (compounds.length > 1)
+ ? new CompoundName(name.substring(0, name.length() - (compounds[compounds.length-1].length()+1)), Arrays.copyOfRange(compounds, 0, compounds.length-1))
+ : empty;
}
private static List<String> parse(String s) {
@@ -177,7 +184,8 @@ public final class CompoundName {
this + "' only have " + compounds.size() + " components.");
if (compounds.size() == n) return this;
if (compounds.size() == 0) return empty;
- return new CompoundName(compounds.subList(0, n));
+ if (compounds.size() - 1 == n) return first;
+ return first.first(n);
}
/**
@@ -284,11 +292,9 @@ public final class CompoundName {
private static String toCompoundString(String [] compounds) {
int all = compounds.length;
- for (int i = 0; i < compounds.length; i++)
- all += compounds[i].length();
+ for (String compound : compounds) all += compound.length();
StringBuilder b = new StringBuilder(all);
- for (int i = 0; i < compounds.length; i++)
- b.append(compounds[i]).append(".");
+ for (String compound : compounds) b.append(compound).append(".");
return b.length()==0 ? "" : b.substring(0, b.length()-1);
}
diff --git a/container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java b/container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java
index 055dbf77371..4bbece0af29 100644
--- a/container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java
+++ b/container-core/src/test/java/com/yahoo/processing/request/CompoundNameTestCase.java
@@ -52,6 +52,15 @@ public class CompoundNameTestCase {
verifyStrict("e", new CompoundName("a.b.c.d.e").rest(4));
verifyStrict(CompoundName.empty, new CompoundName("a.b.c.d.e").rest(5));
}
+ @Test
+ final void testFirstN() {
+ verifyStrict("a.b.c.d.e", new CompoundName("a.b.c.d.e").first(5));
+ verifyStrict("a.b.c.d", new CompoundName("a.b.c.d.e").first(4));
+ verifyStrict("a.b.c", new CompoundName("a.b.c.d.e").first(3));
+ verifyStrict("a.b", new CompoundName("a.b.c.d.e").first(2));
+ verifyStrict("a", new CompoundName("a.b.c.d.e").first(1));
+ verifyStrict(CompoundName.empty, new CompoundName("a.b.c.d.e").first(0));
+ }
@Test
final void testPrefix() {
@@ -103,8 +112,8 @@ public class CompoundNameTestCase {
Splitter peoplesFront = Splitter.on('.');
Iterable<String> answer = peoplesFront.split(NAME);
Iterator<String> expected = answer.iterator();
- for (int i = 0; i < l.size(); ++i) {
- assertEquals(expected.next(), l.get(i));
+ for (String s : l) {
+ assertEquals(expected.next(), s);
}
assertFalse(expected.hasNext());
}