81
81
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemNodeGen ;
82
82
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemScalarNodeGen ;
83
83
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetItemSliceNodeGen ;
84
+ import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .LenNodeGen ;
84
85
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .ListGeneralizationNodeGen ;
85
86
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .NoGeneralizationNodeGen ;
86
87
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .NormalizeIndexNodeGen ;
@@ -148,6 +149,8 @@ abstract static class SequenceStorageBaseNode extends PBaseNode {
148
149
149
150
protected static final int DEFAULT_CAPACITY = 8 ;
150
151
152
+ protected static final int MAX_SEQUENCE_STORAGES = 12 ;
153
+
151
154
protected static boolean isByteStorage (NativeSequenceStorage store ) {
152
155
return store .getElementType () == ListStorageType .Byte ;
153
156
}
@@ -2079,6 +2082,8 @@ public static ExtendNode create(Supplier<GeneralizationNode> genNodeProvider) {
2079
2082
}
2080
2083
2081
2084
public abstract static class RepeatNode extends SequenceStorageBaseNode {
2085
+ private static final String ERROR_MSG = "can't multiply sequence by non-int of type '%p'" ;
2086
+
2082
2087
@ Child private SetItemScalarNode setItemNode ;
2083
2088
@ Child private GetItemScalarNode getItemNode ;
2084
2089
@ Child private GetItemScalarNode getRightItemNode ;
@@ -2090,14 +2095,19 @@ public abstract static class RepeatNode extends SequenceStorageBaseNode {
2090
2095
2091
2096
public abstract SequenceStorage execute (SequenceStorage left , int times );
2092
2097
2098
+ @ Specialization
2099
+ SequenceStorage doEmpty (EmptySequenceStorage s , @ SuppressWarnings ("unused" ) int times ) {
2100
+ return s ;
2101
+ }
2102
+
2093
2103
@ Specialization (guards = "times <= 0" )
2094
- SequenceStorage doGeneric (SequenceStorage s , @ SuppressWarnings ("unused" ) int times ,
2104
+ SequenceStorage doZeroRepeat (SequenceStorage s , @ SuppressWarnings ("unused" ) int times ,
2095
2105
@ Cached ("createClassProfile()" ) ValueProfile storageTypeProfile ) {
2096
2106
return storageTypeProfile .profile (s ).createEmpty (0 );
2097
2107
}
2098
2108
2099
- @ Specialization (limit = "2" , guards = {"!isNative(s)" , "s.getClass() == cachedClass" })
2100
- SequenceStorage doManaged (SequenceStorage s , int times ,
2109
+ @ Specialization (limit = "2" , guards = {"times > 0" , " !isNative(s)" , "s.getClass() == cachedClass" })
2110
+ SequenceStorage doManaged (BasicSequenceStorage s , int times ,
2101
2111
@ Cached ("create()" ) BranchProfile outOfMemProfile ,
2102
2112
@ Cached ("s.getClass()" ) Class <? extends SequenceStorage > cachedClass ) {
2103
2113
try {
@@ -2115,18 +2125,20 @@ SequenceStorage doManaged(SequenceStorage s, int times,
2115
2125
}
2116
2126
}
2117
2127
2118
- @ Specialization (replaces = "doManaged" )
2128
+ @ Specialization (replaces = "doManaged" , limit = "2" , guards = { "times > 0" , "s.getClass() == cachedClass" } )
2119
2129
SequenceStorage doGeneric (SequenceStorage s , int times ,
2120
- @ Cached ("create()" ) BranchProfile outOfMemProfile ) {
2130
+ @ Cached ("create()" ) BranchProfile outOfMemProfile ,
2131
+ @ Cached ("s.getClass()" ) Class <? extends SequenceStorage > cachedClass ) {
2121
2132
try {
2122
- int len = s .length ();
2133
+ SequenceStorage profiled = cachedClass .cast (s );
2134
+ int len = profiled .length ();
2123
2135
2124
2136
ObjectSequenceStorage repeated = new ObjectSequenceStorage (Math .multiplyExact (len , times ));
2125
2137
2126
2138
// TODO avoid temporary array
2127
2139
Object [] values = new Object [len ];
2128
2140
for (int i = 0 ; i < len ; i ++) {
2129
- values [i ] = getGetItemNode ().execute (s , i );
2141
+ values [i ] = getGetItemNode ().execute (profiled , i );
2130
2142
}
2131
2143
2132
2144
Object destArr = repeated .getInternalArrayObject ();
@@ -2138,10 +2150,11 @@ SequenceStorage doGeneric(SequenceStorage s, int times,
2138
2150
}
2139
2151
}
2140
2152
2141
- @ Fallback
2142
- SequenceStorage doGeneric (SequenceStorage s , Object times ) {
2153
+ @ Specialization ( guards = "!isInt(times)" )
2154
+ SequenceStorage doNonInt (SequenceStorage s , Object times ) {
2143
2155
int i = toIndex (times );
2144
2156
if (recursive == null ) {
2157
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
2145
2158
recursive = insert (RepeatNodeGen .create ());
2146
2159
}
2147
2160
return recursive .execute (s , i );
@@ -2162,6 +2175,10 @@ private static void repeat(Object dest, Object src, int len, int times) {
2162
2175
}
2163
2176
}
2164
2177
2178
+ protected static boolean isInt (Object times ) {
2179
+ return times instanceof Integer ;
2180
+ }
2181
+
2165
2182
private int toIndex (Object times ) {
2166
2183
if (isIndexNode == null ) {
2167
2184
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -2170,11 +2187,11 @@ private int toIndex(Object times) {
2170
2187
if (isIndexNode .execute (times )) {
2171
2188
if (castToindexNode == null ) {
2172
2189
CompilerDirectives .transferToInterpreterAndInvalidate ();
2173
- castToindexNode = insert (CastToIndexNode .create ());
2190
+ castToindexNode = insert (CastToIndexNode .createOverflow ());
2174
2191
}
2175
2192
return castToindexNode .execute (times );
2176
2193
}
2177
- throw raise (TypeError , "can't multiply sequence by non-int of type '%p'" , times );
2194
+ throw raise (TypeError , ERROR_MSG , times );
2178
2195
}
2179
2196
2180
2197
public static RepeatNode create () {
@@ -2590,7 +2607,7 @@ EmptySequenceStorage doEmpty(EmptySequenceStorage s, @SuppressWarnings("unused")
2590
2607
return s ;
2591
2608
}
2592
2609
2593
- @ Specialization (limit = "9 " , guards = "s.getClass() == cachedClass" )
2610
+ @ Specialization (limit = "MAX_SEQUENCE_STORAGES " , guards = "s.getClass() == cachedClass" )
2594
2611
BasicSequenceStorage doManaged (BasicSequenceStorage s , int cap ,
2595
2612
@ Cached ("create()" ) BranchProfile overflowErrorProfile ,
2596
2613
@ Cached ("s.getClass()" ) Class <? extends BasicSequenceStorage > cachedClass ) {
@@ -2616,4 +2633,19 @@ public static EnsureCapacityNode create() {
2616
2633
}
2617
2634
2618
2635
}
2636
+
2637
+ public abstract static class LenNode extends SequenceStorageBaseNode {
2638
+
2639
+ public abstract int execute (SequenceStorage s );
2640
+
2641
+ @ Specialization (limit = "MAX_SEQUENCE_STORAGES" , guards = "s.getClass() == cachedClass" )
2642
+ int doSpecial (SequenceStorage s ,
2643
+ @ Cached ("s.getClass()" ) Class <? extends SequenceStorage > cachedClass ) {
2644
+ return cachedClass .cast (s ).length ();
2645
+ }
2646
+
2647
+ public static LenNode create () {
2648
+ return LenNodeGen .create ();
2649
+ }
2650
+ }
2619
2651
}
0 commit comments