Skip to content

Commit 266e17d

Browse files
committed
[optimize] Reduce the memory used for comparing sequences of Item and Node objects
1 parent 3344f29 commit 266e17d

7 files changed

Lines changed: 20 additions & 8 deletions

File tree

exist-core/src/main/java/org/exist/xquery/Except.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Sequence combine(final Sequence ls, final Sequence rs) throws XPathExcept
8282
@Nullable Set<Item> set = null;
8383
for (final SequenceIterator i = rs.unorderedIterator(); i.hasNext(); ) {
8484
if (set == null) {
85-
set = new ObjectAVLTreeSet<>(new ItemComparator(null));
85+
set = new ObjectAVLTreeSet<>(ItemComparator.WITHOUT_COLLATOR);
8686
}
8787
set.add(i.nextItem());
8888
}

exist-core/src/main/java/org/exist/xquery/Intersect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Sequence combine(final Sequence ls, final Sequence rs) throws XPathExcept
7676
@Nullable Set<Item> set = null;
7777
for (final SequenceIterator i = ls.unorderedIterator(); i.hasNext(); ) {
7878
if (set == null) {
79-
set = new ObjectAVLTreeSet<>(new ItemComparator(null));
79+
set = new ObjectAVLTreeSet<>(ItemComparator.WITHOUT_COLLATOR);
8080
}
8181
set.add(i.nextItem());
8282
}

exist-core/src/main/java/org/exist/xquery/value/ArrayListValueSequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void removeDuplicates() {
267267
final List<Item> newValues = new ArrayList<>(values.size());
268268
int newType = Type.ANY_TYPE;
269269

270-
final ItemComparator itemComparator = new ItemComparator();
270+
final ItemComparator itemComparator = ItemComparator.WITHOUT_COLLATOR;
271271

272272
for (int i = 0; i < values.size(); i++) {
273273
final Item value = values.get(i);

exist-core/src/main/java/org/exist/xquery/value/AtomicValueComparator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
@ThreadSafe
4949
public class AtomicValueComparator implements Comparator<AtomicValue> {
5050

51-
protected static final Logger LOG = LogManager.getLogger(RESTServer.class);
51+
protected static final Logger LOG = LogManager.getLogger(AtomicValueComparator.class);
52+
53+
public static AtomicValueComparator WITHOUT_COLLATOR = new AtomicValueComparator();
5254

5355
private final @Nullable Collator collator;
5456

55-
public AtomicValueComparator() {
57+
private AtomicValueComparator() {
5658
this(null);
5759
}
5860

exist-core/src/main/java/org/exist/xquery/value/ItemComparator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class ItemComparator implements Comparator<Item> {
5656
@Nullable private final Collator collator;
5757
@Nullable private AtomicValueComparator atomicValueComparator = null;
5858

59+
public static ItemComparator WITHOUT_COLLATOR = new ItemComparator();
60+
5961
public ItemComparator() {
6062
this(null);
6163
}
@@ -70,7 +72,11 @@ public int compare(final Item n1, final Item n2) {
7072
return Constants.INFERIOR;
7173
} else if (n1 instanceof AtomicValue && n2 instanceof AtomicValue) {
7274
if (atomicValueComparator == null) {
73-
atomicValueComparator = new AtomicValueComparator(collator);
75+
if (collator == null) {
76+
atomicValueComparator = AtomicValueComparator.WITHOUT_COLLATOR;
77+
} else {
78+
atomicValueComparator = new AtomicValueComparator(collator);
79+
}
7480
}
7581
return atomicValueComparator.compare((AtomicValue)n1, (AtomicValue)n2);
7682
} else if (n1 instanceof Comparable) {

exist-core/src/main/java/org/exist/xquery/value/SequenceComparator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public int compare(final Sequence o1, final Sequence o2) {
8383

8484
for (int i = 0; i < o1Count; i++) {
8585
if (itemComparator == null) {
86-
itemComparator = new ItemComparator(collator);
86+
if (collator == null) {
87+
itemComparator = ItemComparator.WITHOUT_COLLATOR;
88+
} else {
89+
itemComparator = new ItemComparator(collator);
90+
}
8791
}
8892

8993
final Item i1 = o1.itemAt(i);

exist-core/src/main/java/org/exist/xquery/value/ValueSequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private void removeDuplicateNodes() {
500500
if (!hasNodes) {
501501
return;
502502
}
503-
final Map<Item, Item> nodes = new TreeMap<>(new ItemComparator());
503+
final Map<Item, Item> nodes = new TreeMap<>(ItemComparator.WITHOUT_COLLATOR);
504504
int j = 0;
505505
for (int i = 0; i <= size; i++) {
506506
if (Type.subTypeOf(values[i].getType(), Type.NODE)) {

0 commit comments

Comments
 (0)