From b5e821774355ee045b98f55f1690f3a1ffaaf841 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Thu, 31 Aug 2023 19:38:44 +0800 Subject: [PATCH] [opt](Nereids) speed up deepEquals of TreeNode (#23710) --- .../org/apache/doris/nereids/trees/TreeNode.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java index 10be3d74ca..6f937cc96d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/TreeNode.java @@ -231,9 +231,9 @@ public interface TreeNode> { * @param that other tree node * @return true if all the tree is equals */ - default boolean deepEquals(TreeNode that) { - Deque thisDeque = new ArrayDeque<>(); - Deque thatDeque = new ArrayDeque<>(); + default boolean deepEquals(TreeNode that) { + Deque> thisDeque = new ArrayDeque<>(); + Deque> thatDeque = new ArrayDeque<>(); thisDeque.push(this); thatDeque.push(that); @@ -244,8 +244,13 @@ public interface TreeNode> { return false; } - TreeNode currentNodeThis = thisDeque.pop(); - TreeNode currentNodeThat = thatDeque.pop(); + TreeNode currentNodeThis = thisDeque.pop(); + TreeNode currentNodeThat = thatDeque.pop(); + + // since TreeNode is immutable, use == to short circuit + if (currentNodeThis == currentNodeThat) { + continue; + } // If current nodes are not equal or the number of child nodes differ, return false. if (!currentNodeThis.equals(currentNodeThat)