@@ -40,24 +40,37 @@ public RubyArray execute(VirtualFrame frame) {
40
40
arrayBuilderNode = insert (ArrayBuilderNode .create ());
41
41
}
42
42
43
- BuilderState state = arrayBuilderNode .start ();
44
- int length = 0 ;
43
+ // Compute the total length
44
+ int totalLength = 0 ;
45
+ final Object [] values = new Object [children .length ];
46
+ for (int n = 0 ; n < children .length ; n ++) {
47
+ final Object value = values [n ] = children [n ].execute (frame );
48
+
49
+ if (isArrayProfile .profile (value instanceof RubyArray )) {
50
+ totalLength += ((RubyArray ) value ).size ;
51
+ } else {
52
+ totalLength ++;
53
+ }
54
+ }
55
+
56
+ // Create a builder with the right length and append values
57
+ BuilderState state = arrayBuilderNode .start (totalLength );
58
+ int index = 0 ;
45
59
46
60
for (int n = 0 ; n < children .length ; n ++) {
47
- final Object childObject = children [n ]. execute ( frame ) ;
61
+ final Object value = values [n ];
48
62
49
- if (isArrayProfile .profile (childObject instanceof RubyArray )) {
50
- final RubyArray childArray = (RubyArray ) childObject ;
51
- final int size = childArray .size ;
52
- arrayBuilderNode .appendArray (state , length , childArray );
53
- length += size ;
63
+ if (isArrayProfile .profile (value instanceof RubyArray )) {
64
+ final RubyArray childArray = (RubyArray ) value ;
65
+ arrayBuilderNode .appendArray (state , index , childArray );
66
+ index += childArray .size ;
54
67
} else {
55
- arrayBuilderNode .appendValue (state , length , childObject );
56
- length ++;
68
+ arrayBuilderNode .appendValue (state , index , value );
69
+ index ++;
57
70
}
58
71
}
59
72
60
- return createArray (arrayBuilderNode .finish (state , length ), length );
73
+ return createArray (arrayBuilderNode .finish (state , index ), index );
61
74
}
62
75
63
76
@ ExplodeLoop
0 commit comments