1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2025-09-18 20:57:50 +02:00
wireguard-go/device
Alexander Yastrebov 9e7529c3d2 device: reduce RoutineHandshake allocations
Reduce allocations by eliminating byte reader, hand-rolled decoding and
reusing message structs.

Synthetic benchmark:

    var msgSink MessageInitiation
    func BenchmarkMessageInitiationUnmarshal(b *testing.B) {
        packet := make([]byte, MessageInitiationSize)
        reader := bytes.NewReader(packet)
        err := binary.Read(reader, binary.LittleEndian, &msgSink)
        if err != nil {
            b.Fatal(err)
        }
        b.Run("binary.Read", func(b *testing.B) {
            b.ReportAllocs()
            for range b.N {
                reader := bytes.NewReader(packet)
                _ = binary.Read(reader, binary.LittleEndian, &msgSink)
            }
        })
        b.Run("unmarshal", func(b *testing.B) {
            b.ReportAllocs()
            for range b.N {
                _ = msgSink.unmarshal(packet)
            }
        })
    }

Results:
                                         │      -      │
                                         │   sec/op    │
MessageInitiationUnmarshal/binary.Read-8   1.508µ ± 2%
MessageInitiationUnmarshal/unmarshal-8     12.66n ± 2%

                                         │      -       │
                                         │     B/op     │
MessageInitiationUnmarshal/binary.Read-8   208.0 ± 0%
MessageInitiationUnmarshal/unmarshal-8     0.000 ± 0%

                                         │      -       │
                                         │  allocs/op   │
MessageInitiationUnmarshal/binary.Read-8   2.000 ± 0%
MessageInitiationUnmarshal/unmarshal-8     0.000 ± 0%

Signed-off-by: Alexander Yastrebov <yastrebov.alex@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2025-05-15 16:42:06 +02:00
..
allowedips_rand_test.go device: use rand.NewSource instead of rand.Seed 2025-05-05 15:10:08 +02:00
allowedips_test.go device: use rand.NewSource instead of rand.Seed 2025-05-05 15:10:08 +02:00
allowedips.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
bind_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
channels.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
constants.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
cookie_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
cookie.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
device_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
device.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
devicestate_string.go device: remove deviceStateNew 2021-02-09 15:39:19 +01:00
endpoint_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
indextable.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
ip.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
kdf_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
keypair.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
logger.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
mobilequirks.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
noise_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
noise-helpers.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
noise-protocol.go device: reduce RoutineHandshake allocations 2025-05-15 16:42:06 +02:00
noise-types.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
peer.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
pools_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
pools.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
queueconstants_android.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
queueconstants_default.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
queueconstants_ios.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
queueconstants_windows.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
race_disabled_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
race_enabled_test.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
receive.go device: reduce RoutineHandshake allocations 2025-05-15 16:42:06 +02:00
send.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
sticky_default.go global: replaced unused function params with _ 2025-05-05 15:10:08 +02:00
sticky_linux.go global: replaced unused function params with _ 2025-05-05 15:10:08 +02:00
timers.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
tun.go global: bump copyright notice 2025-05-05 15:05:35 +02:00
uapi.go global: bump copyright notice 2025-05-05 15:05:35 +02:00