RasaHQ / rasa

💬 Open source machine learning framework to automate text- and voice-based conversations: NLU, dialogue management, connect to Slack, Facebook, and more - Create chatbots and voice assistants
https://rasa.com/docs/rasa/
Apache License 2.0
18.61k stars 4.6k forks source link

Cache `all_subclasses` to reduce `inspect` cost #13044

Open ottonemo opened 1 month ago

ottonemo commented 1 month ago

Hey :sunflower: We are currently experimenting with using an event broker to also do logging based on tracker contents and found that rasa.shared.utils.common.all_subclasses is having a needlessly large (100%) overhead on our runtime.

Since the parameters variations of these functions are limited to the number of slot and event classes, we can use an LRU cache to cache all calls to this function. It is also very unlikely for the result to change during runtime, so no invalidation is needed.

We think that this will also help overall performance in more common circumstances.

We conducted three tests: a baseline run, using our event broker logging and the event broker logging with the LRU cache around all_subclasses. Sample size was 10 conversations in succession for each experiment.

Results:

As you can see, all_subclasses introduced a ~100% overhead while being essentially a lookup table. It would be nice and quite helpful if this change could be merged.

Proposed changes:

Status (please check what you already did):

Profiles of the individual experiments

Baseline: Baseline profile

Event broker with logging: image

Event broker with logging & LRU cache: image