librt: Support specifying vec capacity during construction#21304
Open
librt: Support specifying vec capacity during construction#21304
Conversation
Collaborator
Author
|
mypy_primer failures may be unrelated. |
p-sawicki
reviewed
Apr 23, 2026
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] |
Collaborator
There was a problem hiding this comment.
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); |
Collaborator
There was a problem hiding this comment.
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' |
Collaborator
There was a problem hiding this comment.
seems better to check v[2] here since we would expect v to be ['a', 'b', 'c'] and old to be ['a', 'b', 'Q'].
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.