@@ -34,18 +34,18 @@ public FunctionBase ParseWithCache(String funExp) {
3434 FunctionBase result = calculateCache .get (funExp );
3535 if (result != null ) return result ;
3636 CalculateTree tree = AlgorithmEngineHelper .ParseCalculate (funExp );
37- return CreateCalculate (tree , funExp );
37+ FunctionBase fun = CreateCalculate (tree , funExp );
38+ calculateCache .put (funExp , fun );
39+ return fun ;
3840 }
3941
4042 private FunctionBase CreateCalculate (CalculateTree tree , String exp ) {
41- if (tree .Type == CalculateTreeType .Error ) {
42- throw new RuntimeException (tree .ErrorMessage );
43- }
44- String key = exp .substring (tree .Start , tree .End + 1 );
45- if (calculateCache .containsKey (key )) {
46- return calculateCache .get (key );
47- }
4843 if (tree .Type == CalculateTreeType .String ) {
44+ // 仅叶子节点需要字符串内容;中间节点不再以子串作为缓存 key,避免 substring 产生 O(n²) 子串驻留
45+ String key = exp .substring (tree .Start , tree .End + 1 );
46+ if (calculateCache .containsKey (key )) {
47+ return calculateCache .get (key );
48+ }
4949 try {
5050 FunctionBase fun = AlgorithmEngineHelper .ParseFormula (key );
5151 calculateCache .put (key , fun );
@@ -54,44 +54,39 @@ private FunctionBase CreateCalculate(CalculateTree tree, String exp) {
5454 throw new RuntimeException (e );
5555 }
5656 }
57+ if (tree .Type == CalculateTreeType .Error ) {
58+ throw new RuntimeException (tree .ErrorMessage );
59+ }
5760
5861 FunctionBase leftFunc = CreateCalculate (tree .Nodes .get (0 ), exp );
5962 FunctionBase rightFunc = CreateCalculate (tree .Nodes .get (1 ), exp );
6063 CombineCalculateType combineType = CombineCalculateType .intToEnum (tree .Type .getValue ());
61- FunctionBase fun = AlgorithmEngineHelper .CombineCalculate (leftFunc , combineType , rightFunc );
62- calculateCache .put (key , fun );
63- return fun ;
64+ return AlgorithmEngineHelper .CombineCalculate (leftFunc , combineType , rightFunc );
6465 }
6566
6667 public FunctionBase ParseConditionWithCache (String funExp ) {
6768 FunctionBase result = conditionCache .get (funExp );
6869 if (result != null ) return result ;
6970 ConditionTree tree = AlgorithmEngineHelper .ParseCondition (funExp );
70- return CreateCondition (tree , funExp );
71+ FunctionBase fun = CreateCondition (tree , funExp );
72+ conditionCache .put (funExp , fun );
73+ return fun ;
7174 }
7275
7376 private FunctionBase CreateCondition (ConditionTree tree , String exp ) {
77+ if (tree .Type == ConditionTreeType .String ) {
78+ return ParseWithCache (exp .substring (tree .Start , tree .End + 1 ));
79+ }
7480 if (tree .Type == ConditionTreeType .Error ) {
7581 throw new RuntimeException (tree .ErrorMessage );
7682 }
77- String key = exp .substring (tree .Start , tree .End + 1 );
78- if (conditionCache .containsKey (key )) {
79- return conditionCache .get (key );
80- }
81- if (tree .Type == ConditionTreeType .String ) {
82- return ParseWithCache (key );
83- }
8483
8584 FunctionBase leftFunc = CreateCondition (tree .Nodes .get (0 ), exp );
8685 FunctionBase rightFunc = CreateCondition (tree .Nodes .get (1 ), exp );
8786 if (tree .Type == ConditionTreeType .And ) {
88- FunctionBase fun = AlgorithmEngineHelper .Condition_And (leftFunc , rightFunc );
89- conditionCache .put (key , fun );
90- return fun ;
87+ return AlgorithmEngineHelper .Condition_And (leftFunc , rightFunc );
9188 } else if (tree .Type == ConditionTreeType .Or ) {
92- FunctionBase fun = AlgorithmEngineHelper .Condition_Or (leftFunc , rightFunc );
93- conditionCache .put (key , fun );
94- return fun ;
89+ return AlgorithmEngineHelper .Condition_Or (leftFunc , rightFunc );
9590 }
9691 throw new RuntimeException (tree .ErrorMessage );
9792 }
0 commit comments