Skip to content

Commit 8050a1e

Browse files
authored
Optimize ObjectNode findValue(s) and findParent(s) fast paths (#4008)
1 parent 4f3f6db commit 8050a1e

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,13 @@ public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
327327
@Override
328328
public JsonNode findValue(String propertyName)
329329
{
330-
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
331-
if (propertyName.equals(entry.getKey())) {
332-
return entry.getValue();
333-
}
334-
JsonNode value = entry.getValue().findValue(propertyName);
330+
JsonNode jsonNode = _children.get(propertyName);
331+
if (jsonNode != null) {
332+
return jsonNode;
333+
}
334+
335+
for (JsonNode child : _children.values()) {
336+
JsonNode value = child.findValue(propertyName);
335337
if (value != null) {
336338
return value;
337339
}
@@ -342,44 +344,50 @@ public JsonNode findValue(String propertyName)
342344
@Override
343345
public List<JsonNode> findValues(String propertyName, List<JsonNode> foundSoFar)
344346
{
345-
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
346-
if (propertyName.equals(entry.getKey())) {
347-
if (foundSoFar == null) {
348-
foundSoFar = new ArrayList<JsonNode>();
349-
}
350-
foundSoFar.add(entry.getValue());
351-
} else { // only add children if parent not added
352-
foundSoFar = entry.getValue().findValues(propertyName, foundSoFar);
347+
JsonNode jsonNode = _children.get(propertyName);
348+
if (jsonNode != null) {
349+
if (foundSoFar == null) {
350+
foundSoFar = new ArrayList<>();
353351
}
352+
foundSoFar.add(jsonNode);
353+
return foundSoFar;
354+
}
355+
356+
// only add children if parent not added
357+
for (JsonNode child : _children.values()) {
358+
foundSoFar = child.findValues(propertyName, foundSoFar);
354359
}
355360
return foundSoFar;
356361
}
357362

358363
@Override
359364
public List<String> findValuesAsText(String propertyName, List<String> foundSoFar)
360365
{
361-
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
362-
if (propertyName.equals(entry.getKey())) {
363-
if (foundSoFar == null) {
364-
foundSoFar = new ArrayList<String>();
365-
}
366-
foundSoFar.add(entry.getValue().asText());
367-
} else { // only add children if parent not added
368-
foundSoFar = entry.getValue().findValuesAsText(propertyName,
369-
foundSoFar);
366+
JsonNode jsonNode = _children.get(propertyName);
367+
if (jsonNode != null) {
368+
if (foundSoFar == null) {
369+
foundSoFar = new ArrayList<>();
370370
}
371+
foundSoFar.add(jsonNode.asText());
372+
return foundSoFar;
373+
}
374+
375+
// only add children if parent not added
376+
for (JsonNode child : _children.values()) {
377+
foundSoFar = child.findValuesAsText(propertyName, foundSoFar);
371378
}
372379
return foundSoFar;
373380
}
374381

375382
@Override
376383
public ObjectNode findParent(String propertyName)
377384
{
378-
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
379-
if (propertyName.equals(entry.getKey())) {
380-
return this;
381-
}
382-
JsonNode value = entry.getValue().findParent(propertyName);
385+
JsonNode jsonNode = _children.get(propertyName);
386+
if (jsonNode != null) {
387+
return this;
388+
}
389+
for (JsonNode child : _children.values()) {
390+
JsonNode value = child.findParent(propertyName);
383391
if (value != null) {
384392
return (ObjectNode) value;
385393
}
@@ -390,16 +398,18 @@ public ObjectNode findParent(String propertyName)
390398
@Override
391399
public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar)
392400
{
393-
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
394-
if (propertyName.equals(entry.getKey())) {
395-
if (foundSoFar == null) {
396-
foundSoFar = new ArrayList<JsonNode>();
397-
}
398-
foundSoFar.add(this);
399-
} else { // only add children if parent not added
400-
foundSoFar = entry.getValue()
401-
.findParents(propertyName, foundSoFar);
401+
JsonNode jsonNode = _children.get(propertyName);
402+
if (jsonNode != null) {
403+
if (foundSoFar == null) {
404+
foundSoFar = new ArrayList<>();
402405
}
406+
foundSoFar.add(this);
407+
return foundSoFar;
408+
}
409+
410+
// only add children if parent not added
411+
for (JsonNode child : _children.values()) {
412+
foundSoFar = child.findParents(propertyName, foundSoFar);
403413
}
404414
return foundSoFar;
405415
}

0 commit comments

Comments
 (0)