botbuilder-python
botbuilder-python copied to clipboard
port: Add top level lazy load on LanguageGeneratorManager (#6123)
The changes in Add top level lazy load on LanguageGeneratorManager (#6123) may need to be ported to maintain parity with microsoft/botbuilder-dotnet.
Fixes #6116Description
- Implements lazy loading of lg files to reduce cold startup. LG files are now loaded and cached as needed throughout the bot usage and not all at startup time.
- LG files that contain
exportsare always loaded on startup since they can be accessed anywhere (customers should try to avoid using too many exports to reduce cold startup.- Added type caching to InterfaceConverter to ensure types are instantiated only once while loading the root dialog and dependencies when the bot starts
- Removed loadOnConstructor parameter from
LanguageGeneratorManagerandTemplateEngineLanguageGeneratorconstructors. Lazy instances are used across the board when posible and there is no option to override this (it is not necessary).Additional details
We tested this code against some test bots to measure the startup time and here are the results:
Bot Startup time with 4.15.1 Startup time with optimizations Notes 74 dialogs, 7 languages ~32 secs ~7 secs Higher startup time compared to others due to 35 files using @exports(5 dialogs in 7 languages)24 dialogs, 3 languages ~2 mins 20 secs ~2 secs Bot doesn't use the @exportslg feature141 dialogs, 3 languages > 1 hr 30 mins ~9.5 secs Bot doesn't use the @exportslg featurePerformance improves drastically because of these changes, however, number of dialogs and extensively use of '@exports` will continue to affect bot's cold start.
Breaking changes
This PR introduces some minor breaking changes that should not impact composer bots that haven't been customized, customized bots will be impacted only if they used any of the following methods and properties:
- Removed
loadOnConstructorparameter fromTemplateEngineLanguageGeneratorconstructor- Removed
loadOnConstructorparameter fromLanguageGeneratorManagerconstructorLanguageGeneratorManager.LanguageGeneratorsproperty type was changed fromConcurrentDictionary<string, LanguageGenerator>toConcurrentDictionary<string, Lazy<LanguageGenerator>>and the property is now read only.MultiLanguageGenerator.LanguageGeneratorsproperty type was changed fromConcurrentDictionary<string, LanguageGenerator>toConcurrentDictionary<string, Lazy<LanguageGenerator>>and the property is now read only.MultiLanguageGeneratorBase.TryGetGenerator(DialogContext dialogContext, string locale, out LanguageGenerator generator)method signature was changed toMultiLanguageGeneratorBase.TryGetGenerator(DialogContext dialogContext, string locale, out Lazy<LanguageGenerator> generator). The classes that derive from it have been updated accordingly.
Please review and, if necessary, port the changes.