Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: improve memory allocation for marshall go map type #467

Closed
dinhxuanvu opened this issue Jan 12, 2024 · 1 comment
Closed

feature: improve memory allocation for marshall go map type #467

dinhxuanvu opened this issue Jan 12, 2024 · 1 comment

Comments

@dinhxuanvu
Copy link
Contributor

dinhxuanvu commented Jan 12, 2024

Is your feature request related to a problem? Please describe.
Marshalling go map type to CBOR is a common operation that can be expensive memory-wise of the map has a lot of fields. The code in CBOR uses map iteration that will allocate memory for each field and value. This excessive memory allocation is described in this issue on golang [1].

Describe the solution you'd like
Starting with go 1.18, there are two new methods Value.SetIterKey and Value.SetIterValue that will do fewer allocations.

Plus, the sync.Pool is also another tool that can be used to further reduce memory allocation by reusing temporary memory allocation.

Lastly, the Value.SetZero method (available since go 1.20) is helpful to release memory allocation to the GC when is no longer needed.

Additional context
The new methods SetIterKey and SetIterValue are introduced in go 1.18 and unavailable for go 1.17 or before while SetZero is available starting go 1.20. In order to maintain the backward compatibility for the CBOR codebase (if this is a concern), the code for encode map can be refractored into separate files where a go:build tag can be used to specify which files can be included in compilation for go 1.18+/1.20+ and what not.

The performance/memory improvement can be seen here (collected and analyzed by go benchmark and benchstat):

Screenshot 2024-01-12 at 12 59 00 AM
@dinhxuanvu
Copy link
Contributor Author

This feature has been completed.
/close

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

No branches or pull requests

1 participant