Thanks to visit codestin.com
Credit goes to github.com

Skip to content

librt: Support specifying vec capacity during construction#21304

Open
JukkaL wants to merge 13 commits intomasterfrom
mypyc-vec-cap
Open

librt: Support specifying vec capacity during construction#21304
JukkaL wants to merge 13 commits intomasterfrom
mypyc-vec-cap

Conversation

@JukkaL
Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL commented Apr 23, 2026

Use e.g. vec[i64](capacity=n) to reserve capacity in the buffer during construction. This can be used to reduce the number of buffer reallocs when appending items. Capacity is the length of buffer, while length is the number of items currently stored in the buffer.

I used coding agent assist but did this in multiple smaller increments.

@JukkaL
Copy link
Copy Markdown
Collaborator Author

JukkaL commented Apr 23, 2026

mypy_primer failures may be unrelated.

Comment on lines +372 to +375
len(expr.args) == 1
and expr.arg_kinds == [ARG_POS]
or len(expr.args) == 2
and expr.arg_kinds == [ARG_POS, ARG_NAMED]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

formatting makes this expression a little confusing. add parenthesis so it's `elif ((... and ...) or (... and ...))?

PyObject *VecNested_FromIterable(size_t item_type, size_t depth, PyObject *iterable) {
VecNested v = vec_alloc(0, item_type, depth);
PyObject *VecNested_FromIterable(size_t item_type, size_t depth, PyObject *iterable, int64_t cap) {
VecNested v = vec_alloc(cap > 0 ? cap : 0, item_type, depth);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could remove the ternary expression here and just pass cap i think since cap < 0 is rejected before FromIterable is called. same in other FromIterable functions.

old = v
v = append(v, 'c')
old = append(old, 'Q')
assert v[0] == 'a'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

seems better to check v[2] here since we would expect v to be ['a', 'b', 'c'] and old to be ['a', 'b', 'Q'].

@github-actions
Copy link
Copy Markdown
Contributor

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

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