Skip to content

perf: optimize string concatenation and namespace caching#230

Merged
sudo-tee merged 1 commit intosudo-tee:mainfrom
jinzhongjia:little-performance-increase
Feb 3, 2026
Merged

perf: optimize string concatenation and namespace caching#230
sudo-tee merged 1 commit intosudo-tee:mainfrom
jinzhongjia:little-performance-increase

Conversation

@jinzhongjia
Copy link
Contributor

Summary

  • Replace string concatenation in loops with table.concat in ID generation
  • Cache namespace ID at module level instead of repeated creation
    Changes
    lua/opencode/id.lua
  • random_base62(): Use table + table.concat instead of result = result .. char (O(n) vs O(n²))
  • generate_new_id(): Same optimization for timestamp byte extraction
    lua/opencode/ui/input_window.lua
  • Cache placeholder_ns at module level
  • Remove 3 redundant nvim_create_namespace('input_placeholder') calls
    Why
    String concatenation in Lua creates new string objects each iteration, causing O(n²) complexity. While the impact is minimal for short strings (26 chars), this follows better Lua practices.
    Namespace IDs are stable for the same name, but caching avoids unnecessary function calls on every placeholder refresh.

Replace inefficient string concatenation loops with table.concat to improve performance when building base62 IDs and timestamp hex strings. Cache namespace ID creation in input_window to avoid repeated API calls.
@jinzhongjia
Copy link
Contributor Author

This is a little optimize, it will increase our performance a bit

@sudo-tee
Copy link
Owner

sudo-tee commented Feb 3, 2026

The id calss is rarely used. I think it's only used for the init command.

But I am ok with it being more optimized. It was a loose port from the opencode implementation in typescript

Thanks for the PR

@sudo-tee sudo-tee merged commit 1e6c559 into sudo-tee:main Feb 3, 2026
5 checks passed
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