apply selectors only on sells for performance
All checks were successful
Generate check / check-changes (pull_request) Successful in 3s
Quality / check-changes (pull_request) Successful in 3s
Generate check / verify-generate (pull_request) Has been skipped
Quality / run-tests (pull_request) Successful in 13s

This commit is contained in:
2026-05-16 11:58:35 +01:00
parent 24c2814eef
commit d371aca767

View File

@@ -55,8 +55,8 @@ type ReportWriter interface {
type Selector func(Record) bool type Selector func(Record) bool
// BuildReport reads records from a RecordReader and, if the record passes the Selector, it is // BuildReport reads records from a RecordReader and, if the record passes the Selector, it is
// processed into the report // processed into the ReportWriter.
func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, s Selector) error { func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, sel Selector) error {
buys := make(map[string]*FillerQueue) buys := make(map[string]*FillerQueue)
var buysCount, sellsCount int64 var buysCount, sellsCount int64
@@ -83,14 +83,6 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
return err return err
} }
if !s(rec) {
slog.Debug("Report: skipping record",
slog.String("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()),
)
continue
}
if rec.Side().IsBuy() { if rec.Side().IsBuy() {
buysCount++ buysCount++
} else { } else {
@@ -105,7 +97,7 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
buys[rec.Symbol()] = buyQueue buys[rec.Symbol()] = buyQueue
} }
err = processRecord(ctx, buyQueue, rec, writer) err = processRecord(ctx, buyQueue, rec, sel, writer)
if err != nil { if err != nil {
return fmt.Errorf("processing record: %w", err) return fmt.Errorf("processing record: %w", err)
} }
@@ -114,7 +106,11 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
} }
} }
func processRecord(ctx context.Context, q *FillerQueue, rec Record, writer ReportWriter) error { // processRecord either adds buys to the queue or consumes buys from the queue when processing a
// sell record.
// Selectors are only applied for sells for performance reasons. It's much cheaper to just accumulate
// buys and only actually inspect a record once a sell happens
func processRecord(ctx context.Context, q *FillerQueue, rec Record, sel Selector, writer ReportWriter) error {
slog.Debug("Report: processing record", slog.Debug("Report: processing record",
slog.String("symbol", rec.Symbol()), slog.String("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()), slog.String("side", rec.Side().String()),
@@ -125,6 +121,14 @@ func processRecord(ctx context.Context, q *FillerQueue, rec Record, writer Repor
q.Push(NewFiller(rec)) q.Push(NewFiller(rec))
case SideSell: case SideSell:
if !sel(rec) {
slog.Debug("Report: skipping record",
slog.String("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()),
)
return nil
}
unmatchedQty := rec.Quantity() unmatchedQty := rec.Quantity()
for unmatchedQty.IsPositive() { for unmatchedQty.IsPositive() {