From efad55cf868c1cf0883f1e821702977e854cfbab Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 13 Nov 2020 18:14:10 -0800 Subject: [PATCH] net/packet: speed up packet decoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compiler is failing to draw the connection between slice cap and slice len, so is missing some obvious BCE opportunities. Give it a hint by making the cap equal to the length. The generated code is smaller and cleaner, and a bit faster. name old time/op new time/op delta Decode/tcp4-8 12.2ns ± 1% 11.6ns ± 3% -5.31% (p=0.000 n=28+29) Decode/tcp6-8 12.5ns ± 2% 11.9ns ± 2% -4.84% (p=0.000 n=30+30) Decode/udp4-8 11.5ns ± 1% 11.1ns ± 1% -3.11% (p=0.000 n=25+24) Decode/udp6-8 11.8ns ± 3% 11.4ns ± 1% -3.08% (p=0.000 n=30+26) Decode/icmp4-8 11.0ns ± 3% 10.6ns ± 1% -3.38% (p=0.000 n=25+30) Decode/icmp6-8 11.4ns ± 1% 11.1ns ± 2% -2.29% (p=0.000 n=27+30) Decode/igmp-8 10.3ns ± 0% 10.0ns ± 1% -3.26% (p=0.000 n=19+23) Decode/unknown-8 8.68ns ± 1% 8.38ns ± 1% -3.55% (p=0.000 n=28+29) --- net/packet/packet.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/packet/packet.go b/net/packet/packet.go index 2dc42c937..97324541f 100644 --- a/net/packet/packet.go +++ b/net/packet/packet.go @@ -150,6 +150,7 @@ func (q *Parsed) decode4(b []byte) { return } sub := b[q.subofs:] + sub = sub[:len(sub):len(sub)] // help the compiler do bounds check elimination // We don't care much about IP fragmentation, except insofar as it's // used for firewall bypass attacks. The trick is make the first @@ -267,6 +268,7 @@ func (q *Parsed) decode6(b []byte) { // dropped. q.subofs = 40 sub := b[q.subofs:] + sub = sub[:len(sub):len(sub)] // help the compiler do bounds check elimination switch q.IPProto { case ICMPv6: