-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exp/orderbook: Reallocate offer slice after removals
The code in this PR reallocates offers slice after each offer removal. This is done to prevent situation keeping a large chunks of allocated but unused memory that can lead to OOM crash.
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package orderbook | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stellar/go/xdr" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRemoveOffersMemoryUsage(t *testing.T) { | ||
edges := edgeSet{} | ||
for i := 0; i < 2000; i++ { | ||
edges = edges.addOffer(1, xdr.OfferEntry{ | ||
SellerId: xdr.MustAddress("GCZFUQEPMLGUE2NB5RR7C3I2LTTLOEBM7GYD7PDKI4SU5HHWTDB553WD"), | ||
OfferId: xdr.Int64(i), | ||
}) | ||
} | ||
|
||
var afterAdded, afterRemoved, afterReallocate runtime.MemStats | ||
runtime.ReadMemStats(&afterAdded) | ||
|
||
t.Logf("after added: %d\n", afterAdded.HeapInuse) | ||
|
||
// Remove all offers except one | ||
for i := 0; i < 2000-1; i++ { | ||
var removed bool | ||
edges, removed = edges.removeOffer(1, xdr.Int64(i)) | ||
require.True(t, removed) | ||
|
||
} | ||
|
||
runtime.ReadMemStats(&afterRemoved) | ||
t.Logf("after removed: %d\n", afterRemoved.HeapInuse) | ||
|
||
require.True(t, edges[0].reallocate) | ||
edges.reallocate() | ||
runtime.GC() | ||
|
||
runtime.ReadMemStats(&afterReallocate) | ||
t.Logf("after reallocate: %d\n", afterReallocate.HeapInuse) | ||
|
||
assert.Less(t, afterReallocate.HeapInuse, afterAdded.HeapInuse) | ||
assert.Less(t, afterReallocate.HeapInuse, afterRemoved.HeapInuse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters