Closed juergenpabel closed 3 years ago
I initially tried to cache the imported module but that didn't work; keeping the class reference is probably also clearer for readability. As for side-effects: the only scenario I can think of is if an implementation maintains state in its class attributes that weren't really meant as class attributes. With the prior implementation the class gets pristinely loaded from the module, but now it (and its class state) is preserved. This new behaviour is probably more along what plugin devs would expect anyhow.
This implements a caching mechanism to Utils.get_dynamic_class_instantiation() by storing a reference to the to-be-instantiad class in a directory in Utils.klass_cache with the package_path as the key. This should be a side-effect free change that optimizes runtime for class instantiations where those classes maintain state in class attributes.
My specific use case is the vosk STT plugin, which (now) stores its (~50MB) model in a class attribute in order to reduce class instantiation time for each STT pass (https://github.com/juergenpabel/kalliope-vosk/commit/6d637097cc4da7102da7755851eab490375cdc15). Only the first instantation takes about 1-2 secs on a pi4, subsequent instantiations are now instantanious.