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

perf(gnolang): use slice not map for Attributes.data per usage performance #3437

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

odeke-em
Copy link
Contributor

@odeke-em odeke-em commented Jan 2, 2025

TL;DR: Despite the reduction in memory consumption, the preliminary direct benchmarks show increase in CPU time but in microseconds

 benchstat before.txt after.txt 
name                   old time/op    new time/op    delta
AttributesSetGetDel-8    12.4µs ± 0%    56.6µs ± 3%  +355.22%  (p=0.000 n=8+9)

name                   old alloc/op   new alloc/op   delta
AttributesSetGetDel-8    9.37kB ± 0%    5.38kB ± 0%   -42.61%  (p=0.000 n=10+10)

name                   old allocs/op  new allocs/op  delta
AttributesSetGetDel-8      11.0 ± 0%     107.0 ± 0%  +872.73%  (p=0.000 n=10+10)

Deep dive

Noticed in profiling stdlibs/bytes that a ton of memory was being used in maps, and that's due to the conventional CS 101 that maps with O(1) lookups, insertions and deletions beat O(n) slices' performance, but when n is small, the memory bloat is not worth it and we can use slices as evidenced in profiles for which there was 30% perceptible reduction in RAM where

  • Before:
Showing nodes accounting for 92.90MB, 83.87% of 110.76MB total
Dropped 51 nodes (cum <= 0.55MB)
Showing top 10 nodes out of 123
      flat  flat%   sum%        cum   cum%
   47.37MB 42.77% 42.77%    47.37MB 42.77%  internal/runtime/maps.newarray
   10.50MB  9.48% 52.25%    10.50MB  9.48%  internal/runtime/maps.NewEmptyMap
       8MB  7.22% 59.47%        8MB  7.22%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*StaticBlock).InitStaticBlock
    7.51MB  6.78% 66.25%    13.03MB 11.76%  github.com/gnolang/gno/gnovm/pkg/gnolang.Go2Gno
    6.02MB  5.43% 71.68%    10.73MB  9.68%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*defaultStore).SetObject
       4MB  3.61% 75.29%        4MB  3.61%  github.com/gnolang/gno/gnovm/pkg/gnolang.NewBlock
    3.47MB  3.13% 78.43%     3.47MB  3.13%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Allocator).NewDataArray
    2.52MB  2.27% 80.70%     3.52MB  3.18%  github.com/gnolang/gno/gnovm/pkg/gnolang.toKeyValueExprs
       2MB  1.81% 82.51%        2MB  1.81%  runtime.allocm
    1.51MB  1.36% 83.87%     1.51MB  1.36%  runtime/pprof.(*profMap).lookup
Showing nodes accounting for 47.37MB, 42.77% of 110.76MB total
----------------------------------------------------------+-------------
      flat  flat%   sum%        cum   cum%   calls calls% + context
----------------------------------------------------------+-------------
                                           47.37MB   100% |   internal/runtime/maps.newGroups
   47.37MB 42.77% 42.77%    47.37MB 42.77%                | internal/runtime/maps.newarray
----------------------------------------------------------+-------------
                                           32.01MB 78.05% |   github.com/gnolang/gno/gnovm/pkg/gnolang.preprocess1.func1
                                               7MB 17.07% |   github.com/gnolang/gno/gnovm/pkg/gnolang.evalConst (inline)
                                            1.50MB  3.66% |   github.com/gnolang/gno/gnovm/pkg/gnolang.constType (inline)
                                            0.50MB  1.22% |   github.com/gnolang/gno/gnovm/pkg/gnolang.tryPredefine.func1
         0     0% 42.77%    41.01MB 37.03%                | github.com/gnolang/gno/gnovm/pkg/gnolang.(*Attributes).SetAttribute
                                           41.01MB   100% |   runtime.mapassign_faststr
----------------------------------------------------------+-------------
                                            4.50MB   100% |   github.com/gnolang/gno/gnovm/pkg/test.(*TestOptions).runTestFiles
         0     0% 42.77%     4.50MB  4.06%                | github.com/gnolang/gno/gnovm/pkg/gnolang.(*Machine).RunFiles
                                            4.50MB   100% |   github.com/gnolang/gno/gnovm/pkg/gnolang.(*Machine).runFileDecls

and after:

Showing nodes accounting for 61.99MB, 73.12% of 84.78MB total
Showing top 10 nodes out of 196
      flat  flat%   sum%        cum   cum%
   19.50MB 23.00% 23.00%    19.50MB 23.00%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Attributes).SetAttribute
   12.52MB 14.76% 37.77%    18.02MB 21.26%  github.com/gnolang/gno/gnovm/pkg/gnolang.Go2Gno
    7.58MB  8.94% 46.70%     9.15MB 10.79%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*defaultStore).SetObject
       5MB  5.90% 52.60%        5MB  5.90%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*StaticBlock).InitStaticBlock
    3.47MB  4.09% 56.69%     3.47MB  4.09%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Allocator).NewDataArray
       3MB  3.54% 60.24%        3MB  3.54%  github.com/gnolang/gno/gnovm/pkg/gnolang.NewBlock
       3MB  3.54% 63.77%        3MB  3.54%  github.com/gnolang/gno/gnovm/pkg/gnolang.Nx (inline)
    2.77MB  3.26% 67.04%     2.77MB  3.26%  bytes.growSlice
    2.65MB  3.12% 70.16%     2.65MB  3.12%  internal/runtime/maps.newarray
    2.50MB  2.95% 73.12%     2.50MB  2.95%  runtime.allocm

