[Proposal] change FactoredMatric.svd() so it doesn't prevent all instances of FactoredMatrix from being garbage collected #796
Labels
complexity-moderate
Moderately complicated issues for people who have intermediate experience with the code
Proposal
FactoredMatrix.svd
is annotated with@lru_cache
. This prevents instances of FactoredMatrix from ever being garbage collected, as explained here: https://rednafi.com/python/lru_cache_on_methods/This post also gives a solution, which is to remove the annotation on the method definition, and instead do
Additional context
There is more discussion of the issue in this stackoverflow post.
The python docs also have a faq entry
The solution above still creates a cyclical reference, so requires cycle detection by the garbage collector.
Alternative solution
In this specific case it probably makes more sense to use the
@cached_property
-decorator from functools instead, as it is in the standard library and designed to be used per instance. It avoids a cyclic reference.Or if for backwards compatibility one wants svd to remain a method and not a property, then one could do:
Note that svd being a method is a bit inconsistent anyways, as
U
,S
,Vh
are properties.Also,
eigenvalues
could be a@cached_property
as well.Checklist
The text was updated successfully, but these errors were encountered: