Skip to content

⚡ Bolt: Optimize character matching speed in luaparse.js#26

Open
ericbfriday wants to merge 1 commit into
masterfrom
bolt-optimize-indexof-9511892538594208665
Open

⚡ Bolt: Optimize character matching speed in luaparse.js#26
ericbfriday wants to merge 1 commit into
masterfrom
bolt-optimize-indexof-9511892538594208665

Conversation

@ericbfriday
Copy link
Copy Markdown
Owner

💡 What: Optimized character lookups in luaparse.js by replacing String.prototype.indexOf checks with strict equality (===) and charCodeAt logic.

🎯 Why: In hot execution paths inside lexers and parsers, single character lookups using string indexing methods like 'xX'.indexOf(char) >= 0 are relatively slow. Using .charCodeAt() directly avoids allocating new string primitives for single characters and operates faster. Out-of-bounds .charCodeAt() returns NaN, which safely acts as false when evaluating strict equality ===, eliminating the need for || null fallback checks.

📊 Impact: The npm run bench:luast benchmarks show an improvement in parser throughput:

  • Before: ~292 ops/sec
  • After: ~302 ops/sec
    This results in roughly a 3.4% overall increase in raw parse speed. Since it is in a core tokenization loop, the impact aggregates well.

🔬 Measurement:

  • Run npm run test to verify parser compliance and ensuring there's zero functional regressions.
  • Run npm run bench:luast to witness the speedups.

PR created automatically by Jules for task 9511892538594208665 started by @ericbfriday

@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.

Copilot AI review requested due to automatic review settings May 23, 2026 00:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes hot-path character matching in luaparse.js by replacing several indexOf-based membership checks with direct equality / charCodeAt comparisons, aiming to improve lexer/parser throughput.

Changes:

  • Replaced several single-character '<chars>'.indexOf(x) >= 0 checks with === comparisons and charCodeAt() comparisons.
  • Updated unary operator and table field-separator checks to use explicit equality comparisons.
  • Added an internal .jules/bolt.md note documenting the optimization rationale.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
luaparse.js Reworks several character-membership checks in numeric literal parsing and a couple parser/utility checks for speed.
.jules/bolt.md Adds documentation/notes about the micro-optimization approach and intended usage guidance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread luaparse.js
Comment on lines +1038 to 1040
if ((input.charCodeAt(index) === 105 || input.charCodeAt(index) === 73)) {
++index;
return true;
Comment thread luaparse.js
Comment on lines +1052 to +1056
if ((input.charCodeAt(index) === 117 || input.charCodeAt(index) === 85)) {
++index;
if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
if ((input.charCodeAt(index) === 108 || input.charCodeAt(index) === 76)) {
++index;
if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
if ((input.charCodeAt(index) === 108 || input.charCodeAt(index) === 76)) {
Comment thread luaparse.js
Comment on lines +1067 to 1070
} else if ((input.charCodeAt(index) === 108 || input.charCodeAt(index) === 76)) {
++index;
if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
if ((input.charCodeAt(index) === 108 || input.charCodeAt(index) === 76)) {
++index;
Comment thread luaparse.js
Comment on lines +1122 to 1128
if ((input.charCodeAt(index) === 112 || input.charCodeAt(index) === 80)) {
foundBinaryExponent = true;
++index;

// Sign part is optional and defaults to 1 (positive).
if ('+-'.indexOf(input.charAt(index) || null) >= 0)
if ((input.charCodeAt(index) === 43 || input.charCodeAt(index) === 45))
binarySign = ('+' === input.charAt(index++)) ? 1 : -1;
Comment thread .jules/bolt.md
@@ -0,0 +1,3 @@
## 2024-05-23 - luaparse string matching optimization
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