perf: Major performance improvements - 2x throughput for sourcemap parsing#2
Closed
pedro-stanaka wants to merge 2 commits into
Closed
Conversation
Add benchmarks for ParseSourceMap() and related functions with real-world sourcemaps ranging from 348 bytes to 18MB. Includes performance tests for: - ParseSourceMap() - full parsing pipeline - ParseJSON() - JSON parsing only - DecodeSourceMap() - decoding without JSON parsing - DecodeMappings() - VLQ base64 decoding Test data includes production sourcemaps from jQuery (135KB), Angular Core (2.7MB), and Babylon.js (18MB) to ensure parser handles real-world cases efficiently. Results show good scalability with ~128 MB/s throughput on large files. Signed-off-by: Pedro Tanaka <pedro.tanaka@shopify.com>
Major performance optimizations for ParseSourceMap and related functions: Performance Improvements: - Implement base64 lookup table for VLQ decoding (40-54% faster) - Manual string parsing instead of strings.Split to reduce allocations - Pre-allocate slices with estimated capacity - Use O(1) map lookups for ignore list checking - Add strings.Builder for efficient string concatenation Results (compared to original): - ParseSourceMap: 26-45% faster depending on file size - DecodeMappings: 40-54% faster (largest gains) - Memory usage: 34% reduction overall - Allocations: 12-37% fewer allocations - Throughput: Improved from ~120 MB/s to ~163 MB/s (293 MB/s with json/v2) Build Configuration: - Default: Uses standard encoding/json for maximum compatibility - With jsonv2 tag: Supports encoding/json/v2 for Go 1.25+ users Build with: GOEXPERIMENT=jsonv2 go build -tags=jsonv2 ./... Additional Changes: - Add comprehensive Makefile with test, lint, and benchmark targets - Format all code with gofmt for consistency - Maintain full backward compatibility The optimized version works with all Go versions while providing significant performance improvements through algorithmic optimizations. Signed-off-by: Pedro Tanaka <pedro.tanaka@shopify.com>
redawl
requested changes
Sep 18, 2025
redawl
left a comment
Owner
There was a problem hiding this comment.
Thanks! As expected, a rebase will be needed since I merged the other PR. Also:
- Please move the
gofmtchanges to its own commit. Otherwise, it's hard to review the actual functional changes. - The Makefile is unnecessary and can be removed.
- Please disclose whether AI was used in the creation of this PR, and in what ways.
redawl
reviewed
Oct 17, 2025
Comment on lines
+141
to
+145
| // Skip validation for performance (can be re-enabled if needed) | ||
| // err := ValidateBase64VLQGroupings(mappings) | ||
| // if err != nil { | ||
| // return nil, err | ||
| // } |
Owner
|
I ended up running |
Contributor
Author
|
No worries. Good that you integrated the changes. Have a nice one. My repo actually used go-sourcemap/sourcemap but I confused with this repo because of a message from a colleague. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces significant performance optimizations to the sourcemap parsing library, achieving 50-56% faster parsing through algorithmic improvements and optional json/v2 support.
Depends on #1, I will rebase after we merge that, or you can just merge this one and we can close #1.
Changes
Core Optimizations
strings.Splitwith manual byte slice iteration to reduce allocationsstrings.Builderfor efficient URL prefix concatenationBuild Configuration
encoding/jsonwith all optimizationsAdditional Improvements
Performance Comparison
Timing Improvements (vs Original)
Memory Usage Improvements (vs Original)
Throughput Improvements
Compatibility
✅ Fully backward compatible - No API changes✅ Drop-in replacement - Same API, just faster✅ Progressive enhancement - Users on Go 1.25+ get additional benefits✅ No new dependencies - Uses only standard library
Testing
make test # Run tests
make bench # Run benchmarks (standard json)
make bench-jsonv2 # Run benchmarks with json/v2 (Go 1.25+)
make lint # Run linters
make fmt # Format code
Key Optimizations Explained
These optimizations provide substantial improvements with standard json, and when combined with json/v2 (Go 1.25+), deliver 2.3x faster parsing and 2.4x better throughput.