Skip to content

⚡ Bolt: optimize synthetic embedding generation#204

Draft
hackerxj2010 wants to merge 1 commit into
mainfrom
bolt-optimize-synthetic-embeddings-10231463738773884381
Draft

⚡ Bolt: optimize synthetic embedding generation#204
hackerxj2010 wants to merge 1 commit into
mainfrom
bolt-optimize-synthetic-embeddings-10231463738773884381

Conversation

@hackerxj2010
Copy link
Copy Markdown
Owner

⚡ Bolt: optimize synthetic embedding generation

Identified and resolved a performance bottleneck in syntheticVector generation where SHA-256 hashing was redundantly performed for every vector dimension (e.g., 1,536 times per call).

Optimization

  • Faster PRNG: Replaced per-dimension crypto.createHash calls with a single hash seeding a Mulberry32 Pseudo-Random Number Generator.
  • Efficient Math: Optimized normalizeVector by calculating the inverse magnitude once and using multiplication instead of division.
  • Fast Rounding: Replaced .toFixed(8) with a numerical rounding pattern: Math.sign(v) * Math.round(Math.abs(v) * 1e8) / 1e8.
  • Compatibility: Maintained use of crypto.createHash to ensure stability across supported Node.js versions.

Impact

  • Throughput: Synthetic embedding generation improved from ~117 embeddings/sec to ~1818 embeddings/sec (~15x speedup).
  • Correctness: Verified with new unit tests in packages/ai/src/index.test.ts ensuring determinism and proper vector normalization.

Verification

  • Ran pnpm exec vitest run packages/ai/src/index.test.ts (All passed).
  • Ran full project test suite via pnpm test after pnpm db:generate (All passed).
  • Verified with internal benchmark script.

PR created automatically by Jules for task 10231463738773884381 started by @hackerxj2010

Identified and resolved a performance bottleneck in `syntheticVector` generation where SHA-256 hashing was redundantly performed for every vector dimension.

Changes:
- Replaced per-dimension hashing with a Mulberry32 PRNG seeded once per vector.
- Optimized `normalizeVector` using pre-calculated inverse magnitude and a faster numerical rounding pattern.
- Maintained backward compatibility by continuing to use `crypto.createHash`.
- Added unit tests to ensure determinism and normalization.

Performance Impact:
- Synthetic embedding generation throughput increased from ~117 to ~1818 embeddings/sec (~15x improvement).
- Vector normalization is significantly more efficient in hot paths.

Co-authored-by: hackerxj2010 <198651211+hackerxj2010@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant