@@ -432,26 +432,12 @@ public Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
432
432
throw new RaiseException (getContext (), getContext ().getCoreExceptions ().argumentError ("Result of string concatenation exceeds the system maximum string length" , this ));
433
433
}
434
434
435
- if (shouldRebalanceProfile .profile (left .depth () >= getContext ().getOptions ().ROPE_DEPTH_THRESHOLD )) {
436
- if (flattenNode == null ) {
437
- CompilerDirectives .transferToInterpreterAndInvalidate ();
438
- flattenNode = insert (FlattenNode .create ());
439
- }
440
-
441
- if (left instanceof ConcatRope ) {
442
- left = rebalance ((ConcatRope ) left , getContext ().getOptions ().ROPE_DEPTH_THRESHOLD , flattenNode );
443
- }
435
+ if (shouldRebalanceProfile .profile (left .depth () >= getContext ().getOptions ().ROPE_DEPTH_THRESHOLD && left instanceof ConcatRope )) {
436
+ left = rebalance ((ConcatRope ) left , getContext ().getOptions ().ROPE_DEPTH_THRESHOLD , getFlattenNode ());
444
437
}
445
438
446
- if (shouldRebalanceProfile .profile (right .depth () >= getContext ().getOptions ().ROPE_DEPTH_THRESHOLD )) {
447
- if (flattenNode == null ) {
448
- CompilerDirectives .transferToInterpreterAndInvalidate ();
449
- flattenNode = insert (FlattenNode .create ());
450
- }
451
-
452
- if (right instanceof ConcatRope ) {
453
- right = rebalance ((ConcatRope ) right , getContext ().getOptions ().ROPE_DEPTH_THRESHOLD , flattenNode );
454
- }
439
+ if (shouldRebalanceProfile .profile (right .depth () >= getContext ().getOptions ().ROPE_DEPTH_THRESHOLD && right instanceof ConcatRope )) {
440
+ right = rebalance ((ConcatRope ) right , getContext ().getOptions ().ROPE_DEPTH_THRESHOLD , getFlattenNode ());
455
441
}
456
442
457
443
int depth = depth (left , right );
@@ -465,6 +451,14 @@ public Rope concat(ManagedRope left, ManagedRope right, Encoding encoding,
465
451
depth , isBalanced (left , right ));
466
452
}
467
453
454
+ private FlattenNode getFlattenNode () {
455
+ if (flattenNode == null ) {
456
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
457
+ flattenNode = insert (FlattenNode .create ());
458
+ }
459
+ return flattenNode ;
460
+ }
461
+
468
462
private boolean isBalanced (Rope left , Rope right ) {
469
463
// Our definition of balanced is centered around the notion of rebalancing. We could have a simple structure
470
464
// such as ConcatRope(ConcatRope(LeafRope, LeafRope), LeafRope) that is balanced on its own but may contribute
0 commit comments