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

Reduce memory allocations when computing a tree hash #4976

Merged

Conversation

pvragov
Copy link
Contributor

@pvragov pvragov commented Sep 6, 2023

Hello. I've decided to rework ComputeTreeHash function because it makes lots of memory allocations.
Benchmark I used:

func BenchmarkComputeTreeHash(b *testing.B) {
	var hashes [][]byte
	for i := 0; i < 1024; i++ {
		h := sha256.Sum256([]byte("test"))
		hashes = append(hashes, h[:])
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = glacier.ComputeTreeHash(hashes)
	}
	b.ReportAllocs()
}

Benchcmp result:

benchmark                       old ns/op     new ns/op     delta
BenchmarkComputeTreeHash-10     128886        88221         -31.55%

benchmark                       old allocs     new allocs     delta
BenchmarkComputeTreeHash-10     3124           1              -99.97%

benchmark                       old bytes     new bytes     delta
BenchmarkComputeTreeHash-10     179808        32768         -81.78%

@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch from 529d087 to c553279 Compare September 6, 2023 16:03
@pvragov pvragov changed the title Reduce memory allocations when computing tree hash Reduce memory allocations when computing a tree hash Sep 6, 2023
@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch 2 times, most recently from 865ba6d to 1333191 Compare September 6, 2023 16:54
Copy link
Contributor

@lucix-aws lucix-aws left a comment

Choose a reason for hiding this comment

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

This is a solid change. I have one request: please update treehash_test.go to include actual unit tests for this logic. Right now it doesn't have any real *testing.T-based tests, it just has a standalone function that you'd have to run manually (that doesn't even assert anything). Since the discrete cases here are basically no hash, one hash, even hashes, and odd hashes, I think a case for each # of input hashes in the range [0,3] should cover everything.

When you do so, please verify that the resulting hashes in both the old and new implementation are identical.

As an aside, v2 of the SDK has this same code for glacier. If you'd like the contribution credit there, please open an identical PR with these changes, otherwise we'll happily do ourselves.

@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch from 1333191 to e9b720f Compare September 7, 2023 17:59
@pvragov
Copy link
Contributor Author

pvragov commented Sep 7, 2023

This is a solid change. I have one request: please update treehash_test.go to include actual unit tests for this logic. Right now it doesn't have any real *testing.T-based tests, it just has a standalone function that you'd have to run manually (that doesn't even assert anything). Since the discrete cases here are basically no hash, one hash, even hashes, and odd hashes, I think a case for each # of input hashes in the range [0,3] should cover everything.

When you do so, please verify that the resulting hashes in both the old and new implementation are identical.

As an aside, v2 of the SDK has this same code for glacier. If you'd like the contribution credit there, please open an identical PR with these changes, otherwise we'll happily do ourselves.

Hello, @lucix-aws. Thanks for the review. I've added the tests you asked. Also I've tested that the old implementation passes the tests.

P.S. I'll open the identical PR for v2 after approval. Thanks for the advice.

@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch from e9b720f to 917ea3a Compare September 7, 2023 18:29
@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch from 917ea3a to 97a9620 Compare September 9, 2023 06:51
@pvragov pvragov force-pushed the reduce-memory-allocations-when-computing-tree-hash branch from 97a9620 to 298229f Compare September 9, 2023 07:07
@pvragov pvragov requested a review from isaiahvita September 11, 2023 16:58
@lucix-aws
Copy link
Contributor

@nsofanat1k Thanks again for the contribution. This is good to merge - we plan to do so either later this afternoon or early tomorrow morning.

In the meantime, you're free to open the equivalent in v2.

@isaiahvita
Copy link
Contributor

@nsofanat1k LGTM thanks again for the contribution. @lucix-aws will handle the merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants