Implement egg-style deferred rebuilding algorithm - #158
Conversation
- Add worklist to EGraph for deferred congruence restoration - Update merge() to defer repair by adding to worklist - Add rebuild() method to process worklist - Refactor _repair() to repair() with egg-style algorithm - Update tests to call rebuild() after merge operations - All tests pass with 100% coverage Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR implements egg-style deferred rebuilding for the E-Graph, replacing the immediate upward merging strategy with a worklist-based approach that defers congruence restoration until rebuild() is explicitly called.
Key changes:
- Added worklist tracking to defer congruence restoration work
- Modified
merge()to add affected E-classes to the worklist instead of immediately repairing - Introduced new public
rebuild()method to process the worklist and restore congruence - Refactored
repair()(previously_repair()) to remove the internal loop, with recursion now handled via the worklist
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| egg/apyds_egg/init.py | Core implementation changes: added worklist field, modified merge() to defer rebuilding, added rebuild() method, and refactored repair() to work with the worklist-based approach |
| egg/tests/test_egraph.py | Updated congruence tests to call rebuild() after merge operations to properly test the deferred rebuilding behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if canon in new_parents: | ||
| self.merge(peclass, new_parents[canon]) |
There was a problem hiding this comment.
The repair() method is calling self.merge() at line 210, which adds items to self.worklist. However, since repair() is called from within rebuild() which is already processing the worklist, this could lead to correctness issues if the worklist handling isn't carefully managed. While the current implementation appears correct (as the worklist is cleared and rebuilt in each iteration), the recursive nature of calling merge() from within repair() should be clearly documented. Consider adding a comment explaining that merge operations during repair are safe because the worklist is rebuilt in each iteration of rebuild().
Replaces immediate upward merging with egg-style deferred rebuilding for better performance and explicit control over congruence restoration.
Changes
merge(): Defers repair by adding to worklist rather than calling repair immediatelyrebuild(): New public method processes worklist until congruence is restored_repair()torepair(): Removed internalwhile changed:loop; recursion now handled via worklistAlgorithm Difference
Before (immediate):
After (deferred):
Enables batching multiple merges before a single rebuild, reducing redundant work. Tests updated to call
rebuild()after merge operations.Original prompt
This section details on the original issue you should resolve
<issue_title>使用rebuild优化egg库</issue_title>
<issue_description>使用rebuild优化的egg 算法demo:
目前的版本所依据的算法demo :