@@ -786,15 +786,19 @@ private <T extends Node, U> T insertChildNodeInt(Node[] nodes, int nodeIndex, Cl
786
786
if (expectedClass .isInstance (node )) {
787
787
return (T ) node ;
788
788
}
789
- return doInsertChildNodeInt (nodes , nodeIndex , nodeSupplier , argument );
789
+ return doInsertChildNodeInt (nodes , nodeIndex , expectedClass , nodeSupplier , argument );
790
790
}
791
791
792
792
@ SuppressWarnings ("unchecked" )
793
- private <T extends Node > T doInsertChildNodeInt (Node [] nodes , int nodeIndex , IntNodeFunction <T > nodeSupplier , int argument ) {
793
+ private <T extends Node , U > T doInsertChildNodeInt (Node [] nodes , int nodeIndex , Class < U > expectedClass , IntNodeFunction <T > nodeSupplier , int argument ) {
794
794
CompilerDirectives .transferToInterpreterAndInvalidate ();
795
795
Lock lock = getLock ();
796
796
lock .lock ();
797
797
try {
798
+ Node node = nodes [nodeIndex ];
799
+ if (expectedClass .isInstance (node )) {
800
+ return (T ) node ;
801
+ }
798
802
T newNode = nodeSupplier .apply (argument );
799
803
doInsertChildNode (nodes , nodeIndex , newNode );
800
804
return newNode ;
@@ -809,15 +813,19 @@ private <T extends Node, U extends T> U insertChildNode(Node[] nodes, int nodeIn
809
813
if (node != null && node .getClass () == cachedClass ) {
810
814
return CompilerDirectives .castExact (node , cachedClass );
811
815
}
812
- return CompilerDirectives .castExact (doInsertChildNode (nodes , nodeIndex , nodeSupplier ), cachedClass );
816
+ return CompilerDirectives .castExact (doInsertChildNode (nodes , nodeIndex , cachedClass , nodeSupplier ), cachedClass );
813
817
}
814
818
815
819
@ SuppressWarnings ("unchecked" )
816
- private <T extends Node > T doInsertChildNode (Node [] nodes , int nodeIndex , NodeSupplier <T > nodeSupplier ) {
820
+ private <T extends Node , U > T doInsertChildNode (Node [] nodes , int nodeIndex , Class < U > cachedClass , NodeSupplier <T > nodeSupplier ) {
817
821
CompilerDirectives .transferToInterpreterAndInvalidate ();
818
822
Lock lock = getLock ();
819
823
lock .lock ();
820
824
try {
825
+ Node node = nodes [nodeIndex ];
826
+ if (node != null && node .getClass () == cachedClass ) {
827
+ return (T ) node ;
828
+ }
821
829
T newNode = nodeSupplier .get ();
822
830
doInsertChildNode (nodes , nodeIndex , newNode );
823
831
return newNode ;
@@ -835,7 +843,7 @@ private <T extends Node> T insertChildNode(Node[] nodes, int nodeIndex, T uncach
835
843
if (node != null && node .getClass () == cachedClass ) {
836
844
return CompilerDirectives .castExact (node , cachedClass );
837
845
}
838
- return CompilerDirectives .castExact (doInsertChildNode (nodes , nodeIndex , nodeSupplier ), cachedClass );
846
+ return CompilerDirectives .castExact (doInsertChildNode (nodes , nodeIndex , cachedClass , nodeSupplier ), cachedClass );
839
847
}
840
848
841
849
private void doInsertChildNode (Node [] nodes , int nodeIndex , Node newNode ) {
0 commit comments