Skip to content

Commit ea72901

Browse files
committed
Performance: Cache method calls in loops and optimize string concatenation
Apply low-risk performance optimizations across multiple bundles: Loop optimizations - cache method results: - StyledString: Cache otherRuns.size() before loop - ContributionManager: Cache contributions.size() in indexOf() and duplicate removal - ContentProposalAdapter: Cache autoActivateString.length() in loop - DialogSettings: Cache s.length() before character iteration - MenuManagerShowProcessor: Cache menuModel.getChildren() to avoid repeated calls - WorkbenchPage: Cache getChildren() results to avoid expensive repeated calls String concatenation optimizations: - WizardsRegistryReader: Use StringBuilder for path construction in loop - NewContentTypeDialog: Use StringBuilder for name generation in while loop Repeated expensive calls: - BindingManager: Cache format().length() results in comparison - MenuManagerShowProcessor: Cache getChildren() in loop condition and body All changes are non-API breaking and maintain identical behavior while reducing unnecessary object allocations and method calls.
1 parent 9c171a0 commit ea72901

File tree

9 files changed

+32
-23
lines changed

9 files changed

+32
-23
lines changed

bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/MenuManagerShowProcessor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public void menuAboutToHide(IMenuManager manager) {
123123
* {@link MDynamicMenuContribution} application model elements
124124
*/
125125
private void processDynamicElements(MMenu menuModel, MenuManager menuManager) {
126-
MMenuElement[] menuElements = menuModel.getChildren().toArray(
127-
new MMenuElement[menuModel.getChildren().size()]);
126+
List<MMenuElement> children = menuModel.getChildren();
127+
MMenuElement[] menuElements = children.toArray(new MMenuElement[children.size()]);
128128
for (MMenuElement currentMenuElement : menuElements) {
129129

130130
if (currentMenuElement instanceof MDynamicMenuContribution dmc) {
@@ -154,9 +154,9 @@ private void processDynamicElements(MMenu menuModel, MenuManager menuManager) {
154154
if (mel.size() > 0) {
155155

156156
int position = 0;
157-
while (position < menuModel.getChildren().size()) {
158-
if (currentMenuElement == menuModel.getChildren().get(
159-
position)) {
157+
List<MMenuElement> currentChildren = menuModel.getChildren();
158+
while (position < currentChildren.size()) {
159+
if (currentMenuElement == currentChildren.get(position)) {
160160
position++;
161161
break;
162162
}

bundles/org.eclipse.jface/src/org/eclipse/jface/action/ContributionManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ protected boolean hasDynamicItems() {
272272
* @return <code>int</code> the index or -1 if the item is not found
273273
*/
274274
public int indexOf(String id) {
275-
for (int i = 0; i < contributions.size(); i++) {
275+
int size = contributions.size();
276+
for (int i = 0; i < size; i++) {
276277
IContributionItem item = contributions.get(i);
277278
String itemId = item.getId();
278279
if (itemId != null && itemId.equalsIgnoreCase(id)) {
@@ -489,7 +490,8 @@ public boolean replaceItem(final String identifier,
489490
itemAdded(replacementItem); // throws NPE if (replacementItem == null)
490491

491492
// Go through and remove duplicates.
492-
for (int i = contributions.size() - 1; i > index; i--) {
493+
int size = contributions.size();
494+
for (int i = size - 1; i > index; i--) {
493495
IContributionItem item = contributions.get(i);
494496
if ((item != null) && (identifier.equals(item.getId()))) {
495497
if (Policy.TRACE_TOOLBAR) {

bundles/org.eclipse.jface/src/org/eclipse/jface/bindings/BindingManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,8 +1339,9 @@ public TriggerSequence getBestActiveBindingFor(final ParameterizedCommand comman
13391339
}
13401340

13411341
// If this is still a tie, then just chose the shortest text.
1342-
compareTo = bestTriggerSequence.format().length()
1343-
- currentTriggerSequence.format().length();
1342+
int bestLength = bestTriggerSequence.format().length();
1343+
int currentLength = currentTriggerSequence.format().length();
1344+
compareTo = bestLength - currentLength;
13441345
if (compareTo > 0) {
13451346
bestBinding = currentBinding;
13461347
}

bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/DialogSettings.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ private static void appendEscapedChar(StringBuilder buffer, char c) {
535535

536536
private static String getEscaped(String s) {
537537
StringBuilder result = new StringBuilder(s.length() + 10);
538-
for (int i = 0; i < s.length(); ++i) {
538+
int length = s.length();
539+
for (int i = 0; i < length; ++i) {
539540
appendEscapedChar(result, s.charAt(i));
540541
}
541542
return result.toString();

bundles/org.eclipse.jface/src/org/eclipse/jface/fieldassist/ContentProposalAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,8 @@ private boolean shouldPopupRemainOpen() {
21532153
return true;
21542154
}
21552155
String content = getControlContentAdapter().getControlContents(getControl());
2156-
for (int i=0; i<autoActivateString.length(); i++) {
2156+
int length = autoActivateString.length();
2157+
for (int i=0; i<length; i++) {
21572158
if (content.indexOf(autoActivateString.charAt(i)) >= 0) {
21582159
return true;
21592160
}

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StyledString.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ public StyledString append(StyledString string) {
234234

235235
List<StyleRun> otherRuns = string.fStyleRuns;
236236
if (otherRuns != null && !otherRuns.isEmpty()) {
237-
for (int i = 0; i < otherRuns.size(); i++) {
237+
int size = otherRuns.size();
238+
for (int i = 0; i < size; i++) {
238239
StyleRun curr = otherRuns.get(i);
239240
if (i == 0 && curr.offset != 0) {
240241
appendStyleRun(null, offset); // appended string will

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchPage.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,16 +3423,18 @@ public void resetPerspective() {
34233423
// Hide placeholders for parts that exist in the 'global' areas
34243424
modelService.hideLocalPlaceholders(window, dummyPerspective);
34253425

3426-
int dCount = dummyPerspective.getChildren().size();
3427-
while (!dummyPerspective.getChildren().isEmpty()) {
3428-
MPartSashContainerElement dChild = dummyPerspective.getChildren().remove(0);
3429-
persp.getChildren().add(dChild);
3426+
List<MPartSashContainerElement> dummyChildren = dummyPerspective.getChildren();
3427+
List<MPartSashContainerElement> perspChildren = persp.getChildren();
3428+
int dCount = dummyChildren.size();
3429+
while (!dummyChildren.isEmpty()) {
3430+
MPartSashContainerElement dChild = dummyChildren.remove(0);
3431+
perspChildren.add(dChild);
34303432
}
34313433

3432-
while (persp.getChildren().size() > dCount) {
3433-
MUIElement child = persp.getChildren().get(0);
3434+
while (perspChildren.size() > dCount) {
3435+
MUIElement child = perspChildren.get(0);
34343436
child.setToBeRendered(false);
3435-
persp.getChildren().remove(0);
3437+
perspChildren.remove(0);
34363438
}
34373439

34383440
List<MWindow> existingDetachedWindows = new ArrayList<>();

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/NewContentTypeDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected NewContentTypeDialog(Shell parentShell, IContentTypeManager manager, I
5454
}
5555
int suffix = 2;
5656
while (manager.getContentType(name) != null) {
57-
name = baseName + " (" + suffix + ')'; //$NON-NLS-1$
57+
name = new StringBuilder(baseName).append(" (").append(suffix).append(')').toString(); //$NON-NLS-1$
5858
suffix++;
5959
}
6060
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/registry/WizardsRegistryReader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ private static class CategoryNode {
7676

7777
CategoryNode(Category cat) {
7878
category = cat;
79-
path = ""; //$NON-NLS-1$
8079
String[] categoryPath = category.getParentPath();
80+
StringBuilder pathBuilder = new StringBuilder();
8181
if (categoryPath != null) {
8282
for (String parentPath : categoryPath) {
83-
path += parentPath + '/';
83+
pathBuilder.append(parentPath).append('/');
8484
}
8585
}
86-
path += cat.getId();
86+
pathBuilder.append(cat.getId());
87+
path = pathBuilder.toString();
8788
}
8889

8990
String getPath() {

0 commit comments

Comments
 (0)