Abstract
Symbolic computation systems often face performance bottlenecks due to "expression swell," where redundant mathematical subexpressions consume excessive memory and computational resources. This paper presents the integration of hash consing into JuliaSymbolics, utilizing a global weak-reference hash table to canonicalize expressions. By transforming expression trees into Directed Acyclic Graphs (DAGs), we achieve significant memory deduplication and computational speedups. Evaluation on large-scale biological and thermodynamic models demonstrates up to a 100x speedup in numerical evaluation and substantial reductions in memory allocation, highlighting the efficacy of classical deduplication techniques in modern scientific computing.
Introduction
In symbolic mathematics, expressions are typically represented as Abstract Syntax Trees (ASTs). As these expressions undergo operations like differentiation or simplification, they often grow exponentially in size—a phenomenon known as expression swell. This redundancy leads to increased RAM usage, frequent garbage collection, and inefficient compilation. Modern applications, including neuro-symbolic AI and large-scale physical modeling, require more efficient representations to handle thousands of complex terms rapidly.
Methodology: From Trees to DAGs
To address these challenges, we implemented hash consing within the JuliaSymbolics ecosystem. Hash consing ensures that every structurally distinct immutable object is stored exactly once.
Implementation Details
- Weak-Reference Registry: We utilize a global
WeakValueDictto track all symbolic objects. Before constructing a new expression, the system performs a lookup; if the expression exists, the existing pointer is returned. The use of weak references allows the garbage collector to reclaim memory for unused expressions, preventing leaks. - Structural Canonicalization: This process converts traditional expression trees into Directed Acyclic Graphs (DAGs). In a DAG, shared subexpressions are represented by a single node in memory, rather than being duplicated across the tree.
- Inherent Common Subexpression Elimination (CSE): By maintaining a DAG structure, code generation can be performed via topological sorting. This naturally implements CSE during the construction phase, eliminating the need for a separate, computationally expensive optimization pass later in the pipeline.
Experimental Evaluation
The approach was tested on two benchmarks from the SciML repository:
- B Cell Receptor (BCR) Signaling Model: A systems biology model featuring 1,122 ODEs and over 24,000 reactions.
- X Steam Thermodynamic System: A model based on the IAPWS IF97 standard, scaled to test increasing complexity in state variables.
Results and Discussion
The integration of hash consing yielded dramatic improvements across various metrics. For the BCR model, Jacobian computation time was reduced by approximately 3.2x, with memory usage halved.
In the X Steam model, while initial hashing introduced some overhead for unique terms, the downstream benefits were substantial. Code generation and compilation times saw 5x to 10x improvements, respectively. Most notably, numerical evaluation speeds increased by 20x to 100x for larger model sizes.
The performance gains stem from two primary factors:
- Constant-Time Equality: Structural equality checks are reduced to simple pointer comparisons.
- Reduced Compilation Burden: The compiler processes a significantly smaller, deduplicated graph rather than a bloated tree.
Conclusion and Future Work
Hash consing proves to be a powerful optimization for symbolic systems, particularly those dealing with high degrees of mathematical redundancy. While it introduces overhead in workloads with unique terms, the cumulative benefits in complex modeling are undeniable.
Future research will focus on integrating hash consing with E-graphs to handle logical (rather than just structural) equivalence. Additionally, we aim to implement cross-thread caching and persistent symbolic databases to support parallel computation and long-term knowledge retrieval in AI-driven mathematical reasoning.
Summary updated