Skip to content

Commit f9d14d6

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

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 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,8 +199,8 @@ 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+
private int emptySafeStackPeek(final Deque<Integer> stack) {
203+
if (!stack.isEmpty()) {
202204
return stack.peek();
203205
}
204206
return 0;
@@ -210,9 +212,9 @@ private int emptySafeStackPeek(final Stack<Integer> stack) { // NOPMD LooseCoupl
210212
* @param stack
211213
* stack of integers
212214
*/
213-
private void emptySafeStackPop(final Stack<Integer> stack) { // NOPMD LooseCoupling
214-
if (!stack.empty()) {
215-
stack.pop();
215+
private void emptySafeStackPop(final Deque<Integer> stack) {
216+
if (!stack.isEmpty()) {
217+
stack.pollFirst()
216218
}
217219
}
218220

@@ -517,8 +519,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
517519
boolean isSameIndentColumnLocator = candidateLocator instanceof FixedLocator
518520
&& ((FixedLocator) candidateLocator).getColumn() == ((FixedLocator) locator).getColumn();
519521
if (isSameIndentColumnLocator) {
520-
boolean isOppositeToAfter = ((FixedLocator) candidateLocator).getRight() != null && ((FixedLocator) locator).getRight() == null;
521-
boolean isOppositeToBefore = ((FixedLocator) candidateLocator).getLeft() != null && ((FixedLocator) locator).getLeft() == null;
522+
boolean isOppositeToAfter = candidateLocator.getRight() != null && locator.getRight() == null;
523+
boolean isOppositeToBefore = candidateLocator.getLeft() != null && locator.getLeft() == null;
522524
if (isOppositeToAfter || isOppositeToBefore) {
523525
return true;
524526
}
@@ -573,14 +575,15 @@ public int compare(final ElementLocator o1, final ElementLocator o2) {
573575
private void processColumnLocators(final List<ElementLocator> locators) {
574576
Set<Integer> duplicatedColumnLocatorsIndicator = new HashSet<Integer>();
575577
for (ElementLocator locator : locators) {
576-
if (locator instanceof FixedLocator) {
577-
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(((FixedLocator) locator).getColumn())) {
578+
if (locator instanceof FixedLocator fixedLocator) {
579+
Integer column = fixedLocator.getColumn();
580+
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(column)) {
578581
initialIndents.push(indentationLevel);
579-
columnIndents.push(((FixedLocator) locator).getColumn());
580-
duplicatedColumnLocatorsIndicator.add(((FixedLocator) locator).getColumn());
582+
columnIndents.push(column);
583+
duplicatedColumnLocatorsIndicator.add(column);
581584
}
582585
if (locator.getLeft() != null) {
583-
int locatorIndexOnStack = columnIndents.lastIndexOf(((FixedLocator) locator).getColumn());
586+
int locatorIndexOnStack = columnIndents.lastIndexOf(column);
584587
if (locatorIndexOnStack != -1) {
585588
columnIndents.remove(locatorIndexOnStack);
586589
initialIndents.remove(locatorIndexOnStack);

0 commit comments

Comments
 (0)