[MN-05] Tokenizer Initialization is Expensive #4

Open
opened 2026-05-05 12:15:03 +00:00 by mjasin · 0 comments
Owner

Full Detail: KnowledgeService.cs creates a new TiktokenTokenizer instance in its constructor (line 44). Since KnowledgeService is registered as Scoped, a new tokenizer is initialized for every request/session, which is CPU and memory intensive.

Action:

  • Refactor _tokenizer to be a static field in KnowledgeService.
  • Alternatively, register Tokenizer as a Singleton in Infrastructure/DependencyInjection.cs and inject it.
**Full Detail:** `KnowledgeService.cs` creates a new `TiktokenTokenizer` instance in its constructor (line 44). Since `KnowledgeService` is registered as Scoped, a new tokenizer is initialized for every request/session, which is CPU and memory intensive. **Action:** - Refactor `_tokenizer` to be a `static` field in `KnowledgeService`. - Alternatively, register `Tokenizer` as a `Singleton` in `Infrastructure/DependencyInjection.cs` and inject it.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mjasin/Nexus.Reader#4