PowerShell / ProjectMercury

An interactive shell to work with AI-powered assistance providers
MIT License
32 stars 5 forks source link

Fix the startup perf issue for the `openai-gpt` agent #154

Closed daxian-dbw closed 1 month ago

daxian-dbw commented 1 month ago

PR Summary

Fix the startup perf issue for the openai-gpt agent.

The bottleneck turns out to be the call to GptEncoding.GetEncoding(string encodingName) from the SharpToken package. The first call to that method will trigger the initialization of its internal state, which may involve large table creation and etc.

The startup doesn't immediately require the encoding information, so the fix is to call GptEncoding.GetEncoding on a background thread, so the result will be ready when encoding is actually needed. This reduces the deserialization of setting file from about 2500 ms for cold start and 2000 ms for warm start to about 280 ms for cold start and 20 ms for warm start.

I also changed the serialization/deserialization of the setting file to use source generator in System.Text.Json, to further improve the startup time (~67 ms gain comparing to the reflection-based for cold start, and ~8 ms gain for warm start).