Skip to content

Commit

Permalink
Update FullFillTradeLogs to reduce memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Haiss2 committed Jul 8, 2024
1 parent ecfcfa9 commit 0fae31b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 4 additions & 2 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func (w *Worker) processMessages(m []evmlistenerclient.Message) error {
if err := w.s.Delete(deleteBlocks); err != nil {
return err
}
if err := w.s.Insert(w.priceFiller.FullFillTradeLogs(insertOrders)); err != nil {
w.priceFiller.FullFillTradeLogs(insertOrders)
if err := w.s.Insert(insertOrders); err != nil {
return err
}
for _, log := range insertOrders {
Expand Down Expand Up @@ -168,7 +169,8 @@ func (w *Worker) retryParseLog() error {
insertOrders = append(insertOrders, order)
}

if err := w.s.Insert(w.priceFiller.FullFillTradeLogs(insertOrders)); err != nil {
w.priceFiller.FullFillTradeLogs(insertOrders)
if err := w.s.Insert(insertOrders); err != nil {
return err
}
for _, log := range insertOrders {
Expand Down
13 changes: 5 additions & 8 deletions pkg/pricefiller/price_fillter.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ func (p *PriceFiller) runBackFillTradelogPriceRoutine() {
continue
}

filledTrades := p.FullFillTradeLogs(tradeLogs)
if err := p.s.Insert(filledTrades); err != nil {
p.FullFillTradeLogs(tradeLogs)
if err := p.s.Insert(tradeLogs); err != nil {
p.l.Errorw("Failed to insert tradeLogs", "err", err)
continue
}

p.l.Infow("backfill tradelog price successfully", "trades", filledTrades)
p.l.Infow("backfill tradelog price successfully", "trades", tradeLogs)
}
}

Expand Down Expand Up @@ -193,20 +193,17 @@ func (p *PriceFiller) getPriceAndAmountUsd(address, rawAmt string, at int64) (fl
return 0, 0, nil
}

func (p *PriceFiller) FullFillTradeLogs(tradeLogs []storage.TradeLog) []storage.TradeLog {
resp := make([]storage.TradeLog, len(tradeLogs))
func (p *PriceFiller) FullFillTradeLogs(tradeLogs []storage.TradeLog) {
for idx, tradeLog := range tradeLogs {
// for the safety, sleep a bit to avoid Binance rate limit
time.Sleep(10 * time.Millisecond)
filledTradeLog, err := p.fullFillTradeLog(tradeLog)
if err != nil {
p.l.Errorw("Failed to fullFillTradeLog", "err", err, "tradeLog", tradeLog)
resp[idx] = tradeLog
continue
}
resp[idx] = filledTradeLog
tradeLogs[idx] = filledTradeLog
}
return resp
}

func (p *PriceFiller) getDecimals(address string) (int64, error) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/pricefiller/price_fillter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestFillPrice(t *testing.T) {
require.NoError(t, err)
}

newLogs := filler.FullFillTradeLogs([]storage.TradeLog{
tradeLogs := []storage.TradeLog{
{
Taker: "0x807cf9a772d5a3f9cefbc1192e939d62f0d9bd38",
MakerToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
Expand Down Expand Up @@ -57,7 +57,8 @@ func TestFillPrice(t *testing.T) {
Timestamp: 1720061615000,
EventHash: "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8",
},
})
}
filler.FullFillTradeLogs(tradeLogs)

t.Log(newLogs)
t.Log(tradeLogs)
}

0 comments on commit 0fae31b

Please sign in to comment.