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

unmarshal: optimize slice capacity growth in unmarshalArray function #87

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

semihbkgr
Copy link
Contributor

fixes #86

Copy link
Owner

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it. Thank you :)

@DHowett
Copy link
Owner

DHowett commented Jan 27, 2025

I should note before merging, though, that this doesn't meet the criteria you laid out in #86 - when the cap is 5, it still grows to 10 rather than 8.

Is that still acceptable?

@semihbkgr
Copy link
Contributor Author

If the slice has already been allocated with an underlying array (e.g., the slice capacity is 5), and the parsed elements cannot fit into the current capacity (e.g., 7 elements), then the capacity should grow by doubling (resulting in a new capacity of 10).

However, if the slice has not been allocated yet (e.g., it is nil, so the slice capacity is 0), then the slice should grow logarithmically. For example, when the array has 7 elements, the slice capacity should be 8.

This is actually what I planned with these changes.

@DHowett
Copy link
Owner

DHowett commented Jan 27, 2025

Ah, perfect

@semihbkgr
Copy link
Contributor Author

This is also the same as how encoding/json behaves when unmarshaling slices

@DHowett DHowett merged commit 90368fb into DHowett:main Jan 27, 2025
1 check passed
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.

Incorrect slice capacity growth when unmarshaling arrays
2 participants