Skip to content

Commit 3af0c77

Browse files
committed
[optimise] Avoid unnecessary double iteration in fn:reverse
1 parent 004d5dc commit 3af0c77

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@
10321032
<include>src/main/java/org/exist/xquery/functions/fn/FunBaseURI.java</include>
10331033
<include>src/main/java/org/exist/xquery/functions/fn/FunDeepEqual.java</include>
10341034
<include>src/main/java/org/exist/xquery/functions/fn/FunParseIetfDate.java</include>
1035+
<include>src/main/java/org/exist/xquery/functions/fn/FunReverse.java</include>
10351036
<include>src/main/java/org/exist/xquery/functions/fn/FunSerialize.java</include>
10361037
<include>src/main/java/org/exist/xquery/functions/fn/FunTrace.java</include>
10371038
<include>src/main/java/org/exist/xquery/functions/fn/FunUnparsedText.java</include>
@@ -1466,6 +1467,7 @@
14661467
<exclude>src/main/java/org/exist/xquery/functions/fn/FunDeepEqual.java</exclude>
14671468
<exclude>src/main/java/org/exist/xquery/functions/fn/FunDocAvailable.java</exclude>
14681469
<exclude>src/main/java/org/exist/xquery/functions/fn/FunParseIetfDate.java</exclude>
1470+
<exclude>src/main/java/org/exist/xquery/functions/fn/FunReverse.java</exclude>
14691471
<exclude>src/main/java/org/exist/xquery/functions/fn/FunSerialize.java</exclude>
14701472
<exclude>src/main/java/org/exist/xquery/functions/fn/FunTrace.java</exclude>
14711473
<exclude>src/main/java/org/exist/xquery/functions/fn/FunUnparsedText.java</exclude>

exist-core/src/main/java/org/exist/xquery/functions/fn/FunReverse.java

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -27,15 +51,15 @@
2751
import org.exist.xquery.value.FunctionReturnSequenceType;
2852
import org.exist.xquery.value.Item;
2953
import org.exist.xquery.value.Sequence;
30-
import org.exist.xquery.value.SequenceIterator;
54+
import org.exist.xquery.value.ValueSequence;
3155
import org.exist.xquery.value.SequenceType;
3256
import org.exist.xquery.value.Type;
33-
import org.exist.xquery.value.ValueSequence;
3457

3558
/**
3659
* Implements the fn:reverse function.
3760
*
3861
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
62+
* @author <a href="mailto:[email protected]">Adam Retter</a>
3963
*/
4064
public class FunReverse extends Function {
4165

@@ -79,37 +103,34 @@ public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException
79103
argumentsChecked = true;
80104
}
81105

82-
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
106+
@Override
107+
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
83108
if (context.getProfiler().isEnabled()) {
84-
context.getProfiler().start(this);
109+
context.getProfiler().start(this);
85110
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
86-
if (contextSequence != null)
87-
{context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);}
88-
if (contextItem != null)
89-
{context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());}
90-
}
91-
92-
Sequence result;
111+
if (contextSequence != null) {
112+
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
113+
}
114+
if (contextItem != null) {
115+
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
116+
}
117+
}
118+
93119
final Sequence seq = getArguments(contextSequence, contextItem)[0];
94-
if (seq.isEmpty())
95-
{result = Sequence.EMPTY_SEQUENCE;}
96-
else {
97-
final Sequence tmp = new ValueSequence();
98-
Item item;
99-
for(final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
100-
item = i.nextItem();
101-
tmp.add(item);
102-
}
103-
result = new ValueSequence();
104-
for (int i = seq.getItemCount() - 1; i >= 0; i--) {
105-
result.add(tmp.itemAt(i));
106-
}
120+
final Sequence result;
121+
if (seq.isEmpty()) {
122+
result = Sequence.EMPTY_SEQUENCE;
123+
} else {
124+
result = new ValueSequence();
125+
for (int i = seq.getItemCount() - 1; i >= 0; i--) {
126+
result.add(seq.itemAt(i));
127+
}
107128
}
108129

109-
if (context.getProfiler().isEnabled())
110-
{context.getProfiler().end(this, "", result);}
111-
112-
return result;
113-
}
130+
if (context.getProfiler().isEnabled()) {
131+
context.getProfiler().end(this, "", result);
132+
}
114133

134+
return result;
135+
}
115136
}

0 commit comments

Comments
 (0)