Skip to content

Optimize platformvm mempool peek operations (~20% less memory) #1455

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

Closed
wants to merge 6 commits into from

Conversation

gyuho
Copy link
Contributor

@gyuho gyuho commented May 2, 2023

Why this should be merged

Simplify, optimize platform vm mempool peek operations.

How this works

Previously, we just list and append all txs, and then iterate one by one with the max tx size limit. This PR introduces listing with limit at the heap level.

How this was tested

Benchmarks + unit tests.

go install -v golang.org/x/tools/cmd/benchcmp@latest
go install -v golang.org/x/perf/cmd/benchstat@latest

go test -run=NONE -bench=BenchmarkPeekTxs > /tmp/cpu.before.txt
go test -run=NONE -bench=BenchmarkPeekTxs > /tmp/cpu.after.txt
benchcmp /tmp/cpu.before.txt /tmp/cpu.after.txt
benchstat -alpha 0.03 /tmp/cpu.before.txt /tmp/cpu.after.txt

go test -run=NONE -bench=BenchmarkPeekTxs -benchmem > /tmp/mem.before.txt
go test -run=NONE -bench=BenchmarkPeekTxs -benchmem > /tmp/mem.after.txt
benchcmp /tmp/mem.before.txt /tmp/mem.after.txt
benchstat -alpha 0.03 /tmp/mem.before.txt /tmp/mem.after.txt
benchmark               old ns/op     new ns/op     delta
BenchmarkPeekTxs-12     2540          1919          -24.45%

benchmark               old ns/op     new ns/op     delta
BenchmarkPeekTxs-12     2428          1796          -26.03%

benchmark               old allocs     new allocs     delta
BenchmarkPeekTxs-12     3              3              +0.00%

benchmark               old bytes     new bytes     delta
BenchmarkPeekTxs-12     13568         10240         -24.53%

pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
           │ /tmp/cpu.before.txt │       /tmp/cpu.after.txt        │
           │       sec/op        │    sec/op     vs base           │
PeekTxs-12          2.540µ ± ∞ ¹   1.919µ ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.03

goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
           │ /tmp/mem.before.txt │       /tmp/mem.after.txt        │
           │       sec/op        │    sec/op     vs base           │
PeekTxs-12          2.428µ ± ∞ ¹   1.796µ ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.03

           │ /tmp/mem.before.txt │        /tmp/mem.after.txt        │
           │        B/op         │     B/op       vs base           │
PeekTxs-12         13.25Ki ± ∞ ¹   10.00Ki ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.03

           │ /tmp/mem.before.txt │       /tmp/mem.after.txt       │
           │      allocs/op      │  allocs/op   vs base           │
PeekTxs-12           3.000 ± ∞ ¹   3.000 ± ∞ ¹  ~ (p=1.000 n=1) ²
¹ need >= 6 samples for confidence interval at level 0.95

gyuho added 4 commits May 2, 2023 01:03
Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
cat /tmp/cpu.before.txt
cat /tmp/cpu.after.txt
cat /tmp/mem.before.txt
cat /tmp/mem.after.txt
goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
BenchmarkPeekTxs-12       486460              2414 ns/op
PASS
ok      github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool      1.456s
goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
BenchmarkPeekTxs-12       627082              1849 ns/op
PASS
ok      github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool      1.414s
goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
BenchmarkPeekTxs-12       479960              2340 ns/op           13568 B/op          3 allocs/op
PASS
ok      github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool      1.383s
goos: darwin
goarch: arm64
pkg: github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool
BenchmarkPeekTxs-12       659138              1806 ns/op           10240 B/op          3 allocs/op
PASS
ok      github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool      1.463s

Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
@gyuho gyuho self-assigned this May 2, 2023
@gyuho
Copy link
Contributor Author

gyuho commented May 2, 2023

For benchmarks, you can just reproduce with commenting out/in the following:

func (m *mempool) PeekTxs(maxTxsBytes int) []*txs.Tx {
	// txs := m.unissuedDecisionTxs.List()
	// txs = append(txs, m.unissuedStakerTxs.List()...)

	// size := 0
	// for i, tx := range txs {
	// 	size += len(tx.Bytes())
	// 	if size > maxTxsBytes {
	// 		return txs[:i]
	// 	}
	// }
	// return txs

	txs, remaining := m.unissuedDecisionTxs.ListWithLimit(maxTxsBytes)
	if remaining <= 0 {
		return txs
	}
	txs2, _ := m.unissuedStakerTxs.ListWithLimit(remaining)
	return append(txs, txs2...)
}

Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
@@ -36,12 +36,12 @@ var (
// $ go test -run=NONE -bench=BenchmarkMarshalVersion > /tmp/cpu.before.txt
// $ USE_BUILDER=true go test -run=NONE -bench=BenchmarkMarshalVersion > /tmp/cpu.after.txt
// $ benchcmp /tmp/cpu.before.txt /tmp/cpu.after.txt
// $ benchstat -alpha 0.03 -geomean /tmp/cpu.before.txt /tmp/cpu.after.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This flag is deprecated btw

@@ -15,7 +15,15 @@ var _ Heap = (*txHeap)(nil)
type Heap interface {
Add(tx *txs.Tx)
Get(txID ids.ID) *txs.Tx

// Returns all the transactions in the order of heap.
Copy link
Contributor

Choose a reason for hiding this comment

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

as I mentioned above, I'd drop List and keep only ListWithLimit

Signed-off-by: Gyuho Lee <gyuho.lee@avalabs.org>
@gyuho gyuho added the vm This involves virtual machines label May 3, 2023
@gyuho gyuho added the DO NOT MERGE This PR must not be merged in its current state label May 13, 2023
@gyuho
Copy link
Contributor Author

gyuho commented May 13, 2023

Discussed offline with @StephenButtolph for other great suggestions.

To be updated.

@StephenButtolph
Copy link
Contributor

StephenButtolph commented May 25, 2023

Seems 1536 is replacing this - so closing.

hexfusion pushed a commit to hexfusion/avalanchego that referenced this pull request Jun 22, 2023
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
@dhrubabasu dhrubabasu deleted the mempool branch November 21, 2023 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DO NOT MERGE This PR must not be merged in its current state vm This involves virtual machines
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants