Repository navigation
Module no longer copies the initial values of its variables - #2103
Open
tombouctou wants to merge 3 commits into
Open
tombouctou wants to merge 3 commits into
tombouctou wants to merge 3 commits into
Conversation
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
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.
Module[{s = value}, ...]stored a deep copy ofvalueas the variable's value, at every call. The copy is not needed:x = valuestores the value itself.AppendTo, ...) makes a new value and leaves the old one as it was (Part assignment changes only the variable, never a value it shares #2084).The copy cost as much as the value is large. Combinatorica's
UnionSetstarts withModule[{..., set = s}, ...]on the whole union-find list, soMinimumSpanningTreecopied that list at every union.Change:
Modulestores the value itself.Measurement: locally,
MinimumSpanningTreeon 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.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