8000 Reduce allocation in udp muxed conn addr decode (#686) · pion/ice@d9a6837 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit d9a6837

Browse files
committed
Reduce allocation in udp muxed conn addr decode (#686)
1 parent 528d31f commit d9a6837

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

udp_mux_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,25 @@ func BenchmarkAddressEncoding(b *testing.B) {
157157
}
158158
buf := make([]byte, 64)
159159

160-
for i := 0; i < b.N; i++ {
161-
if _, err := encodeUDPAddr(addr, buf); err != nil {
162-
require.NoError(b, err)
160+
b.Run("encode", func(b *testing.B) {
161+
for i := 0; i < b.N; i++ {
162+
if _, err := encodeUDPAddr(addr, buf); err != nil {
163+
require.NoError(b, err)
164+
}
163165
}
164-
}
166+
})
167+
168+
b.Run("decode", func(b *testing.B) {
169+
n, _ := encodeUDPAddr(addr, buf)
170+
var addr *net.UDPAddr
171+
var err error
172+
for i := 0; i < b.N; i++ {
173+
if addr, err = decodeUDPAddr(buf[:n]); err != nil {
174+
require.NoError(b, err)
175+
}
176+
}
177+
_ = addr
178+
})
165179
}
166180

167181
func testMuxConnection(t *testing.T, udpMux *UDPMuxDefault, ufrag string, network string) {

udp_muxed_conn.go

+24-32
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import (
77
"encoding/binary"
88
"io"
99
"net"
10-
"net/netip"
11-
"reflect"
1210
"sync"
1311
"time"
14-
"unsafe"
1512

1613
"github.com/pion/logging"
1714
"github.com/pion/transport/v2/packetio"
@@ -217,51 +214,46 @@ func (c *udpMuxedConn) writePacket(data []byte, addr *net.UDPAddr) error {
217214
}
218215

219216
func encodeUDPAddr(addr *net.UDPAddr, buf []byte) (int, error) {
220-
if len(addr.IP) != 0 && len(addr.IP) != net.IPv4len && len(addr.IP) != net.IPv6len {
221-
return 0, errInvalidAddress
217+
total := 1 + len(addr.IP) + 2 + len(addr.Zone)
218+
if len(buf) < total {
219+
return 0, io.ErrShortBuffer
222220
}
223221

224-
var n int
225-
if ip4 := addr.IP.To4(); len(ip4) == net.IPv4len {
226-
d := (*reflect.SliceHeader)(unsafe.Pointer(&ip4)) // nolint:gosec
227-
n = len(netip.AddrFrom4(*(*[4]byte)(unsafe.Pointer(d.Data))).AppendTo(buf[2:2])) // nolint:gosec
228-
} else if len(addr.IP) != 0 {
229-
d := (*reflect.SliceHeader)(unsafe.Pointer(&addr.IP)) // nolint:gosec
230-
n = len(netip.AddrFrom16(*(*[16]byte)(unsafe.Pointer(d.Data))).AppendTo(buf[2:2])) // nolint:gosec
231-
}
222+
buf[0] = uint8(len(addr.IP))
223+
offset := 1
232224

233-
total := 2 + n + 2 + len(addr.Zone)
234-
if total > len(buf) {
235-
return 0, io.ErrShortBuffer
236-
}
225+
copy(buf[offset:], addr.IP)
226+
offset += len(addr.IP)
237227

238-
binary.LittleEndian.PutUint16(buf, uint16(n))
239-
offset := 2 + n
240228
binary.LittleEndian.PutUint16(buf[offset:], uint16(addr.Port))
241229
offset += 2
230+
242231
copy(buf[offset:], addr.Zone)
243232
return total, nil
244233
}
245234

246235
func decodeUDPAddr(buf []byte) (*net.UDPAddr, error) {
247-
addr := net.UDPAddr{}
236+
addr := &net.UDPAddr{}
248237

249-
offset := 0
250-
ipLen := int(binary.LittleEndian.Uint16(buf[:2]))
251-
offset += 2
252238
// Basic bounds checking
253-
if ipLen+offset > len(buf) {
239+
if len(buf) == 0 || len(buf) < int(buf[0])+3 {
254240
return nil, io.ErrShortBuffer
255241
}
256-
if err := addr.IP.UnmarshalText(buf[offset : offset+ipLen]); err != nil {
257-
return nil, err
242+
243+
ipLen := int(buf[0])
244+
offset := 1
245+
246+
if ipLen == 0 {
247+
addr.IP = nil
248+
} else {
249+
addr.IP = append(addr.IP[:0], buf[offset:offset+ipLen]...)
250+
offset += ipLen
258251
}
259-
offset += ipLen
260-
addr.Port = int(binary.LittleEndian.Uint16(buf[offset : offset+2]))
252+
253+
addr.Port = int(binary.LittleEndian.Uint16(buf[offset:]))
261254
offset += 2
262-
zone := make([]byte, len(buf[offset:]))
263-
copy(zone, buf[offset:])
264-
addr.Zone = string(zone)
265255

266-
return &addr, nil
256+
addr.Zone = string(buf[offset:])
257+
258+
return addr, nil
267259
}

0 commit comments

Comments
 (0)
0