Update mutable list reallocation#4103
Open
d-torrance wants to merge 5 commits intoMacaulay2:developmentfrom
Open
Update mutable list reallocation#4103d-torrance wants to merge 5 commits intoMacaulay2:developmentfrom
d-torrance wants to merge 5 commits intoMacaulay2:developmentfrom
Conversation
1cd998d to
0deeea0
Compare
The only time we ever needed a List instead of an Expr when creating a List object directly was in copy, and we only needed this in a few places, so we just use the list() constructor and copy the sequence instead. Also simplify sethash -- no need to pass is_mutable separately since that information is kept in the list. Also remove unused "newlist" function
This is to keep track of the amount that we have allocated for mutable lists.
This fixes an old issue from bugs/dan, so we remove it
0deeea0 to
c261b4a
Compare
dfd544e to
e8aec4e
Compare
antonleykin
reviewed
Feb 5, 2026
| hash:hash_t, | ||
| Mutable:bool | ||
| Mutable:bool, | ||
| capacity:int -- amount of allocated memory (for mutable lists) |
Contributor
There was a problem hiding this comment.
Does it add one word to every list (mutable or not)?
Member
Author
There was a problem hiding this comment.
Yes, it does. It's redundant for immutable lists since this will always agree with the length of the underlying sequence.
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.
Currently, when we add an element to a mutable list, we reallocate exactly enough memory to fit the new element. So doing something like
for i to n - 1 do x##x = iconstructs n brand new mutable lists.Instead, we double the length of the underlying sequence if needed. (And also halve it if it gets small from repeated calls to
remove.)So each list
xnow has two lengths:length(x.v)-- the "visible" length at top levelx.capacity-- the actual length allocated for the underlying sequenceEdit: Originally, I had these swapped, but that broke pretty much everything, since so much code depends on
x.vhaving exactly the same elements asx.For immutable lists, these two numbers should always be the same.
I changed some pretty low-level stuff, so chances are there are bugs... Draft for now
Before
After
Closes: #659