Skip to content

⚡ Bolt: optimize unique random number generation#102

Open
babelman97 wants to merge 1 commit into
mainfrom
bolt-optimize-random-gen-2622278185721710192
Open

⚡ Bolt: optimize unique random number generation#102
babelman97 wants to merge 1 commit into
mainfrom
bolt-optimize-random-gen-2622278185721710192

Conversation

@babelman97

Copy link
Copy Markdown
Owner

💡 What

Implemented a hybrid sampling strategy in random.html for unique random number generation.

  • Low density (< 50%): Uses rejection sampling with a Set.
  • High density (>= 50%): Uses a Sparse Fisher-Yates shuffle algorithm with a Map.
  • Switched DOM updates from innerText to textContent.
  • Added CSS max-height and overflow-y: auto to the results container.
  • Added range validation and a 10,000,000 limit safety cap.

🎯 Why

Rejection sampling suffers from the "coupon collector's problem" where collisions become extremely frequent as the sample count approaches the range size, leading to $O(N^2)$ or worse performance. This caused the browser to hang for several seconds when generating large unique sets.

📊 Impact

  • Logic performance: Internal JS execution for 999,999 numbers in a 1,000,000 range improved by ~6.5x (from ~2600ms to ~400ms).
  • Total performance: Total browser time (including DOM rendering) for 1M numbers reduced from ~14.5s to ~5.5s.
  • UX: The UI no longer breaks or hangs when displaying millions of digits, thanks to CSS containment and scrollable results.

🔬 Measurement

Verified using a Playwright benchmarking script measuring performance.now() for internal logic and total time until the DOM reflects the results. Screenshot random_optimized.png shows the new scrollable UI.


PR created automatically by Jules for task 2622278185721710192 started by @babelman97

Implemented a hybrid sampling algorithm in random.html to address performance collapse at high densities. Added Sparse Fisher-Yates shuffle for samples >= 50% of range and improved UI responsiveness for large result sets.

- Replaced rejection sampling with Sparse Fisher-Yates (Map-based) for high density
- Switched from innerText to textContent for faster DOM updates
- Added input validation and a 10M range safety cap
- Enhanced CSS for result container with scrolling and overflow handling
- Logic speedup: ~6.5x (400ms vs 2600ms for 1M range)
- Total UI speedup: ~2.6x (5.5s vs 14.5s total time)

Co-authored-by: babelman97 <[email protected]>
@google-labs-jules

Copy link
Copy Markdown

👋 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