Skip to content

Eliminate exception-based hashtable lookups in solver hot paths#1976

Draft
Copilot wants to merge 6 commits intomasterfrom
copilot/audit-performance-bottlenecks
Draft

Eliminate exception-based hashtable lookups in solver hot paths#1976
Copilot wants to merge 6 commits intomasterfrom
copilot/audit-performance-bottlenecks

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Audit for common OCaml performance anti-patterns found in the solver fixpoint iteration loops.

Changes

Solver files: try HM.find … with Not_foundHM.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):

(* before *)
HM.replace infl y (VS.add x (try HM.find infl y with Not_found -> VS.empty))

(* after *)
HM.replace infl y (VS.add x (HM.find_default infl y VS.empty))

These are in the innermost fixpoint iteration loops — called millions of times per analysis. find_default avoids 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_foundfind_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:

let find_default_delayed h k f = Option.default_delayed f (HM.find_option h k)
(* before *)
let old = try HM.find rho' y with Not_found -> S.Dom.bot () in

(* after *)
let old = find_default_delayed rho' y S.Dom.bot in

This preserves lazy evaluation semantics — S.Dom.bot is only called when the key is absent — while avoiding exception-based control flow. The helper is added to td3.ml, topDown.ml (for the HPM pair hashtable), topDown_term.ml, and topDown_space_cache_term.ml.

Copilot AI and others added 2 commits April 1, 2026 09:09
…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>
Copilot AI changed the title [WIP] Check for performance anti-patterns in OCaml code Eliminate exception-based hashtable lookups and redundant list allocations in solver hot paths Apr 1, 2026
Copilot AI requested a review from michael-schwarz April 1, 2026 09:16
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot See if batteries has something like this and if it does use that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 1, 2026

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:

  • ocaml-batteries-team.github.io
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

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>
Copilot AI changed the title Eliminate exception-based hashtable lookups and redundant list allocations in solver hot paths Eliminate exception-based hashtable lookups in solver hot paths Apr 1, 2026
…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>
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.

Audit for performance bottlenecks

3 participants