File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 341341# ## Constructors
342342# ##
343343
344+ """
345+ $(TYPEDSIGNATURES)
346+
347+ Implements hash consing (flyweight design pattern) for `BasicSymbolic` objects.
348+
349+ This function checks if an equivalent `BasicSymbolic` object already exists. It uses a
350+ custom hash function (`hash2`) incorporating metadata and symtypes to search for existing
351+ objects in a `WeakValueDict` (`wvd`). Due to the possibility of hash collisions (where
352+ different objects produce the same hash), a custom equality check (`isequal2`) which
353+ includes metadata comparison, is used to confirm the equivalence of objects with matching
354+ hashes. If an equivalent object is found, the existing object is returned; otherwise, the
355+ input `s` is returned. This reduces memory usage, improves compilation time for runtime
356+ code generation, and supports built-in common subexpression elimination, particularly when
357+ working with symbolic objects with metadata.
358+
359+ Using a `WeakValueDict` ensures that only weak references to `BasicSymbolic` objects are
360+ stored, allowing objects that are no longer strongly referenced to be garbage collected.
361+ Custom functions `hash2` and `isequal2` are used instead of `Base.hash` and `Base.isequal`
362+ to accommodate metadata without disrupting existing tests reliant on the original behavior
363+ of those functions.
364+ """
344365function BasicSymbolic (s:: BasicSymbolic ):: BasicSymbolic
345366 h = hash2 (s)
346367 t = get! (wvd, h, s)
You can’t perform that action at this time.
0 commit comments