85
85
import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
86
86
import com .oracle .graal .python .builtins .objects .cell .PCell ;
87
87
import com .oracle .graal .python .builtins .objects .code .PCode ;
88
+ import com .oracle .graal .python .builtins .objects .common .HashingCollectionNodes ;
89
+ import com .oracle .graal .python .builtins .objects .common .PHashingCollection ;
90
+ import com .oracle .graal .python .builtins .objects .common .SequenceNodes ;
88
91
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
89
92
import com .oracle .graal .python .builtins .objects .dict .PDict ;
90
93
import com .oracle .graal .python .builtins .objects .function .Arity ;
144
147
import com .oracle .graal .python .runtime .PythonParser .ParserMode ;
145
148
import com .oracle .graal .python .runtime .exception .PException ;
146
149
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
150
+ import com .oracle .graal .python .runtime .sequence .PSequence ;
147
151
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
148
152
import com .oracle .truffle .api .CompilerDirectives ;
149
153
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
@@ -698,6 +702,8 @@ public abstract static class IdNode extends PythonBuiltinNode {
698
702
699
703
@ Child private ReadAttributeFromObjectNode readId = null ;
700
704
@ Child private WriteAttributeToObjectNode writeId = null ;
705
+ @ Child private SequenceNodes .LenNode lenNode = null ;
706
+ @ Child private HashingCollectionNodes .LenNode setLenNode = null ;
701
707
702
708
@ SuppressWarnings ("unused" )
703
709
@ Specialization
@@ -761,11 +767,11 @@ Object doEmptyFrozenSet(@SuppressWarnings("unused") PFrozenSet value) {
761
767
}
762
768
763
769
protected boolean isEmptyImmutableBuiltin (Object object ) {
764
- return (object instanceof PTuple && PGuards . isEmpty ((PTuple ) object )) ||
770
+ return (object instanceof PTuple && isEmpty ((PTuple ) object )) ||
765
771
(object instanceof String && PGuards .isEmpty ((String ) object )) ||
766
- (object instanceof PString && PGuards . isEmpty ((PString ) object )) ||
767
- (object instanceof PBytes && PGuards . isEmpty ((PBytes ) object )) ||
768
- (object instanceof PFrozenSet && PGuards . isEmpty ((PFrozenSet ) object ));
772
+ (object instanceof PString && isEmpty ((PString ) object )) ||
773
+ (object instanceof PBytes && isEmpty ((PBytes ) object )) ||
774
+ (object instanceof PFrozenSet && isEmpty ((PFrozenSet ) object ));
769
775
}
770
776
771
777
/**
@@ -789,6 +795,22 @@ Object doId(Object obj) {
789
795
return obj .hashCode ();
790
796
}
791
797
798
+ protected boolean isEmpty (PSequence s ) {
799
+ if (lenNode == null ) {
800
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
801
+ lenNode = insert (SequenceNodes .LenNode .create ());
802
+ }
803
+ return lenNode .execute (s ) == 0 ;
804
+ }
805
+
806
+ protected boolean isEmpty (PHashingCollection s ) {
807
+ if (setLenNode == null ) {
808
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
809
+ setLenNode = insert (HashingCollectionNodes .LenNode .create ());
810
+ }
811
+ return setLenNode .execute (s ) == 0 ;
812
+ }
813
+
792
814
private Object getId (PythonObject obj ) {
793
815
if (readId == null ) {
794
816
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -815,6 +837,7 @@ public abstract static class IsInstanceNode extends PythonBuiltinNode {
815
837
@ Child private LookupAndCallBinaryNode instanceCheckNode = LookupAndCallBinaryNode .create (__INSTANCECHECK__ );
816
838
@ Child private CastToBooleanNode castToBooleanNode = CastToBooleanNode .createIfTrueNode ();
817
839
@ Child private TypeBuiltins .InstanceCheckNode typeInstanceCheckNode = TypeBuiltins .InstanceCheckNode .create ();
840
+ @ Child private SequenceStorageNodes .LenNode lenNode ;
818
841
819
842
public static IsInstanceNode create () {
820
843
return BuiltinFunctionsFactory .IsInstanceNodeFactory .create (null );
@@ -834,10 +857,10 @@ public boolean isInstance(Object instance, PythonClass cls,
834
857
return instanceClass == cls || isSubtypeNode .execute (instanceClass , cls ) || isInstanceCheckInternal (instance , cls );
835
858
}
836
859
837
- @ Specialization (guards = "clsTuple.len( ) == cachedLen" , limit = "getVariableArgumentInlineCacheLimit()" )
860
+ @ Specialization (guards = "getLength(clsTuple ) == cachedLen" , limit = "getVariableArgumentInlineCacheLimit()" )
838
861
@ ExplodeLoop
839
862
public boolean isInstanceTupleConstantLen (Object instance , PTuple clsTuple ,
840
- @ Cached ("clsTuple.len( )" ) int cachedLen ,
863
+ @ Cached ("getLength(clsTuple )" ) int cachedLen ,
841
864
@ Cached ("create()" ) IsInstanceNode isInstanceNode ) {
842
865
Object [] array = clsTuple .getArray ();
843
866
for (int i = 0 ; i < cachedLen ; i ++) {
@@ -864,6 +887,14 @@ public boolean isInstance(Object instance, PTuple clsTuple,
864
887
public boolean isInstance (Object instance , Object cls ) {
865
888
return isInstanceCheckInternal (instance , cls ) || typeInstanceCheckNode .executeWith (cls , instance );
866
889
}
890
+
891
+ protected int getLength (PTuple t ) {
892
+ if (lenNode == null ) {
893
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
894
+ lenNode = insert (SequenceStorageNodes .LenNode .create ());
895
+ }
896
+ return lenNode .execute (t .getSequenceStorage ());
897
+ }
867
898
}
868
899
869
900
// issubclass(class, classinfo)
@@ -873,6 +904,7 @@ public abstract static class IsSubClassNode extends PythonBuiltinNode {
873
904
@ Child private LookupAndCallBinaryNode subclassCheckNode = LookupAndCallBinaryNode .create (__SUBCLASSCHECK__ );
874
905
@ Child private CastToBooleanNode castToBooleanNode = CastToBooleanNode .createIfTrueNode ();
875
906
@ Child private IsSubtypeNode isSubtypeNode = IsSubtypeNode .create ();
907
+ @ Child private SequenceStorageNodes .LenNode lenNode ;
876
908
877
909
public static IsSubClassNode create () {
878
910
return BuiltinFunctionsFactory .IsSubClassNodeFactory .create (null );
@@ -885,10 +917,10 @@ private boolean isInstanceCheckInternal(Object derived, Object cls) {
885
917
886
918
public abstract boolean executeWith (Object derived , Object cls );
887
919
888
- @ Specialization (guards = "clsTuple.len( ) == cachedLen" , limit = "getVariableArgumentInlineCacheLimit()" )
920
+ @ Specialization (guards = "getLength(clsTuple ) == cachedLen" , limit = "getVariableArgumentInlineCacheLimit()" )
889
921
@ ExplodeLoop
890
922
public boolean isSubclassTupleConstantLen (Object derived , PTuple clsTuple ,
891
- @ Cached ("clsTuple.len( )" ) int cachedLen ,
923
+ @ Cached ("getLength(clsTuple )" ) int cachedLen ,
892
924
@ Cached ("create()" ) IsSubClassNode isSubclassNode ) {
893
925
Object [] array = clsTuple .getArray ();
894
926
for (int i = 0 ; i < cachedLen ; i ++) {
@@ -915,6 +947,14 @@ public boolean isSubclass(Object derived, PTuple clsTuple,
915
947
public boolean isSubclass (Object derived , Object cls ) {
916
948
return isInstanceCheckInternal (derived , cls ) || isSubtypeNode .execute (derived , cls );
917
949
}
950
+
951
+ protected int getLength (PTuple t ) {
952
+ if (lenNode == null ) {
953
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
954
+ lenNode = insert (SequenceStorageNodes .LenNode .create ());
955
+ }
956
+ return lenNode .execute (t .getSequenceStorage ());
957
+ }
918
958
}
919
959
920
960
// iter(object[, sentinel])
@@ -1098,17 +1138,18 @@ public abstract static class OrdNode extends PythonBuiltinNode {
1098
1138
@ Specialization
1099
1139
public int ord (String chr ) {
1100
1140
if (chr .length () != 1 ) {
1101
- raise (TypeError , "ord() expected a character, but string of length %d found" , chr .length ());
1141
+ throw raise (TypeError , "ord() expected a character, but string of length %d found" , chr .length ());
1102
1142
}
1103
-
1104
1143
return chr .charAt (0 );
1105
1144
}
1106
1145
1107
1146
@ Specialization
1108
1147
public int ord (PBytes chr ,
1148
+ @ Cached ("create()" ) SequenceStorageNodes .LenNode lenNode ,
1109
1149
@ Cached ("create()" ) SequenceStorageNodes .GetItemNode getItemNode ) {
1110
- if (chr .len () != 1 ) {
1111
- raise (TypeError , "ord() expected a character, but string of length %d found" , chr .len ());
1150
+ int len = lenNode .execute (chr .getSequenceStorage ());
1151
+ if (len != 1 ) {
1152
+ throw raise (TypeError , "ord() expected a character, but string of length %d found" , len );
1112
1153
}
1113
1154
1114
1155
return (byte ) getItemNode .execute (chr .getSequenceStorage (), 0 );
@@ -1122,11 +1163,12 @@ public abstract static class PrintNode extends PythonBuiltinNode {
1122
1163
@ SuppressWarnings ("unused" )
1123
1164
@ Specialization
1124
1165
public Object print (PTuple values , String sep , String end , Object file , boolean flush ,
1166
+ @ Cached ("create()" ) SequenceStorageNodes .LenNode lenNode ,
1125
1167
@ Cached ("createNotNormalized()" ) SequenceStorageNodes .GetItemNode getItemNode ,
1126
1168
@ Cached ("create(__STR__)" ) LookupAndCallUnaryNode callStr ) {
1127
1169
try {
1128
1170
PythonContext context = getContext ();
1129
- if (values .len ( ) == 0 ) {
1171
+ if (lenNode . execute ( values .getSequenceStorage () ) == 0 ) {
1130
1172
write (context , end );
1131
1173
} else {
1132
1174
SequenceStorage store = values .getSequenceStorage ();
0 commit comments