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

Optimize Label parsing calls #8395

Merged
merged 8 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions core/src/main/java/hudson/model/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,16 @@ public static Set<LabelAtom> parse(@CheckForNull String labels) {
final Set<LabelAtom> r = new TreeSet<>();
labels = fixNull(labels);
if (labels.length() > 0) {
final QuotedStringTokenizer tokenizer = new QuotedStringTokenizer(labels);
while (tokenizer.hasMoreTokens())
r.add(Jenkins.get().getLabelAtom(tokenizer.nextToken()));
Jenkins j = Jenkins.get();
LabelAtom labelAtom = j.tryGetLabelAtom(labels);
if (labelAtom == null) {
final QuotedStringTokenizer tokenizer = new QuotedStringTokenizer(labels);
while (tokenizer.hasMoreTokens())
r.add(j.getLabelAtom(tokenizer.nextToken()));
} else {
r.add(labelAtom);
}
}
return r;
}

Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,16 @@ public Label getLabel(String expr) {
}
}

/**
* Returns the label atom of the given name, only if it already exists.
* @return non-null if the label atom already exists.
*/
public @Nullable LabelAtom tryGetLabelAtom(@CheckForNull String name) {
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
if (name == null) return null;
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
return (LabelAtom) labels.get(name);
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
}


/**
* Gets all the active labels in the current system.
*/
Expand Down
23 changes: 23 additions & 0 deletions test/src/test/java/hudson/model/labels/LabelBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hudson.model.labels;

import hudson.model.Label;
import jenkins.benchmark.jmh.JmhBenchmark;
import jenkins.benchmark.jmh.JmhBenchmarkState;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;

@JmhBenchmark
public class LabelBenchmark {
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
public static class MyState extends JmhBenchmarkState {
}

@Benchmark
public void simpleLabel(MyState state, Blackhole blackhole) {
blackhole.consume(Label.parse("some-label"));
}

@Benchmark
public void complexLabel(MyState state, Blackhole blackhole) {
blackhole.consume(Label.parse("label1 && label2"));
}
}
38 changes: 38 additions & 0 deletions test/src/test/java/hudson/model/labels/LabelBenchmarkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hudson.model.labels;

import static org.junit.Assert.assertTrue;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.junit.Ignore;
import org.junit.Test;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.openjdk.jmh.runner.options.OptionsBuilder;

public class LabelBenchmarkTest {
@Test
@Ignore(value="This is a benchmark, not a test")
public void runBenchmark() throws Exception {
// run the minimum possible number of iterations
ChainedOptionsBuilder options = new OptionsBuilder()
.mode(Mode.AverageTime)
.forks(1)
.result("jmh-report.json")
.resultFormat(ResultFormatType.JSON)
.operationsPerInvocation(1)
.threads(1)
.warmupForks(0)
.warmupIterations(0)
.measurementBatchSize(1)
.measurementIterations(1)
.timeUnit(TimeUnit.NANOSECONDS)
.shouldFailOnError(true)
.include(LabelBenchmark.class.getName() + ".*");
new Runner(options.build()).run();
assertTrue(Files.exists(Paths.get("jmh-report.json")));
}
}