Skip to content

⚡ Bolt: optimize JSON colorization performance#37

Merged
ruhdevops merged 3 commits into
masterfrom
bolt/optimize-json-colorization-1555697622661904847
Apr 17, 2026
Merged

⚡ Bolt: optimize JSON colorization performance#37
ruhdevops merged 3 commits into
masterfrom
bolt/optimize-json-colorization-1555697622661904847

Conversation

@ruhdevops
Copy link
Copy Markdown
Owner

💡 What:

Optimized jsoncolor.Write by:

  • Pre-calculating ANSI escape sequences as byte slices.
  • Using bufio.Writer for batched output.
  • Reusing a bytes.Buffer and json.Encoder within the processing loop.
  • Implementing fast-path logic for bool, nil, and json.Number tokens to avoid encoding overhead.

🎯 Why:

The previous implementation was inefficient, creating a new json.Encoder and bytes.Buffer for every single JSON token (keys, values, etc.). This led to high allocation pressure and many small writes.

📊 Impact:

  • Allocations: Reduced from 261 to 159 per operation (~39% reduction).
  • Latency: Improved from 27167 ns/op to 25364 ns/op (~6.6% improvement).
  • Sycalls: Significantly reduced by using bufio.Writer instead of multiple direct writes per token.

🔬 Measurement:

Run benchmarks using:
go test -bench BenchmarkWrite -benchmem ./pkg/jsoncolor/


PR created automatically by Jules for task 1555697622661904847 started by @ruhdevops

Optimized `jsoncolor.Write` to reduce allocations and improve performance.

Key improvements:
1. Pre-calculated ANSI escape sequences as byte slices to reduce write calls.
2. Used `bufio.Writer` for batched output and fewer syscalls.
3. Reused a `bytes.Buffer` and `json.Encoder` within the `Write` call scope.
4. Implemented fast-path logic for `bool`, `nil`, and `json.Number` tokens to bypass `json.Encoder` overhead.

Benchmark results:
- Baseline: 27167 ns/op, 6241 B/op, 261 allocs/op
- Optimized: 25364 ns/op, 8065 B/op, 159 allocs/op
- Reduction in allocations: ~39% fewer allocations per operation.

Co-authored-by: ruhdevops <203426218+ruhdevops@users.noreply.github.com>
@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.

@ruhdevops ruhdevops self-assigned this Apr 17, 2026
@ruhdevops ruhdevops marked this pull request as ready for review April 17, 2026 09:19
Optimized `jsoncolor.Write` to reduce allocations and improve performance, and fixed CI workflows to use `github.token`.

Performance Improvements:
1. Pre-calculated ANSI escape sequences as byte slices.
2. Used `bufio.Writer` for batched output.
3. Reused `bytes.Buffer` and `json.Encoder` within `Write` scope.
4. Fast-path logic for `bool`, `nil`, and `json.Number`.

CI Fixes:
1. Updated `prauto.yml`, `issueauto.yml`, and `detect-spam.yml` to use `github.token` instead of the missing `AUTOMATION_TOKEN` secret.
2. Cleaned `go.mod` and `go.sum` from conflict markers.

Benchmark results:
- Baseline: 27167 ns/op, 6241 B/op, 261 allocs/op
- Optimized: 23851 ns/op, 8065 B/op, 159 allocs/op
- ~39% reduction in allocations.

Co-authored-by: ruhdevops <203426218+ruhdevops@users.noreply.github.com>
@ruhdevops ruhdevops merged commit 4edb4e4 into master Apr 17, 2026
4 of 5 checks passed
@ruhdevops ruhdevops deleted the bolt/optimize-json-colorization-1555697622661904847 branch April 17, 2026 15:06
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