[Feature] Replacing encoding/json with https://github.com/bytedance/sonic #992

Closed
opened 2025-12-29 02:27:08 +01:00 by adam · 3 comments
Owner

Originally created by @JanisCasper on GitHub (Mar 31, 2025).

Use case

Performance enhancement

Description

Hi,
we've been evaluating Headscale for our company for a while and started to look for quick wins to improve the performance of the server. One bottleneck we stumbled upon is the significant amount of CPU time spent on JSON parsing operations. Although part of the standard library of Go, encoding/json is not the best-performing JSON parser (see benchmarks like https://github.com/go-json-experiment/jsonbench ).
Would it be worth discussing a switch to Sonic? We've conducted some internal load tests, and the results are quite promising.
I’m happy to create a matching PR but wanted to reach out to you first to discuss the matter.

Contribution

  • I can write the design doc for this feature
  • I can contribute this feature

How can it be implemented?

No response

Originally created by @JanisCasper on GitHub (Mar 31, 2025). ### Use case Performance enhancement ### Description Hi, we've been evaluating Headscale for our company for a while and started to look for quick wins to improve the performance of the server. One bottleneck we stumbled upon is the significant amount of CPU time spent on JSON parsing operations. Although part of the standard library of Go, encoding/json is not the best-performing JSON parser (see benchmarks like https://github.com/go-json-experiment/jsonbench ). Would it be worth discussing a switch to Sonic? We've conducted some internal load tests, and the results are quite promising. I’m happy to create a matching PR but wanted to reach out to you first to discuss the matter. ### Contribution - [ ] I can write the design doc for this feature - [ ] I can contribute this feature ### How can it be implemented? _No response_
adam added the enhancement label 2025-12-29 02:27:08 +01:00
adam closed this issue 2025-12-29 02:27:10 +01:00
Author
Owner

@kradalby commented on GitHub (Mar 31, 2025):

One bottleneck we stumbled upon is the significant amount of CPU time spent on JSON parsing operations.

We are aware of this bottleneck, it is because we use the database for storage when the application is running, even if all the data could be kept in memory. It is somewhere on the roadmap to rearchitecture this, but we have a lot to do before.

Although part of the standard library of Go, encoding/json is not the best-performing JSON parser (see benchmarks like go-json-experiment/jsonbench ).

The value of the stdlib is stability over time, headscale isnt really a performance project, as often stated, its targeted at home labbers.

Would it be worth discussing a switch to Sonic?

I dont think it is something we would consider. Sonic, as an example, is not pure Go, contains ARM and C code which we cannot look at, evaluate and which would dramatically complicate our builds.

I think these sort of large unknown "short term" wins would not really be beneficial and rather promote us to not address the actual issue.

@kradalby commented on GitHub (Mar 31, 2025): > One bottleneck we stumbled upon is the significant amount of CPU time spent on JSON parsing operations. We are aware of this bottleneck, it is because we use the database for storage when the application is running, even if all the data could be kept in memory. It is _somewhere_ on the roadmap to rearchitecture this, but we have a lot to do before. > Although part of the standard library of Go, encoding/json is not the best-performing JSON parser (see benchmarks like [go-json-experiment/jsonbench](https://github.com/go-json-experiment/jsonbench) ). The value of the stdlib is stability over time, headscale isnt really a performance project, as often stated, its targeted at home labbers. > Would it be worth discussing a switch to Sonic? I dont think it is something we would consider. Sonic, as an example, is not pure Go, contains ARM and C code which we cannot look at, evaluate and which would dramatically complicate our builds. I think these sort of large unknown "short term" wins would not really be beneficial and rather promote us to not address the actual issue.
Author
Owner

@JanisCasper commented on GitHub (Apr 1, 2025):

Allright, thanks a lot for answering!
Would you guys be open to contributions (from us) to the rearchitecturing?

@JanisCasper commented on GitHub (Apr 1, 2025): Allright, thanks a lot for answering! Would you guys be open to contributions (from us) to the rearchitecturing?
Author
Owner

@kradalby commented on GitHub (Apr 14, 2025):

We are open to contributions that help, but help on larger work where things touch each other, maybe other than code review is a bit hard. In general the overhead of planning quickly seem to defeat the purpose. But independent work or small things (like https://github.com/juanfont/headscale/pull/2514 and https://github.com/juanfont/headscale/pull/2509) is easier.

@kradalby commented on GitHub (Apr 14, 2025): We are open to contributions that help, but help on larger work where things touch each other, maybe other than code review is a bit hard. In general the overhead of planning quickly seem to defeat the purpose. But independent work or small things (like https://github.com/juanfont/headscale/pull/2514 and https://github.com/juanfont/headscale/pull/2509) is easier.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/headscale#992