From 28dee17585bcd5fd10db5b0641394f814ca6f188 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Tue, 6 Jun 2023 13:47:12 +0200 Subject: [PATCH] Remove unnecessary handler allocations (#47) Cleanup unnecessary creation of `handler` vars for various event handlers. Signed-off-by: SuperQ --- ping.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/ping.go b/ping.go index ff08121..73b6437 100644 --- a/ping.go +++ b/ping.go @@ -482,8 +482,8 @@ func (p *Pinger) run(ctx context.Context, conn packetConn) error { recv := make(chan *packet, 5) defer close(recv) - if handler := p.OnSetup; handler != nil { - handler() + if p.OnSetup != nil { + p.OnSetup() } g, ctx := errgroup.WithContext(ctx) @@ -578,10 +578,8 @@ func (p *Pinger) Stop() { } func (p *Pinger) finish() { - handler := p.OnFinish - if handler != nil { - s := p.Statistics() - handler(s) + if p.OnFinish != nil { + p.OnFinish(p.Statistics()) } } @@ -764,9 +762,8 @@ func (p *Pinger) processPacket(recv *packet) error { return fmt.Errorf("invalid ICMP echo reply; type: '%T', '%v'", pkt, pkt) } - handler := p.OnRecv - if handler != nil { - handler(inPkt) + if p.OnRecv != nil { + p.OnRecv(inPkt) } return nil @@ -814,8 +811,7 @@ func (p *Pinger) sendICMP(conn packetConn) error { } return err } - handler := p.OnSend - if handler != nil { + if p.OnSend != nil { outPkt := &Packet{ Nbytes: len(msgBytes), IPAddr: p.ipaddr, @@ -823,7 +819,7 @@ func (p *Pinger) sendICMP(conn packetConn) error { Seq: p.sequence, ID: p.id, } - handler(outPkt) + p.OnSend(outPkt) } // mark this sequence as in-flight p.awaitingSequences[currentUUID][p.sequence] = struct{}{}