Skip to content

Enable json v2 support for 59% faster sourcemap parsing#20

Merged
vmihailenco merged 1 commit into
go-sourcemap:masterfrom
pedro-stanaka:feat/improve-perf-json
Oct 1, 2025
Merged

Enable json v2 support for 59% faster sourcemap parsing#20
vmihailenco merged 1 commit into
go-sourcemap:masterfrom
pedro-stanaka:feat/improve-perf-json

Conversation

@pedro-stanaka

@pedro-stanaka pedro-stanaka commented Sep 29, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for the experimental encoding/json/v2 package through build tags, providing significant performance improvements for sourcemap parsing while maintaining full backward compatibility.

Changes

  • Add unmarshalJSON() abstraction layer to switch between JSON implementations
  • Create json_decoder.go for standard encoding/json (default)
  • Create json_decoder_v2.go with jsonv2 build tag for Go 1.25+
  • Replace 2 json.Unmarshal calls with the abstraction in consumer.go
  • Minor bonus: Use strings.Builder for more efficient URL concatenation

Performance Improvements

Benchmarks show 50-74% faster parsing across different sourcemap sizes:

Sourcemap Size Standard JSON JSON/v2 Improvement
Small (5KB) 66 MB/s 115 MB/s +74%
Medium (100KB) 104 MB/s 176 MB/s +69%
Large (500KB) 113 MB/s 180 MB/s +59%
XLarge (2.5MB) 125 MB/s 189 MB/s +51%
XXLarge (10MB) 118 MB/s 187 MB/s +59%

Key Metrics

  • Throughput: 118 MB/s → 187 MB/s (59% improvement on large files)
  • Allocations: 444 → 413 (7% fewer)
  • Memory usage: Roughly the same

Usage

The change is opt-in and requires no code changes from users:

# Standard build (default - uses encoding/json)
go build ./...

# With json/v2 optimization (Go 1.25+)
GOEXPERIMENT=jsonv2 go build -tags=jsonv2 ./...

Benchmarks

I have comprehensive benchmarks that demonstrate these improvements across various sourcemap sizes (from 5KB to 10MB). If you're interested, I can submit them as a follow-up/preamble PR. The benchmarks include:

  • Synthetic sourcemap generator for consistent testing
  • Performance tests across 6 different file sizes
  • Memory allocation analysis
  • Throughput measurements in MB/s

Why This Matters

Many modern web applications produce large sourcemaps (2-20MB is common), and parsing performance directly impacts:

  • Build tool performance
  • Error reporting services
  • Development server response times
  • CI/CD pipeline speeds

This simple change provides substantial performance improvements with zero risk to existing users.

References

Add experimental json/v2 decoder support via build tags for improved performance
while maintaining full backward compatibility.

- Add unmarshalJSON abstraction in consumer.go
- Create json_decoder.go for standard encoding/json (default)
- Create json_decoder_v2.go with jsonv2 build tag for Go 1.25+
- Bonus: Minor optimization using strings.Builder for URL concatenation

Users can opt-in to json/v2 with:
  GOEXPERIMENT=jsonv2 go build -tags=jsonv2 ./...

This provides ~59% faster parsing with zero API changes.
@pedro-stanaka pedro-stanaka marked this pull request as ready for review September 29, 2025 14:52
@vmihailenco vmihailenco merged commit 7941718 into go-sourcemap:master Oct 1, 2025
@vmihailenco

Copy link
Copy Markdown
Member

Looks good, thanks 👍

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