File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
starlark/src/values/typing/type_compiled Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,21 @@ pub(crate) fn register_eval_type(globals: &mut GlobalsBuilder) {
36
36
}
37
37
38
38
/// Check if a value matches the given type.
39
+ ///
40
+ /// This operation can be very fast or very slow depending on how it is used.
41
+ ///
42
+ /// `isinstance(x, list)` is very fast,
43
+ /// because it is compiled to a special bytecode instruction.
44
+ ///
45
+ /// `isinstance(x, list[str])` is `O(N)` operation
46
+ /// because it checks every element in this list.
47
+ ///
48
+ /// `L = list; [isinstance(x, L) for x in y]` is slow when `L` is not a constant:
49
+ /// `isinstance()` first converts `list` to a type in a loop, which is slow.
50
+ ///
51
+ /// But last operation can be optimized like this:
52
+ /// `L = eval_type(list); [isinstance(x, L) for x in y]`:
53
+ /// `eval_type()` converts `list` value into prepared type matcher.
39
54
fn isinstance < ' v > (
40
55
#[ starlark( require = pos) ] value : Value < ' v > ,
41
56
#[ starlark( require = pos) ] ty : ValueOfUnchecked < ' v , AbstractType > ,
You can’t perform that action at this time.
0 commit comments