Skip to content

pre-allocate for Set() new component for lower memory usage #107

Description

@JoshuaC215

Extra allocations are done when using an empty bytes.Buffer here in createInsertComponents(). bytes.Buffer uses a normal grow strategy of successively doubling in size. This could be avoided by counting the length of the needed keys and the final set value and starting with a Buffer that is about the right size.

Activity

  1. buger commented on Jun 21, 2017

    @buger
    Owner

    Yes, it is a good idea!

    I also think in terms of implementing multiple key set which will lead us to full marshaling implementation. When we set multiple keys, it make the first iteration, and find all keys first, and estimate how big new buffer should be, and on second iteration fills it with data.

  2. JoshuaC215 commented on Jun 21, 2017

    @JoshuaC215
    ContributorAuthor

    Actually I dug into source and realized the bytes.Buffer provides a 64 byte initial slice for writing into. Given that, I think it's a tossup whether the complexity is worth it to pre-allocate.

  3. buger commented on Jul 28, 2026

    @buger
    Owner

    Fixed in v1.4.0 — Set now pre-allocates the output buffer using calcAllocateSpace, reducing allocations from 6 to 1 per call. See: https://github.com/buger/jsonparser/releases/tag/v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions