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: use fmt.Fprintf to avoid unnecessary string+args + WriteString #3434

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 1, 2025

In a bid to remove unnecessary CPU and memory bloat for the gnovm which takes the order of minutes to run certain code, I noticed the pattern:

io.StringWriter.WriteString(fmt.Sprintf(...))

in which fmt.Sprintf(...) has to create a string by inserting all arguments into the format specifiers then pass that into .WriteString which defeats the entire purpose of io.StringWriter.WriteString that *bytes.Buffer and *strings.Builder implement.

Just from picking a single benchmark that already exists results in improvements in all dimensions:

name               old time/op    new time/op    delta
StringLargeData-8    8.87ms ± 1%    8.28ms ± 3%   -6.68%  (p=0.000 n=17+19)

name               old alloc/op   new alloc/op   delta
StringLargeData-8    8.44MB ± 0%    7.78MB ± 0%   -7.75%  (p=0.000 n=20+19)

name               old allocs/op  new allocs/op  delta
StringLargeData-8     94.1k ± 0%     70.1k ± 0%  -25.51%  (p=0.000 n=15+20)

for heavily used code this is going to reduce on garbage collection cycles too.

Fixes #3433

@github-actions github-actions bot added 📦 🤖 gnovm Issues or PRs gnovm related 📦 🌐 tendermint v2 Issues or PRs tm2 related labels Jan 1, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Jan 1, 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 1, 2025

Codecov Report

Attention: Patch coverage is 48.27586% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tm2/pkg/sdk/auth/params.go 0.00% 9 Missing ⚠️
gnovm/pkg/gnolang/machine.go 77.77% 4 Missing ⚠️
tm2/pkg/bft/rpc/lib/server/handlers.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Member

@notJoon notJoon left a comment

Choose a reason for hiding this comment

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

more cleaner than previous version. I left some comments

@@ -2131,25 +2131,25 @@ func (m *Machine) String() string {
totalLength = vsLength + ssLength + xsLength + bsLength + obsLength + fsLength + exceptionsLength
)

var builder strings.Builder
builder := new(strings.Builder)
Copy link
Member

@notJoon notJoon Jan 2, 2025

Choose a reason for hiding this comment

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

strings.Builder is a value type, so you don't need to create it using the new keyword. Using new will allocate memory on the heap, which may result in another unnecessary memory allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, builder doesn't escape the heap. Go isn't like traditional languages where invoking "new" automatically allocates on the heap; escape analysis determines that and you can verifiably check that it doesn't escape and also run benchmarks to see that it doesn't escape using -gcflags='-l -m'. I can however bring it to a much better compromise of

var sb strings.Builder
builder := &sb

which is a much better compromise than

fmt.Fprintf(&sb, ...)

in every spot and materially even faster.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I didn't know that flag option has exists. Then this seems to have no problem at all. 👍

gnovm/pkg/gnolang/machine.go Show resolved Hide resolved
@odeke-em odeke-em force-pushed the all-use-fmt.Fprintf-to-avoid-raw-string-concatentation+io.StringWriter.WriteString branch from ff8f2c3 to 932a525 Compare January 2, 2025 03:04
@odeke-em
Copy link
Contributor Author

odeke-em commented Jan 2, 2025

Thank you for the code review and discourse @notJoon, I've made an update, kindly please help me take another stab.

@odeke-em odeke-em requested a review from notJoon January 2, 2025 03:06
Copy link
Member

@notJoon notJoon left a comment

Choose a reason for hiding this comment

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

LGTM 💯

@odeke-em odeke-em force-pushed the all-use-fmt.Fprintf-to-avoid-raw-string-concatentation+io.StringWriter.WriteString branch from 932a525 to 902ba09 Compare January 2, 2025 03:12
In a bid to remove unnecessary CPU and memory bloat for the gnovm which
takes the order of minutes to run certain code, I noticed the pattern:

    io.StringWriter.WriteString(fmt.Sprintf(...))

in which fmt.Sprintf(...) has to create a string by inserting all
arguments into the format specifiers then pass that into .WriteString
which defeats the entire purpose of io.StringWriter.WriteString
that *bytes.Buffer and *strings.Builder implement.

Just from picking a single benchmark that already exists results in
improvements in all dimensions:

```shell
$ benchstat before.txt after.txt
name               old time/op    new time/op    delta
StringLargeData-8    8.87ms ± 1%    8.28ms ± 3%   -6.68%  (p=0.000 n=17+19)

name               old alloc/op   new alloc/op   delta
StringLargeData-8    8.44MB ± 0%    7.78MB ± 0%   -7.75%  (p=0.000 n=20+19)

name               old allocs/op  new allocs/op  delta
StringLargeData-8     94.1k ± 0%     70.1k ± 0%  -25.51%  (p=0.000 n=15+20)
```

for heavily used code this is going to reduce on garbage collection
cycles too.

Fixes gnolang#3433
@odeke-em odeke-em force-pushed the all-use-fmt.Fprintf-to-avoid-raw-string-concatentation+io.StringWriter.WriteString branch from 902ba09 to 6cd03c2 Compare January 2, 2025 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: Triage
3 participants