Skip to content

Commit

Permalink
Merge pull request #5402 from res0nance/JENKINS-65308
Browse files Browse the repository at this point in the history
[JENKINS-65308] Improve performance of Jenkins#trimLabels()
  • Loading branch information
MarkEWaite authored Apr 11, 2021
2 parents b71b295 + 7ede2f1 commit 901bf54
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
29 changes: 28 additions & 1 deletion core/src/main/java/hudson/model/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,34 @@ public boolean contains(Node node) {
* This is usually used as a signal that this label is invalid.
*/
public boolean isEmpty() {
return getNodes().isEmpty() && getClouds().isEmpty();
return !(hasAnyNodes() || hasAnyClouds());
}

private boolean hasAnyNodes() {
Set<Node> nodes = this.nodes;
if(nodes!=null) return !nodes.isEmpty();

Jenkins h = Jenkins.get();
if(this.matches(h)) return true;
for (Node n : h.getNodes()) {
if(this.matches(n))
return true;
}
return false;
}

private boolean hasAnyClouds() {
Set<Cloud> clouds = this.clouds;
if(clouds==null) {
Jenkins h = Jenkins.get();
for (Cloud c : h.clouds) {
if(c.canProvision(this))
return true;
}
return false;
} else {
return !clouds.isEmpty();
}
}

/*package*/ void reset() {
Expand Down
13 changes: 13 additions & 0 deletions test/src/test/java/hudson/model/labels/LabelAtomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public void getClouds() {
assertThat(l2.getClouds(), is(empty()));
}

@Test
public void isEmpty() throws Exception {
Label l = new LabelAtom("label");
assertThat(l.isEmpty(), is(true));
l = new LabelAtom("label");
j.createSlave("node", "label", null);
assertThat(l.isEmpty(), is(false));
Label l2 = new LabelAtom("label2");
Cloud test = new TestCloud("test", "label2");
j.jenkins.clouds.add(test);
assertThat(l2.isEmpty(), is(false));
}

private static class TestCloud extends Cloud {

private final List<Label> labels;
Expand Down

0 comments on commit 901bf54

Please sign in to comment.