Fixes #3436

…mance

Noticed in profiling stdlibs/bytes that a ton of memory was being
used in maps, and that's due to the conventional CS 101 that maps
with O(1) lookups, insertions and deletions beat O(n) slices'
performance, but when n is small, the memory bloat is not worth
it and we can use slices as evidenced in profiles for which
there was 30% perceptible reduction in RAM where
* Before:

```shell
Showing nodes accounting for 92.90MB, 83.87% of 110.76MB total
Dropped 51 nodes (cum <= 0.55MB)
Showing top 10 nodes out of 123
      flat  flat%   sum%        cum   cum%
   47.37MB 42.77% 42.77%    47.37MB 42.77%  internal/runtime/maps.newarray
   10.50MB  9.48% 52.25%    10.50MB  9.48%  internal/runtime/maps.NewEmptyMap
       8MB  7.22% 59.47%        8MB  7.22%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*StaticBlock).InitStaticBlock
    7.51MB  6.78% 66.25%    13.03MB 11.76%  github.com/gnolang/gno/gnovm/pkg/gnolang.Go2Gno
    6.02MB  5.43% 71.68%    10.73MB  9.68%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*defaultStore).SetObject
       4MB  3.61% 75.29%        4MB  3.61%  github.com/gnolang/gno/gnovm/pkg/gnolang.NewBlock
    3.47MB  3.13% 78.43%     3.47MB  3.13%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Allocator).NewDataArray
    2.52MB  2.27% 80.70%     3.52MB  3.18%  github.com/gnolang/gno/gnovm/pkg/gnolang.toKeyValueExprs
       2MB  1.81% 82.51%        2MB  1.81%  runtime.allocm
    1.51MB  1.36% 83.87%     1.51MB  1.36%  runtime/pprof.(*profMap).lookup
```

```shell
Showing nodes accounting for 47.37MB, 42.77% of 110.76MB total
----------------------------------------------------------+-------------
      flat  flat%   sum%        cum   cum%   calls calls% + context
----------------------------------------------------------+-------------
                                           47.37MB   100% |   internal/runtime/maps.newGroups
   47.37MB 42.77% 42.77%    47.37MB 42.77%                | internal/runtime/maps.newarray
----------------------------------------------------------+-------------
                                           32.01MB 78.05% |   github.com/gnolang/gno/gnovm/pkg/gnolang.preprocess1.func1
                                               7MB 17.07% |   github.com/gnolang/gno/gnovm/pkg/gnolang.evalConst (inline)
                                            1.50MB  3.66% |   github.com/gnolang/gno/gnovm/pkg/gnolang.constType (inline)
                                            0.50MB  1.22% |   github.com/gnolang/gno/gnovm/pkg/gnolang.tryPredefine.func1
         0     0% 42.77%    41.01MB 37.03%                | github.com/gnolang/gno/gnovm/pkg/gnolang.(*Attributes).SetAttribute
                                           41.01MB   100% |   runtime.mapassign_faststr
----------------------------------------------------------+-------------
                                            4.50MB   100% |   github.com/gnolang/gno/gnovm/pkg/test.(*TestOptions).runTestFiles
         0     0% 42.77%     4.50MB  4.06%                | github.com/gnolang/gno/gnovm/pkg/gnolang.(*Machine).RunFiles
                                            4.50MB   100% |   github.com/gnolang/gno/gnovm/pkg/gnolang.(*Machine).runFileDecls
```

and after:

```shell
Showing nodes accounting for 61.99MB, 73.12% of 84.78MB total
Showing top 10 nodes out of 196
      flat  flat%   sum%        cum   cum%
   19.50MB 23.00% 23.00%    19.50MB 23.00%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Attributes).SetAttribute
   12.52MB 14.76% 37.77%    18.02MB 21.26%  github.com/gnolang/gno/gnovm/pkg/gnolang.Go2Gno
    7.58MB  8.94% 46.70%     9.15MB 10.79%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*defaultStore).SetObject
       5MB  5.90% 52.60%        5MB  5.90%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*StaticBlock).InitStaticBlock
    3.47MB  4.09% 56.69%     3.47MB  4.09%  github.com/gnolang/gno/gnovm/pkg/gnolang.(*Allocator).NewDataArray
       3MB  3.54% 60.24%        3MB  3.54%  github.com/gnolang/gno/gnovm/pkg/gnolang.NewBlock
       3MB  3.54% 63.77%        3MB  3.54%  github.com/gnolang/gno/gnovm/pkg/gnolang.Nx (inline)
    2.77MB  3.26% 67.04%     2.77MB  3.26%  bytes.growSlice
    2.65MB  3.12% 70.16%     2.65MB  3.12%  internal/runtime/maps.newarray
    2.50MB  2.95% 73.12%     2.50MB  2.95%  runtime.allocm
```

Fixes gnolang#3436
@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Jan 2, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Jan 2, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
  • The pull request description provides enough details
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 The pull request was created from a fork (head branch repo: odeke-em/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission
The pull request description provides enough details

If

🟢 Condition met
└── 🟢 Not (🔴 Pull request author is a member of the team: core-contributors)

Can be checked by

  • team core-contributors

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

Attention: Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
gnovm/pkg/gnolang/nodes.go 85.18% 3 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@jefft0 jefft0 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related review/triage-pending PRs opened by external contributors that are waiting for the 1st review
Projects
Status: Triage
3 participants