161161import com .oracle .truffle .api .interop .UnsupportedMessageException ;
162162import com .oracle .truffle .api .interop .UnsupportedTypeException ;
163163import com .oracle .truffle .api .library .CachedLibrary ;
164+ import com .oracle .truffle .api .nodes .ExplodeLoop ;
164165import com .oracle .truffle .api .nodes .Node ;
165166import com .oracle .truffle .api .profiles .BranchProfile ;
166167import com .oracle .truffle .api .profiles .ConditionProfile ;
@@ -2009,15 +2010,28 @@ byte[] doNativeByte(NativeSequenceStorage s) {
20092010 return barr ;
20102011 }
20112012
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" )
20132027 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 ) );
20192033 }
2020- throw raiseNode . raise ( TypeError , "expected a bytes-like object" ) ;
2034+ return barr ;
20212035 }
20222036
20232037 private static byte [] exactCopy (byte [] barr , int len ) {
@@ -2032,6 +2046,10 @@ protected GetItemScalarNode getGetItemNode() {
20322046 return getItemNode ;
20332047 }
20342048
2049+ protected static int len (LenNode lenNode , SequenceStorage s ) {
2050+ return lenNode .execute (s );
2051+ }
2052+
20352053 public static ToByteArrayNode create () {
20362054 return ToByteArrayNodeGen .create (true );
20372055 }
0 commit comments