Skip to content

Performance: Optimize IPC memory allocations with stack buffers#7

Open
devin-ai-integration[bot] wants to merge 1 commit intomasterfrom
devin/1752839697-performance-improvements
Open

Performance: Optimize IPC memory allocations with stack buffers#7
devin-ai-integration[bot] wants to merge 1 commit intomasterfrom
devin/1752839697-performance-improvements

Conversation

@devin-ai-integration
Copy link

@devin-ai-integration devin-ai-integration bot commented Jul 18, 2025

Performance: Optimize IPC memory allocations with stack buffers

Summary

This PR optimizes the most critical performance bottleneck in dictationbridge-core: repeated dynamic memory allocations in the IPC layer. Every text insertion and deletion event was triggering new/delete operations, causing unnecessary heap fragmentation and allocation overhead.

Key Changes:

  • inproc/ipc.cpp: Replaced dynamic allocations in SendTextInsertedEvent() and SendTextDeletedEvent() with 1KB stack buffers
  • Fallback mechanism: Large messages (>1KB) still use heap allocation for safety
  • PERFORMANCE_ANALYSIS.md: Comprehensive analysis documenting all performance issues found in the codebase

Performance Impact: Eliminates heap allocations for typical dictation text chunks, reducing memory fragmentation and function call latency in the hot path that processes every dictation event.

Review & Testing Checklist for Human

  • Build verification: Confirm project compiles successfully with Visual Studio 2015 and SCons
  • Basic functionality test: Verify dictation still works correctly with both Windows Speech Recognition and Dragon NaturallySpeaking
  • Stack buffer size validation: Check that 1KB is appropriate for typical dictation text lengths (review existing logs/data if available)
  • Memory safety review: Verify no buffer overflow risks exist with the stack allocation approach
  • Performance testing: Measure actual performance improvement during active dictation sessions

Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    Master["master/main.cpp<br/>Message Processing"]:::context
    IPC["inproc/ipc.cpp<br/>IPC Layer"]:::major-edit
    Hooks["inproc/wordhooks_impl.cpp<br/>Word Event Hooks"]:::context
    UserHooks["inproc/userhooks.cpp<br/>User Input Hooks"]:::context
    Client["client/main.cpp<br/>Command Client"]:::context
    
    Hooks -->|"SendTextInsertedEvent()"| IPC
    UserHooks -->|"SendTextDeletedEvent()"| IPC
    Client -->|"SendCommand()"| Master
    IPC -->|"WM_COPYDATA"| Master
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit  
        L3[Context/No Edit]:::context
    end
    
    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB
    classDef context fill:#FFFFFF
Loading

Notes

Risk Assessment: Medium risk due to changes in critical hot path, but logic is straightforward.

Why this optimization matters: The IPC functions are called for every single character/word that gets dictated, making memory allocation overhead significant during active dictation sessions.

Alternative approaches considered: Thread-local buffers, pre-allocated buffer pools, but stack allocation with heap fallback provides the best balance of simplicity and performance.

Session Info:

Future optimizations identified: The performance analysis report documents 6 additional optimization opportunities across string operations, polling patterns, and system calls that can be addressed in follow-up PRs.

- Replace dynamic allocations in SendTextInsertedEvent and SendTextDeletedEvent
- Use 1KB stack buffers for typical message sizes with heap fallback
- Eliminates heap allocations for most dictation events (< 1KB)
- Reduces memory fragmentation and improves performance in hot paths
- Add comprehensive performance analysis report

Co-Authored-By: pranav@techesoterica.com <pranav.lal@gmail.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

0 participants