Skip to content

Commit adee9d9

Browse files
committed
perf: replace synchronized Stack class with a non-synchronized Deque
1 parent 20f2501 commit adee9d9

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/formatting/ExtendedFormattingConfigBasedStream.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
import java.lang.reflect.Method;
1515
import java.lang.reflect.ParameterizedType;
1616
import java.lang.reflect.Type;
17+
import java.util.ArrayDeque;
1718
import java.util.Collection;
1819
import java.util.Collections;
1920
import java.util.Comparator;
21+
import java.util.Deque;
2022
import java.util.HashMap;
2123
import java.util.HashSet;
24+
import java.util.LinkedList;
2225
import java.util.List;
2326
import java.util.Map;
2427
import java.util.Set;
25-
import java.util.Stack;
2628

2729
import org.apache.logging.log4j.LogManager;
2830
import org.apache.logging.log4j.Logger;
@@ -70,8 +72,8 @@
7072
public class ExtendedFormattingConfigBasedStream extends FormattingConfigBasedStream implements IDelegatingTokenStream {
7173

7274
private static final Logger LOGGER = LogManager.getLogger(ExtendedFormattingConfigBasedStream.class);
73-
private final Stack<Integer> columnIndents = new Stack<Integer>(); // NOPMD LooseCoupling, ReplaceVectorWithList
74-
private final Stack<Integer> initialIndents = new Stack<Integer>(); // NOPMD LooseCoupling, ReplaceVectorWithList
75+
private final LinkedList<Integer> columnIndents = new LinkedList<Integer>(); // NOPMD LooseCoupling
76+
private final Deque<Integer> initialIndents = new ArrayDeque<Integer>();
7577
private final Set<NoFormatLocator> noFormatLocators = new HashSet<NoFormatLocator>();
7678

7779
private INode currentNode;
@@ -197,25 +199,13 @@ public LineEntry createLineEntry(final EObject grammarElement, final String valu
197199
* @return
198200
* top element or 0 if stack empty
199201
*/
200-
private int emptySafeStackPeek(final Stack<Integer> stack) { // NOPMD LooseCoupling
201-
if (!stack.empty()) {
202-
return stack.peek();
202+
private int emptySafeStackPeek(final Deque<Integer> stack) {
203+
if (!stack.isEmpty()) {
204+
return stack.peekFirst();
203205
}
204206
return 0;
205207
}
206208

207-
/**
208-
* Popping an element from a stack of integers. Nothing happens if the stack is empty..
209-
*
210-
* @param stack
211-
* stack of integers
212-
*/
213-
private void emptySafeStackPop(final Stack<Integer> stack) { // NOPMD LooseCoupling
214-
if (!stack.empty()) {
215-
stack.pop();
216-
}
217-
}
218-
219209
@Override
220210
public Line createLine() {
221211
return new ExtendedLine(this);
@@ -517,8 +507,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
517507
boolean isSameIndentColumnLocator = candidateLocator instanceof FixedLocator
518508
&& ((FixedLocator) candidateLocator).getColumn() == ((FixedLocator) locator).getColumn();
519509
if (isSameIndentColumnLocator) {
520-
boolean isOppositeToAfter = ((FixedLocator) candidateLocator).getRight() != null && ((FixedLocator) locator).getRight() == null;
521-
boolean isOppositeToBefore = ((FixedLocator) candidateLocator).getLeft() != null && ((FixedLocator) locator).getLeft() == null;
510+
boolean isOppositeToAfter = candidateLocator.getRight() != null && locator.getRight() == null;
511+
boolean isOppositeToBefore = candidateLocator.getLeft() != null && locator.getLeft() == null;
522512
if (isOppositeToAfter || isOppositeToBefore) {
523513
return true;
524514
}
@@ -533,8 +523,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
533523
private void processColumnLocatorsBeforeComments() {
534524
// workaround: xtext implementation assumes that when the comment occurs other locators (just before the comment) are not caught
535525
if (last instanceof AbstractElement) {
536-
emptySafeStackPop(columnIndents);
537-
emptySafeStackPop(initialIndents);
526+
columnIndents.pollFirst();
527+
initialIndents.pollFirst();
538528
}
539529
}
540530

@@ -573,20 +563,21 @@ public int compare(final ElementLocator o1, final ElementLocator o2) {
573563
private void processColumnLocators(final List<ElementLocator> locators) {
574564
Set<Integer> duplicatedColumnLocatorsIndicator = new HashSet<Integer>();
575565
for (ElementLocator locator : locators) {
576-
if (locator instanceof FixedLocator) {
577-
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(((FixedLocator) locator).getColumn())) {
578-
initialIndents.push(indentationLevel);
579-
columnIndents.push(((FixedLocator) locator).getColumn());
580-
duplicatedColumnLocatorsIndicator.add(((FixedLocator) locator).getColumn());
566+
if (locator instanceof FixedLocator fixedLocator) {
567+
Integer column = fixedLocator.getColumn();
568+
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(column)) {
569+
initialIndents.addFirst(indentationLevel);
570+
columnIndents.addFirst(column);
571+
duplicatedColumnLocatorsIndicator.add(column);
581572
}
582573
if (locator.getLeft() != null) {
583-
int locatorIndexOnStack = columnIndents.lastIndexOf(((FixedLocator) locator).getColumn());
574+
int locatorIndexOnStack = columnIndents.lastIndexOf(column);
584575
if (locatorIndexOnStack != -1) {
585576
columnIndents.remove(locatorIndexOnStack);
586577
initialIndents.remove(locatorIndexOnStack);
587578
} else {
588-
emptySafeStackPop(columnIndents);
589-
emptySafeStackPop(initialIndents);
579+
columnIndents.pollFirst();
580+
initialIndents.pollFirst();
590581
}
591582
}
592583
}

0 commit comments

Comments
 (0)