Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

LanguageUtilities created new GlobalLang, WindowsAiLang, and list instances on every call, causing unnecessary memory allocations in hot paths like OCR operations.

Changes

New LanguageService (Text-Grab/Services/LanguageService.cs)

  • Caches available OCR languages list on first call
  • Caches current input language, invalidates when keyboard language tag changes
  • Caches OCR language selection based on LastUsedLang setting
  • Caches system translation language string
  • Static WindowsAiLang instance reused across calls
  • Thread-safe with lock synchronization
  • Exposes InvalidateLanguagesCache(), InvalidateOcrLanguageCache(), InvalidateAllCaches() for manual invalidation

Updated LanguageUtilities

  • Delegates to Singleton<LanguageService>.Instance
  • Full backward compatibility—no call site changes required
// Before: allocates new GlobalLang every call
public static ILanguage GetCurrentInputLanguage()
{
    string inputLang = InputLanguageManager.Current.CurrentInputLanguage.Name;
    return new GlobalLang(inputLang);
}

// After: returns cached instance, only allocates when language changes
public static ILanguage GetCurrentInputLanguage()
    => Singleton<LanguageService>.Instance.GetCurrentInputLanguage();

Tests (Tests/LanguageServiceTests.cs)

  • Coverage for GetLanguageTag, GetLanguageKind, singleton behavior, and backward compatibility
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor Language Utilities to be a Service to improve lower string memory allocations for every call</issue_title>
<issue_description>Use a more efficient pattern for querying the languages available. Also cache and query

  • Selected Text Grab language and setting
  • Keyboard language
  • System language</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Refactor language utilities to improve memory allocations Refactor LanguageUtilities to cached LanguageService singleton Jan 26, 2026
Copilot AI requested a review from TheJoeFin January 26, 2026 23:15
Refactored serialization options, singleton pattern, and method caching for better reliability and maintainability. Enhanced test clarity and code structure.
Refactored to cache dictionaries and regexes as static fields, reducing unnecessary allocations and improving efficiency without altering core logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants