Skip to content

Update mutable list reallocation#4103

Open
d-torrance wants to merge 5 commits intoMacaulay2:developmentfrom
d-torrance:mutable-list-growth
Open

Update mutable list reallocation#4103
d-torrance wants to merge 5 commits intoMacaulay2:developmentfrom
d-torrance:mutable-list-growth

Conversation

@d-torrance
Copy link
Member

@d-torrance d-torrance commented Jan 23, 2026

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 = i constructs 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 x now has two lengths:

  • length(x.v) -- the "visible" length at top level
  • x.capacity -- the actual length allocated for the underlying sequence

Edit: Originally, I had these swapped, but that broke pretty much everything, since so much code depends on x.v having exactly the same elements as x.

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

i1 : x = new MutableList

o1 = MutableList{}

o1 : MutableList

i2 : elapsedTime for i to 99999 do x##x = i
 -- 10.8305s elapsed

After

i2 : elapsedTime for i to 99999 do x##x = i
 -- .0493995s elapsed

Closes: #659

@d-torrance d-torrance force-pushed the mutable-list-growth branch 5 times, most recently from 1cd998d to 0deeea0 Compare January 23, 2026 22:07
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
@d-torrance d-torrance marked this pull request as ready for review January 24, 2026 12:36
hash:hash_t,
Mutable:bool
Mutable:bool,
capacity:int -- amount of allocated memory (for mutable lists)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it add one word to every list (mutable or not)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it does. It's redundant for immutable lists since this will always agree with the length of the underlying sequence.

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