Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ObjectNode findValue(s) and findParent(s) fast paths #4008

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 46 additions & 36 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,13 @@ public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
@Override
public JsonNode findValue(String propertyName)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
return entry.getValue();
}
JsonNode value = entry.getValue().findValue(propertyName);
JsonNode jsonNode = _children.get(propertyName);
if (jsonNode != null) {
return jsonNode;
}

for (JsonNode child : _children.values()) {
JsonNode value = child.findValue(propertyName);
if (value != null) {
return value;
}
Expand All @@ -342,44 +344,50 @@ public JsonNode findValue(String propertyName)
@Override
public List<JsonNode> findValues(String propertyName, List<JsonNode> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<JsonNode>();
}
foundSoFar.add(entry.getValue());
} else { // only add children if parent not added
foundSoFar = entry.getValue().findValues(propertyName, foundSoFar);
JsonNode jsonNode = _children.get(propertyName);
if (jsonNode != null) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<>();
}
foundSoFar.add(jsonNode);
return foundSoFar;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here's the problem that was reported as #4229: we are to collect ALL matches, not just main-level one

}

// only add children if parent not added
for (JsonNode child : _children.values()) {
foundSoFar = child.findValues(propertyName, foundSoFar);
}
return foundSoFar;
}

@Override
public List<String> findValuesAsText(String propertyName, List<String> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<String>();
}
foundSoFar.add(entry.getValue().asText());
} else { // only add children if parent not added
foundSoFar = entry.getValue().findValuesAsText(propertyName,
foundSoFar);
JsonNode jsonNode = _children.get(propertyName);
if (jsonNode != null) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<>();
}
foundSoFar.add(jsonNode.asText());
return foundSoFar;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and here

}

// only add children if parent not added
for (JsonNode child : _children.values()) {
foundSoFar = child.findValuesAsText(propertyName, foundSoFar);
}
return foundSoFar;
}

@Override
public ObjectNode findParent(String propertyName)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
return this;
}
JsonNode value = entry.getValue().findParent(propertyName);
JsonNode jsonNode = _children.get(propertyName);
if (jsonNode != null) {
return this;
}
for (JsonNode child : _children.values()) {
JsonNode value = child.findParent(propertyName);
if (value != null) {
return (ObjectNode) value;
}
Expand All @@ -390,16 +398,18 @@ public ObjectNode findParent(String propertyName)
@Override
public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<JsonNode>();
}
foundSoFar.add(this);
} else { // only add children if parent not added
foundSoFar = entry.getValue()
.findParents(propertyName, foundSoFar);
JsonNode jsonNode = _children.get(propertyName);
if (jsonNode != null) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<>();
}
foundSoFar.add(this);
return foundSoFar;
}

// only add children if parent not added
for (JsonNode child : _children.values()) {
foundSoFar = child.findParents(propertyName, foundSoFar);
}
return foundSoFar;
}
Expand Down