diff --git a/src/types.jl b/src/types.jl index 91cb5c8f..8b3d0dee 100644 --- a/src/types.jl +++ b/src/types.jl @@ -341,6 +341,25 @@ end ### Constructors ### +""" +$(TYPEDSIGNATURES) + +Implements hash consing (flyweight design pattern) for `BasicSymbolic` objects. + +This function checks if an equivalent `BasicSymbolic` object already exists based on a +custom hash (`hash2`) and equality check (`isequal2`) that incorporate metadata and +symtypes. If an equivalent object is found in the `wvd` (a `WeakValueDict`), the existing +object is returned. Otherwise, the input `s` is returned. This approach reduces memory +usage, improves compilation time for runtime code generation, and supports built-in common +subexpression elimination, especially beneficial when dealing with duplicate symbolic +expressions. + +Using a `WeakValueDict` ensures that only weak references to `BasicSymbolic` objects are +stored, allowing objects that are no longer strongly referenced to be garbage collected. +The custom `hash2` and `isequal2` functions are used instead of `Base.hash` and +`Base.isequal` respectively to avoid breaking existing tests that rely on the default +behavior of these functions, which does not consider metadata. +""" function BasicSymbolic(s::BasicSymbolic)::BasicSymbolic h = hash2(s) t = get!(wvd, h, s)