Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

samhvw8
Copy link

@samhvw8 samhvw8 commented Apr 22, 2025

Improve performance by offloading token counting to a dedicated Web Worker. This prevents blocking the main thread during token calculations.

Key changes:

Add token-counter worker with optimized Tiktoken usage Implement robust WorkerManager with error handling and retries Update base provider to use worker for token counting Configure esbuild to properly bundle worker code

Context

because of Current token counter that make Roo un-responsive when we have large input & output token

image

image

ref: https://discord.com/channels/1332146336664915968/1364053939045007391

Implementation

(nodejs is still single thread using worker to improve performance)

Screenshots

before after

How to Test

Get in Touch


Important

Offloads token counting to a Web Worker architecture, improving performance by preventing main thread blocking.

  • Behavior:
    • Offloads token counting to token-counter.worker.ts to prevent main thread blocking.
    • Updates BaseProvider to use WorkerManager for token counting.
  • Worker Management:
    • Introduces WorkerManager in WorkerManager.ts for managing worker lifecycle, error handling, and retries.
  • Build Configuration:
    • Updates esbuild.js to bundle token-counter.worker.ts with workerConfig.
  • Misc:
    • Adds error handling and cleanup in BaseProvider and WorkerManager.

This description was created by Ellipsis for 3942456. You can customize this summary. It will automatically update as commits are pushed.

Copy link

changeset-bot bot commented Apr 22, 2025

⚠️ No Changeset found

Latest commit: 3ad7adf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 22, 2025
Copy link

ellipsis-dev bot commented Apr 22, 2025

The changes in this pull request are focused on improving token counting performance by moving it to a Web Worker architecture. The modifications across different files are interconnected and support this architectural shift. Therefore, it is not necessary to split this pull request into smaller ones, as the changes are part of a cohesive feature enhancement.

src/api/providers/base-provider.ts Show resolved Hide resolved
src/api/providers/base-provider.ts Outdated Show resolved Hide resolved
retryDelay: config.retryDelay ?? 1000,
}

const absolutePath = path.resolve(__dirname, workerPath)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolution of the worker file path using __dirname may not correctly locate the worker in a production build. If WorkerManager is compiled into a subdirectory (like dist/services/workers), appending a relative path like workers/token-counter.worker.js might resolve incorrectly. Consider revising the path computation or using an absolute path.

return worker
}

private async handleWorkerError(workerId: string, config: WorkerConfig): Promise<void> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The retry logic in handleWorkerError only terminates and deletes the failing worker without automatically reinitializing it. Consider either restarting the worker after the delay or document that subsequent calls to initializeWorker will create a new instance.

This comment was generated because it violated a code review rule: mrule_aQsEnH8jWdOfHq2Z.

@dosubot dosubot bot added the enhancement New feature or request label Apr 22, 2025
@cte
Copy link
Collaborator

cte commented Apr 22, 2025

Ah, interesting idea - will play around with it!

@hannesrudolph hannesrudolph moved this from New to PR [Pre Approval Review] in Roo Code Roadmap Apr 22, 2025
Improve performance by offloading token counting to a dedicated Web Worker.
This prevents blocking the main thread during token calculations.

Key changes:

Add token-counter worker with optimized Tiktoken usage
Implement robust WorkerManager with error handling and retries
Update base provider to use worker for token counting
Configure esbuild to properly bundle worker code
@samhvw8 samhvw8 force-pushed the feat/improve-token-count-perf branch from 3942456 to 5d414dd Compare April 22, 2025 17:24
Move token counting logic to a dedicated worker thread for improved performance.
Adds support for both text and image content blocks using cl100k_base encoding.
The worker is lazily initialized and reused across requests.

Key changes:

Create token-counter.worker.ts for threaded token counting
Update BaseProvider to use worker for countTokens method
Add proper cleanup and error handling
Support image token estimation
@samhvw8 samhvw8 force-pushed the feat/improve-token-count-perf branch from 9d1c2ef to f4ac42c Compare April 22, 2025 18:06
@shyndman
Copy link

I patched this into a local Roo build, with hopes that it would help with the frequent and lengthy pauses I've been encountering while using the extension.

It worked like a charm. I've been coding for about 16 hours with the patch, and not so much as a hiccup.

Copy link

ellipsis-dev bot commented Apr 29, 2025

PR #2848 moves token counting to Web Workers to prevent UI blocking. Tested by @shyndman with 16 hours of usage, reporting no issues. Uses token-counter.worker.ts and WorkerManager for management and error handling.

base-provider.ts | token counting implementation location

@samhvw8
Copy link
Author

samhvw8 commented Apr 29, 2025

I patched this into a local Roo build, with hopes that it would help with the frequent and lengthy pauses I've been encountering while using the extension.

It worked like a charm. I've been coding for about 16 hours with the patch, and not so much as a hiccup.

Thanks for your review @shyndman 💪

@cte cte mentioned this pull request Apr 29, 2025
@samhvw8 samhvw8 closed this Apr 30, 2025
@github-project-automation github-project-automation bot moved this from PR [Pre Approval Review] to Done in Roo Code Roadmap Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.