Skip to content

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

Merged
JukkaL merged 208 commits intomasterfrom
librt-vecs-extension
Jan 27, 2026
Merged

[mypyc] Add experimental C extension librt.vecs (part 1/2)#20653
JukkaL merged 208 commits intomasterfrom
librt-vecs-extension

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jan 27, 2026

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.

@github-actions

This comment has been minimized.

static PyModuleDef vecsmodule = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "vecs",
.m_doc = "vecs doc",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add TODO comment?

// as vec[i64] or vec[float]. Assume that certain #defines are provided that
// provide all the item type specific definitions:
//
// VEC vec C type (e.g. VecI64)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to also describe the other defines.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit eabda0e into master Jan 27, 2026
23 checks passed
@JukkaL JukkaL deleted the librt-vecs-extension branch January 27, 2026 16:46
JukkaL added a commit that referenced this pull request Jan 28, 2026
Add support for `vec` types with reference item types, such as
`vec[str]` or `vec[MyClass | None]`. Arbitrary union item types aren't
supported -- only optional types are accepted. Also add support for
nested vecs, such as `vec[vec[i64]]`.

No mypyc primitives are included in this PR yet. I'll add them in
follow-up PRs.

This continues the work started in #20653. Refer to that PR for a more
detailed description of the feature.

Related issue: mypyc/mypyc#840
JukkaL added a commit that referenced this pull request Feb 3, 2026
Add basic irbuild support for `vec[t]`. The runtime representation of
`vec[t]` is a C struct. This is similar to how fixed-length tuples are
represented. Multiple different structs are used, depending on the item
type (`VecI32` for `vec[i32`] and so on).

The C extension `librt.vec` that defines the `vec` type was added in
#20653 and #20656. These PRs also explain the implementation in more
detail.

Add RType subclass RVec that is used for vecs. We need a new RType
class, since primitives types can't be generic and they can't be struct
types.

This is based on an old branch, so it mostly uses old-style primitives.
I am planning to modernize some of the primitives in follow-up PRs.

This doesn't include codegen support, and irbuild test cases are only
included for `vec[i64]`. I will create follow-up PRs that add the
remaining irbuild tests, codegen support and run tests. All these tests
are are passing on my local full branch.

Related issue: mypyc/mypyc#840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants