Skip to content

Use Base.IdDict instead of objectid to store subexpressions - #4204

Closed
odow wants to merge 1 commit into
masterfrom
od/hash
Closed

Use Base.IdDict instead of objectid to store subexpressions#4204
odow wants to merge 1 commit into
masterfrom
od/hash

Conversation

@odow

@odow odow commented Jul 22, 2026

Copy link
Copy Markdown
Member

Closes #4203

I didn't realise objectid could be re-used, but I guess it makes sense. It's not documented very well.

But this is exactly what I didn't want to happen, because we don't want to keep every single subexpression alive for the life of the model. That could be a big increase in memory usage.

One option is to make the subexpression storage opt-in.

@odow

odow commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

I would have much preferred that we just exploited common subexpressions within a single call to moi_function: #4032 (comment)

Then there's no model-level state that we are tracking.

@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.96%. Comparing base (62b9c47) to head (22f4ae5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4204      +/-   ##
==========================================
+ Coverage   99.95%   99.96%   +0.01%     
==========================================
  Files          43       43              
  Lines        6306     6313       +7     
==========================================
+ Hits         6303     6311       +8     
+ Misses          3        2       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AayushSabharwal

Copy link
Copy Markdown
Contributor

To avoid keeping expressions alive, something like a WeakKeyDict is useful. It uses WeakRef to store the keys, allowing the GC to collect them, and a finalizer to remove it from the dictionary when they get cleaned up. However, the implementation in Base is pretty slow iirc. SymbolicUtils uses WeakCacheSets.jl for hashconsing. It's a set not a dict, so it isn't directly applicable here, but it illustrates the sort of approach that we found to work well in such scenarios.

@odow

odow commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

I need to:

@odow

odow commented Jul 27, 2026

Copy link
Copy Markdown
Member Author

Closing because I don't like this. At minimum, we can do something like #4207. Or maybe WeakRef + @expression.

@odow odow closed this Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Potential hash collision for nonlinear subexpressions

2 participants