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(abci): removes the goroutine when call SnapshotIfApplicable #17174

Merged
merged 2 commits into from
Jul 29, 2023

Conversation

zakir-code
Copy link
Contributor

Description

follow-up #17158


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • run make lint and make test
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@zakir-code zakir-code requested a review from a team as a code owner July 28, 2023 10:01
Comment on lines 840 to 847
app.prepareCheckStater(app.checkState.ctx)
}

go app.snapshotManager.SnapshotIfApplicable(header.Height)
// The SnapshotIfApplicable method will create the snapshot by starting the goroutine
app.snapshotManager.SnapshotIfApplicable(header.Height)

return resp, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Change potentially affects state.

Call sequence:

(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).Commit (baseapp/abci.go:799)

@julienrbrt
Copy link
Member

Can you run ./scripts/go-mod-tidy-all.sh

@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Jul 28, 2023
@zakir-code
Copy link
Contributor Author

zakir-code commented Jul 28, 2023

Should ./scripts/go-mod-tidy-all.sh be added to the makefile and replace them

@tac0turtle
Copy link
Member

any chance we can do some sort of bechmark to verify this is performance related?

@julienrbrt
Copy link
Member

any chance we can do some sort of bechmark to verify this is performance related?

It saves a very tiny overhead (go routine creation) when it could have been avoided, but this is still beneficial. Not sure if a benchmark would be significantly show the difference.

@zakir-code
Copy link
Contributor Author

zakir-code commented Jul 29, 2023

Hi, Here is a simple benchmark test

package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"testing"
)

type snapshotManager struct {
	w        io.Writer
	interval uint64
}

func newSnapshotManager(interval uint64, dir string) *snapshotManager {
	file, err := os.Create(filepath.Join(dir, "snapshot1.data"))
	if err != nil {
		panic(err)
	}
	return &snapshotManager{interval: interval, w: file}
}

func (m *snapshotManager) SnapshotIfApplicable1(height int64) {
	if m == nil {
		return
	}
	if m.interval <= 0 || uint64(height)%m.interval != 0 {
		return
	}
	go func() {
		_, _ = fmt.Fprintln(m.w, "create snapshot")
	}()
}

func (m *snapshotManager) SnapshotIfApplicable2(height int64) {
	if m == nil {
		return
	}
	if m.interval <= 0 || uint64(height)%m.interval != 0 {
		return
	}
	_, _ = fmt.Fprintln(m.w, "create snapshot")
}

func Benchmark_SnapshotIfApplicable1(b *testing.B) {
	manager := newSnapshotManager(10, b.TempDir())

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		manager.SnapshotIfApplicable1(int64(i))
	}
}

func Benchmark_SnapshotIfApplicable2(b *testing.B) {
	manager := newSnapshotManager(10, b.TempDir())

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go manager.SnapshotIfApplicable2(int64(i))
	}
}
go version go1.20.4 darwin/amd64

go test -test.v -test.paniconexit0 -test.bench . -test.run ^$

goos: darwin
goarch: amd64
pkg: github.com/cosmos/cosmos-sdk
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Benchmark_SnapshotIfApplicable1
Benchmark_SnapshotIfApplicable1-12       8077500               172.3 ns/op
Benchmark_SnapshotIfApplicable2
Benchmark_SnapshotIfApplicable2-12       3608736               362.0 ns/op
PASS
ok      github.com/cosmos/cosmos-sdk        5.874s

@tac0turtle tac0turtle added this pull request to the merge queue Jul 29, 2023
Merged via the queue into cosmos:main with commit 33051ce Jul 29, 2023
mergify bot pushed a commit that referenced this pull request Jul 29, 2023
…17174)

(cherry picked from commit 33051ce)

# Conflicts:
#	simapp/go.mod
#	simapp/go.sum
#	tests/go.mod
#	tests/go.sum
@faddat faddat mentioned this pull request Nov 8, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants