Eliminate exception-based hashtable lookups in solver hot paths#1976
Eliminate exception-based hashtable lookups in solver hot paths#1976
Conversation
…ap in pat_exp.ml Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/e9e6f40d-2d85-4c11-a73a-8870b1bc3fea Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/e9e6f40d-2d85-4c11-a73a-8870b1bc3fea Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
src/solver/td3.ml
Outdated
| destabilize_normal; | ||
|
|
||
| let sys_change = S.sys_change (fun v -> try HM.find rho v with Not_found -> S.Dom.bot ()) in | ||
| let sys_change = S.sys_change (fun v -> HM.find_default rho v (S.Dom.bot ())) in |
There was a problem hiding this comment.
There's another catch with ones like this: this now always calls bot (), whereas previously it was only called when actually necessary.
There's a reason why Batteries has some *_delayed functions for some things.
There was a problem hiding this comment.
@copilot See if batteries has something like this and if it does use that.
There was a problem hiding this comment.
Batteries' Hashtbl.S does not have a find_default_delayed (or equivalent lazy) variant. The find_default usages with S.Dom.bot () have been reverted to try HM.find ... with Not_found -> S.Dom.bot () to preserve the lazy evaluation semantics. The VS.empty cases remain as find_default since VS.empty is a constant (commit c001b0f).
There was a problem hiding this comment.
@copilot: As there are so many of these, define a helper function find_default_delayed from find_opt and BatOption.default_delayed and use that.
There was a problem hiding this comment.
Done in commit ca2e46a. Added find_default_delayed h k f = Option.default_delayed f (HM.find_option h k) to each affected solver module and replaced all try HM.find ... with Not_found -> S.Dom.bot () patterns (9 total across td3.ml, topDown.ml, topDown_term.ml, topDown_space_cache_term.ml). In topDown.ml the helper is defined over HPM (the pair hashtable) since the single S.Dom.bot () case there uses that table.
There was a problem hiding this comment.
Maybe it should just be in GobHashtbl.
It might require some interface changes, so it might be easier to benchmark this first and then clean up if it turns out to be beneficial.
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/549bf1a5-1caf-4b0a-b8e3-fadb8c217dea Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/ad9206be-ceef-4040-9820-00b29c798cba Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
…und patterns Agent-Logs-Url: https://github.com/goblint/analyzer/sessions/c0745a92-b9e7-48ca-962c-ee3a8c19c6e7 Co-authored-by: michael-schwarz <13812333+michael-schwarz@users.noreply.github.com>
Audit for common OCaml performance anti-patterns found in the solver fixpoint iteration loops.
Changes
Solver files:
try HM.find … with Not_found→HM.find_default(constant defaults)The pattern below appears across solver modules (
td3.ml,topDown.ml,topDown_term.ml,topDown_space_cache_term.ml,topDown_deprecated.ml,sLR.ml,sLRterm.ml,sLRphased.ml,effectWConEq.ml,worklist.ml):These are in the innermost fixpoint iteration loops — called millions of times per analysis.
find_defaultavoids the exception-raise overhead on every cache miss and was already used inconsistently in some places.This replacement is applied only where the default value is a constant (e.g.
VS.empty).Solver files:
try HM.find … with Not_found→find_default_delayed(function-call defaults)For cases where the default is a function call (e.g.
S.Dom.bot ()), a local helper is defined in each affected module and used instead:This preserves lazy evaluation semantics —
S.Dom.botis only called when the key is absent — while avoiding exception-based control flow. The helper is added totd3.ml,topDown.ml(for theHPMpair hashtable),topDown_term.ml, andtopDown_space_cache_term.ml.