mypy
eabda0ee - [mypyc] Add experimental C extension librt.vecs (part 1/2) (#20653)

Commit
137 days ago
[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: https://github.com/mypyc/mypyc/issues/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.
Author
Parents
Loading