Skip to content

Optimize chat message storage with circular buffer implementation #6

@BP602

Description

@BP602

Problem

Current chat message storage uses inefficient array mutations that create memory pressure and performance issues:

  • [...messages, newMessage] copies entire message arrays on every new message
  • For 600 messages: 600+ object copies per message addition
  • Creates garbage collection pressure in busy chats (10+ msg/sec)
  • Memory can grow to 4.5GB+ during extended use

Current Implementation Issues

  1. Array mutation: Line 1510 in ChatProvider.jsx creates new arrays constantly
  2. Sorting overhead: O(n log n) sort on every message when mixing optimistic/confirmed
  3. Multiple array copies: Trimming creates additional array copies via slice()
  4. Memory fragmentation: Scattered allocations instead of fixed buffers

Proposed Solution

Implement a circular buffer architecture:

class CircularBuffer {
  constructor(size) {
    this.buffer = new Array(size);
    this.size = size;
    this.head = 0;
    this.count = 0;
  }
  
  add(message) {
    this.buffer[this.head] = message;           // O(1)
    this.head = (this.head + 1) % this.size;   // O(1)
    this.count = Math.min(this.count + 1, this.size);
  }
}

Benefits

  • O(1) insertion vs current O(n) array copy
  • Fixed memory footprint - no growing/shrinking arrays
  • Eliminates garbage collection pressure
  • Better cache locality for message access
  • Scales to higher message limits without performance degradation

Files to Modify

  • src/renderer/src/providers/ChatProvider.jsx - Replace array-based storage
  • Add circular buffer utility class
  • Update message access patterns for rendering

Acceptance Criteria

  • Messages stored in fixed-size circular buffers
  • O(1) message insertion performance
  • Memory usage remains stable during extended use
  • All existing functionality preserved (paused mode, trimming, etc.)
  • Performance improvement measurable in busy chats

This optimization will resolve the fundamental architecture issue causing memory leaks and enable higher message limits without performance degradation.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions