Repository navigation
Conversation
b1695a4 to
a0001d5
Compare
tombouctou
left a comment
There was a problem hiding this comment.
The ruff modernisation part looks fine, and CI is green. Two things in the slots part look like blockers to me, though: as written the slots make each Definition larger, not smaller, and previously saved user definitions stop loading.
Blocking
1. "__dict__" in Definition.__slots__ makes each Definition bigger
Measured on Python 3.14 against this PR's base commit (0fcb088e2), allocating 20,000 Definition("Global`q") under tracemalloc:
| Variant | bytes per Definition |
|---|---|
| base (no slots) | 808 |
this PR (slots + __dict__) |
936 |
slots with a real literal_index slot, no __dict__ |
824 |
- The only thing that needs
__dict__is theliteral_indexcache (self.__dict__.setdefault("literal_index", {})and two other places indefinitions.py). - Suggested fix: add a
literal_indexslot, initialise it toNonein__init__and create the dict lazily. I only measured the eager-dict variant (824); the lazy one should land a little under base. - Even then the saving is at best a few dozen bytes per definition: modern CPython already stores instance attributes compactly, so the memory case is weak.
- Evaluation speed showed no difference beyond noise on my machine.
2. Old pickles of user definitions no longer load
A get_user_definitions() string produced on master and fed to set_user_definitions() on this branch fails on first use:
AttributeError: 'mathics.core.definitions.Definition' object has no attribute 'ownvalues'
- Cause: the old pickle state is a plain dict, which unpickling writes into the instance
__dict__, where the slot descriptors hide it. - New-format pickles load fine on both this branch and master; only old → new is broken.
- This affects anything that persists that string across an upgrade (I believe mathics-django does so in its sessions, but I did not check that repo). The
builtin_filenamecache is only protected by its mtime check and could hit the same error. - Suggested fix: a
__setstate__that accepts both the plain dict and the(dict, slots)form. Dropping__dict__(item 1) without it would turn this into a different error rather than fix it.
Should fix
3. Typos introduced by c686a4e, all in definitions.py
in the bfield with the attributewe couldb pick the optionsautoload files,bbb and extension modules- "A Definition is a collection of
Rules and attributes are associated withSymbol" — dropping "which" broke the sentence. - "
Definitionobjects" — missing space.
4. Definitions.__slots__ without __dict__ is an API break for little gain
- There is one
Definitionsper session, so there is no memory to save. - Per-instance monkeypatching stops working, which is why
test_config_values.pyhad to patch the class instead (the patched test itself is fine). - Any front-end or pymathics module that sets an ad-hoc attribute on
definitionswill now raise. In-tree, every assignment is covered by the slot list. - I'd either drop the slots on
Definitionsor note the break inCHANGES.rst.
Minor
symbols.pystill importsFrozenSetfromtyping, now unused;Optionalis half-converted (Atom.equal2andoriginal: Optional["Atom"]remain).test/test_session.pygainsimport_and_load_builtins(). It looks unrelated to slots (presumably makes the file runnable standalone) — worth a mention in the description.- The alphabetical reordering in
__init__separates the "TraceEvaluation uses these" comment fromtrace_show_rewrite, and putsrule_indexand its comment betweenownvaluesandsubvalues. Mixing the reorder into the slots change also makes the diff harder to read. - Title typo: "definitons.py".
itertools.pairwiseandX | Noneare fine withrequires-python >= 3.11.
Two things in the slots part look like blockers to me, though: as written the slots make each The first part is what I see as more problematic. The second is just a matter of setting the right
Most of the attributes of the class are dictionaries. The part that I like about SLOTS is that prevents the (ab)use of monkeypatching. Most of these dict attributes could be packed into a "dispatch table": ownvalues are just the rules associated to the Definitions name, downvalues to those rules having the same head, upvalues are those that have the same lookup symbol, etc and so on. Then, this part of the complexity of the class would be moved to the dispatch table, and then the class could be more lightweight.
Here I would expect a larger improvement, not in memory, but in attribute access. I would like to make the cache access faster, and I expected this change do that. Have you measure it? |
Fixed in commit 963a893
Removed. This was an unintended merge mistake from a stray
I think this is technically incorrect. It happens that the two attributes just happen to fit in strict alphabetical order. I've added a space between these settings and made the comment more explicit though. |
I believe this addresses blocking concern 1. "__dict__" in Definition.__slots__ makes each Definition bigger. Not related, but we now require at least 3.12 (and tolerate 3.15) which was mostly done in PR #2125. I believe this
Right now, I don't believe we have pickle files to worry about. |
a03cdca to
87425db
Compare
I believe one of the tests relies on monkey-patching.
The one place where I don't think this is right is where we have some sort of literal pattern. Here, a dictionary lookup is in fact the right thing. Both of these come from @tombouctou's changes. But @tombouctou correct me if I am mistaken here. (I do make lots of mistakes.) |
87425db to
ab2d897
Compare
Both you @mmatera and @tombouctou are more facile at this than I am. I'd appreciate it if you'd tell me, and we'll decide what to do. Personally, I just look at CI execution times and go by that, which admittedly is not very precise. |
…cs.core.symbols Will add 3.15 when mesonpy becomes available
ab2d897 to
dda698d
Compare
|
@mmatera and @tombouctou: I think I've addressed the issues or commented on them. But as you can see, I make mistakes. (AI got its training from me.) So if I've missed something, there are further items, or something to discuss, chime in. |
|
We should care about how we are educating AI... :) Tomorrow I will try to run the benchmarks to see the impact of these changes. |
Use slots in class
Definition; "ruff" changed filesdefinition.py,symbols.py, andtest_definition.py.Additional changes to drop support for Python 3.11.
(Reworked PR from #2130)