Skip to content

Microoptimization: slots for _NumVarImpl#1021

Open
IgnaceBleukx wants to merge 1 commit into
masterfrom
slots_for_numvarimpl
Open

Microoptimization: slots for _NumVarImpl#1021
IgnaceBleukx wants to merge 1 commit into
masterfrom
slots_for_numvarimpl

Conversation

@IgnaceBleukx

Copy link
Copy Markdown
Collaborator

I recently learned about using __slots__ in python objects
https://stackoverflow.com/questions/472000/usage-of-slots

The main advantage is that your objects take less memory, which may be very useful for models with loads of variables/constraints (e.g., files we read in from DIMACS?)

I've implemented it for _NumVarImpl, no real time gains, but memory footprint for a variable now goes from ±450B to ±80B which is nice : )

The real gains are probably when typing Operator and Comparison, or some of the globals (e.g., Multiplication).

@tias

tias commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

oh nice, this is some of the benefits I was thinking we could only get with mypyc, so if we can get it pure python, great.

In that SO post, it says parents also need slots, so strange this already works. But also, I wouldn't mind if 'name' becomes part of the slot of Expression, then there could be an even bigger win? or would that require using slots in all expressions?

@IgnaceBleukx

Copy link
Copy Markdown
Collaborator Author

Yes good point, we might as well have name as a slot for Expression : )

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