Reclaim memory manually in some tests that fuzz the beacon state #15395
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
Tests
What does this PR do? Why is it needed?
#15292 is currently blocked because unit tests that involve fuzzing the state use a ridiculous amount of memory. An example is
prysm/beacon-chain/core/altair/attestation_test.go
Lines 460 to 482 in f5a9394
By default the fuzzer initializes slice fields with a maximum length of 10, but multi-value slice constructors for block roots, state roots and randao mixes always use slices with the length equal to the one defined in
fieldparams
, padding the input slices if necessary. Because of this large slices are allocated in every iteration of the test, and because the beacon state keeps a reference to the multi-value slice, the multi-value slice is not garbage collected fast enough during fuzzing.To fix this, I wrote a helper function
FreeMemory
that callsdebug.FreeOSMemory()
every 10 loop iterations. Each test that fuzzes a proto beacon state and initializes a native state from that proto state inside the loop should call this function. This will significantly reduce in-use memory (usually more than 100x) while still keeping tests times relatively short (out of all existing such tests the longest one took 11 seconds on my machine).