Commit eabda0e
authored
[mypyc] Add experimental C extension librt.vecs (part 1/2) (#20653)
The extension defines the `vec[t]` type and associated functions. `vec`
is a sequence type optimized for use in compiled code. `vec` values are
immutable and represented internally as a struct with a length and
buffer fields. Only the buffer object is mutable. This allows very fast
access to the length field in compiled code, but any operations that
change the length of a `vec` value must return a modified `vec` value
(e.g. `append`).
Related issue: mypyc/mypyc#840.
This PR doesn't define any mypyc primitives -- only use via generic
Python operations is supported at this point. I have a local branch that
implements efficient mypyc primitives for vecs, and there are
significant performance gains over `list` objects in benchmarks.
This PR only adds support for item types `i64`, `i32`, `i16`, `u8`,
`float` and `bool` (e.g. `vec[i32]`). A follow-up PR will add support
for reference item types (e.g. `vec[str]`) and nested vecs.
See the comment at the top of `mypyc/lib-rt/vecs/librt_vecs.c` for a
more detailed description. It also documents some implementation details
of reference item types and nested vec types (which are not included yet
in this PR).
Currently a fairly minimal API is supported. This example shows some of
the supported operations:
```
from librt.vecs import vec, append, remove, pop
v = vec[i32]([2, 3])
v[1] = v[0] + 2
v = append(v, 4)
v = remove(v, 2)
v, x = pop(v)
```
Additionally, read-only slicing and equality is supported. The `in`
operator and iteration only work through the sequence API, but I will
add proper support for these later on.1 parent e711be3 commit eabda0e
17 files changed
Lines changed: 2845 additions & 0 deletions
File tree
- mypyc
- ir
- lib-rt
- vecs
- test-data
- fixtures
- test
- mypy/typeshed/stubs/librt/librt
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
107 | 121 | | |
108 | 122 | | |
109 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
135 | 149 | | |
136 | 150 | | |
0 commit comments