161
161
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
162
162
import com .oracle .truffle .api .interop .UnsupportedTypeException ;
163
163
import com .oracle .truffle .api .library .CachedLibrary ;
164
+ import com .oracle .truffle .api .nodes .ExplodeLoop ;
164
165
import com .oracle .truffle .api .nodes .Node ;
165
166
import com .oracle .truffle .api .profiles .BranchProfile ;
166
167
import com .oracle .truffle .api .profiles .ConditionProfile ;
@@ -2009,15 +2010,28 @@ byte[] doNativeByte(NativeSequenceStorage s) {
2009
2010
return barr ;
2010
2011
}
2011
2012
2012
- @ Specialization
2013
+ @ Specialization (guards = { "len(lenNode, s) == cachedLen" , "cachedLen <= 32" })
2014
+ @ ExplodeLoop
2015
+ byte [] doGenericLenCached (SequenceStorage s ,
2016
+ @ Cached CastToByteNode castToByteNode ,
2017
+ @ Cached @ SuppressWarnings ("unused" ) LenNode lenNode ,
2018
+ @ Cached ("len(lenNode, s)" ) int cachedLen ) {
2019
+ byte [] barr = new byte [cachedLen ];
2020
+ for (int i = 0 ; i < cachedLen ; i ++) {
2021
+ barr [i ] = castToByteNode .execute (getGetItemNode ().execute (s , i ));
2022
+ }
2023
+ return barr ;
2024
+ }
2025
+
2026
+ @ Specialization (replaces = "doGenericLenCached" )
2013
2027
byte [] doGeneric (SequenceStorage s ,
2014
- @ Cached PRaiseNode raiseNode ) {
2015
- if ( s instanceof ByteSequenceStorage ) {
2016
- return doByteSequenceStorage (( ByteSequenceStorage ) s ) ;
2017
- } else if ( s instanceof NativeSequenceStorage && isByteStorage (( NativeSequenceStorage ) s ) ) {
2018
- return doNativeByte (( NativeSequenceStorage ) s );
2028
+ @ Cached CastToByteNode castToByteNode ,
2029
+ @ Cached LenNode lenNode ) {
2030
+ byte [] barr = new byte [ lenNode . execute ( s )] ;
2031
+ for ( int i = 0 ; i < barr . length ; i ++ ) {
2032
+ barr [ i ] = castToByteNode . execute ( getGetItemNode (). execute ( s , i ) );
2019
2033
}
2020
- throw raiseNode . raise ( TypeError , "expected a bytes-like object" ) ;
2034
+ return barr ;
2021
2035
}
2022
2036
2023
2037
private static byte [] exactCopy (byte [] barr , int len ) {
@@ -2032,6 +2046,10 @@ protected GetItemScalarNode getGetItemNode() {
2032
2046
return getItemNode ;
2033
2047
}
2034
2048
2049
+ protected static int len (LenNode lenNode , SequenceStorage s ) {
2050
+ return lenNode .execute (s );
2051
+ }
2052
+
2035
2053
public static ToByteArrayNode create () {
2036
2054
return ToByteArrayNodeGen .create (true );
2037
2055
}
0 commit comments