Skip to content

Commit d6cf2e2

Browse files
committed
Walk XML children once when filtering by tag
childElements(parent, tag) used to call childElements(parent) and filter the resulting list, traversing the NodeList twice. Inline the filter so each call walks the children once. Also drop the pre-sized ArrayList in childElements(parent) — Stream.toList() returns a right-sized immutable list directly.
1 parent d829426 commit d6cf2e2

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

code/src/java/pcgen/core/namegen/NameGenDataLoader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,20 @@ private static void loadCategory(Element category, RuleSet rs, Map<String, List<
203203
private static List<Element> childElements(Element parent)
204204
{
205205
NodeList nodes = parent.getChildNodes();
206-
List<Element> out = new ArrayList<>(nodes.getLength());
207-
IntStream.range(0, nodes.getLength())
206+
return IntStream.range(0, nodes.getLength())
208207
.mapToObj(nodes::item)
209208
.filter(n -> n.getNodeType() == Node.ELEMENT_NODE)
210209
.map(Element.class::cast)
211-
.forEach(out::add);
212-
return out;
210+
.toList();
213211
}
214212

215213
private static List<Element> childElements(Element parent, String tagName)
216214
{
217-
return childElements(parent).stream()
215+
NodeList nodes = parent.getChildNodes();
216+
return IntStream.range(0, nodes.getLength())
217+
.mapToObj(nodes::item)
218+
.filter(n -> n.getNodeType() == Node.ELEMENT_NODE)
219+
.map(Element.class::cast)
218220
.filter(e -> tagName.equals(e.getTagName()))
219221
.toList();
220222
}

0 commit comments

Comments
 (0)