Skip to content

perf(intellij): implement Gson singleton service to reduce memory allocation #8452

@houssemzaier

Description

@houssemzaier

Description

The IntelliJ plugin currently creates new Gson instances repeatedly throughout the codebase. This pattern is an anti-pattern for heavy objects in IntelliJ plugin development and causes unnecessary performance overhead:

  • Memory waste: Each Gson instance requires significant memory allocation due to reflection-based type adapter initialization
  • GC pressure: Short-lived Gson objects increase garbage collection frequency
  • CPU overhead: Repeated reflection operations on every instantiation
  • Against IntelliJ best practices: Heavy objects should follow the Light Service pattern

Current State

Common problematic patterns in our Kotlin codebase:

  • Config file parsing: Gson() created per file read
  • Message handling: Gson() created per IDE ↔ Core communication
  • JCEF bridge: Gson() created per React ↔ Kotlin message exchange
  • State persistence: Gson() created for each save/load operation

Proposed Solution

Implement JsonService as an IntelliJ Light Service following platform best practices:

  1. Create application-scoped singleton service with @Service(Service.Level.APP) annotation
  2. Provide two Gson instances:
    • Standard compact formatter for production use
    • Pretty-printed formatter for debugging and config files
  3. Add convenience methods for common JSON operations
  4. Migrate all existing Gson() and GsonBuilder().create() calls to use the singleton

Implementation Location:

  • Service: extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/services/JsonService.kt
  • Pattern: Light Service (zero XML configuration, auto-discovered by platform)
  • Scope: Application-level (single instance per IDE session)
  • Thread-safety: Gson is inherently thread-safe

Expected Benefits

  • Reduced memory footprint: Single initialization instead of repeated allocations
  • Lower GC pressure: Fewer short-lived objects
  • Better CPU efficiency: Reflection-based initialization done once
  • Platform compliance: Follows IntelliJ Platform SDK recommendations for resource-intensive objects

References

Implementation Plan

I'm willing to implement this optimization and will submit a PR after this issue is reviewed. The implementation will:

  • Create the JsonService Light Service
  • Migrate all existing Gson instantiations
  • Ensure no functional regressions (all tests pass)
  • Follow Continue's contribution guidelines

Labels: performance, enhancement, intellij
Priority: Medium-High

Metadata

Metadata

Assignees

Labels

ide:jetbrainsRelates specifically to JetBrains extensionkind:enhancementIndicates a new feature request, imrovement, or extension

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions