Skip to content

Module no longer copies the initial values of its variables - #2103

Open
tombouctou wants to merge 3 commits into
masterfrom
module-shares-initial-values
Open

tombouctou wants to merge 3 commits into
masterfrom
module-shares-initial-values

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

Module[{s = value}, ...] stored a deep copy of value as the variable's value, at every call. The copy is not needed:

The copy cost as much as the value is large. Combinatorica's UnionSet starts with Module[{..., set = s}, ...] on the whole union-find list, so MinimumSpanningTree copied that list at every union.

Change: Module stores the value itself.

Measurement: locally, MinimumSpanningTree on a 20×20 grid takes 9.6 s instead of 13.2 s, with the other MATH-425 changes in place.

Tests: test/builtin/test_module_values.py.

  • The initial value's elements are not copied; this fails on master.
  • Changing the variable with Part assignment, nested Part, AppendTo, two variables from one value, or recursion never changes the value it started from.

The full pytest run and the doctests pass.

🤖 Generated with Claude Code

tombouctou and others added 3 commits November 4, 2026 17:31
Module[{s = value}, ...] stored a deep copy of value as the variable's
value, at every call. The copy is not needed: x = value stores the value
itself, and what changes a variable's value (Part assignment, AppendTo,
...) makes a new value and leaves the old one as it was (#2084).

The copy cost as much as the value is large. Combinatorica's UnionSet
starts with Module[{..., set = s}, ...] on the whole union-find list, so
MinimumSpanningTree copied that list at every union: on a 20x20 grid it
takes 9.6 s instead of 13.2 s (with the other MATH-425 changes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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