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

types/basetypes: Reduce memory allocations of (ObjectValue).ToTerraformValue() #791

Merged
merged 2 commits into from
Jul 5, 2023

Conversation

bflad
Copy link
Contributor

@bflad bflad commented Jul 3, 2023

Reference: #775

The prior implementation used the exported AttributeTypes() method, which in this case was unnecessarily creating a copy of the map when it was only be used for walking the existing data. At scale, such as handling 1000+ objects, this creates unnecessary pressure on the Go garbage collector.

benchstat comparison with new benchmark test:

goos: darwin
goarch: arm64
pkg: github.com/hashicorp/terraform-plugin-framework/types/basetypes
                                   │  original   │              proposed               │
                                   │   sec/op    │   sec/op     vs base                │
ObjectValueToTerraformValue1000-10   408.4µ ± 1%   318.4µ ± 1%  -22.02% (p=0.000 n=10)

                                   │   original   │               proposed               │
                                   │     B/op     │     B/op      vs base                │
ObjectValueToTerraformValue1000-10   449.2Ki ± 0%   286.6Ki ± 0%  -36.19% (p=0.000 n=10)

                                   │  original   │              proposed              │
                                   │  allocs/op  │  allocs/op   vs base               │
ObjectValueToTerraformValue1000-10   2.051k ± 0%   2.020k ± 0%  -1.51% (p=0.000 n=10)

…rmValue()

Reference: #775

The prior implementation used the exported `AttributeTypes()` method, which in this case was unnecessarily creating a copy of the map when it was only be used for walking the existing data. At scale, such as handling 1000+ objects, this creates unnecessary pressure on the Go garbage collector.

`benchstat` comparison with new benchmark test:

```text
goos: darwin
goarch: arm64
pkg: github.com/hashicorp/terraform-plugin-framework/types/basetypes
                                   │  original   │              proposed               │
                                   │   sec/op    │   sec/op     vs base                │
ObjectValueToTerraformValue1000-10   408.4µ ± 1%   318.4µ ± 1%  -22.02% (p=0.000 n=10)

                                   │   original   │               proposed               │
                                   │     B/op     │     B/op      vs base                │
ObjectValueToTerraformValue1000-10   449.2Ki ± 0%   286.6Ki ± 0%  -36.19% (p=0.000 n=10)

                                   │  original   │              proposed              │
                                   │  allocs/op  │  allocs/op   vs base               │
ObjectValueToTerraformValue1000-10   2.051k ± 0%   2.020k ± 0%  -1.51% (p=0.000 n=10)
```
@bflad bflad added the bug Something isn't working label Jul 3, 2023
@bflad bflad added this to the v1.4.0 milestone Jul 3, 2023
@bflad bflad requested a review from a team as a code owner July 3, 2023 16:39
Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@bflad bflad merged commit 9fe2ca5 into main Jul 5, 2023
@bflad bflad deleted the bflad/object-optimization branch July 5, 2023 14:53
@bflad bflad modified the milestones: v1.4.0, v1.3.3 Jul 20, 2023
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants