kalliope-project / kalliope

Kalliope is a framework that will help you to create your own personal assistant.
https://kalliope-project.github.io/
GNU General Public License v3.0
1.71k stars 230 forks source link

Added caching to Utils.get_dynamic_class_instantiation() #658

Closed juergenpabel closed 3 years ago

juergenpabel commented 3 years ago

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.

juergenpabel commented 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.