From ca4f28e03a6091a5427684ccdb3751bdcdf1226e Mon Sep 17 00:00:00 2001 From: geolffreym Date: Tue, 16 Apr 2024 08:47:52 -0600 Subject: [PATCH] docs: added details about memoru alignment in structs --- events.go | 3 +-- metrics.go | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/events.go b/events.go index 034ffd8..ac5d643 100644 --- a/events.go +++ b/events.go @@ -8,8 +8,7 @@ import ( // byteToString convert an array of bytes to a string with no-copy strategy. func bytesToString(b []byte) string { // Optimizing space with ordered types. - // perf: no allocation/copy to convert to string. - // instead take the already existing byte slice to create a string struct. + // perf: no allocation/copy to convert to string instead take the already existing byte slice to create a string struct. // WARNING: use this approach with caution and only if we are sure that the bytes slice is not gonna change. return *(*string)(unsafe.Pointer(&b)) } diff --git a/metrics.go b/metrics.go index 0d90f88..879251a 100644 --- a/metrics.go +++ b/metrics.go @@ -4,16 +4,25 @@ package noise // We can add any method related to adaptive lookup logic here. // Please see [docs] for more information. // +// !IMPORTANT The order of byte size needed for each type in structs matter and impact the struct size. +// The fields are distributed in a way that ensures their alignment in 8-byte blocks. +// For instance on a 64-bit CPU, alignment blocks are 8 bytes. +// +// 0: [latency, bandwidth, nonce, sent], // 8 bytes +// 1: [recv, handshakeTime, 2bytespadding], // 8 bytes +// 2: [bytesRecv], // 8 bytes +// 3: [bytesSent], // 8 bytes +// // [docs]: https://arxiv.org/pdf/1509.04417.pdf type metrics struct { - bytesRecv uint64 // bytes received - bytesSent uint64 // bytes sent - handshakeTime uint32 // how long took the handshake to complete. - latency uint16 // rtt in ms - bandwidth uint16 // remote peer bandwidth - nonce uint16 // nonce ordering factor - sent uint16 // counter sent messages - recv uint16 // counter received messages + latency uint16 // rtt in ms: 2bytes + bandwidth uint16 // remote peer bandwidth: 2bytes + nonce uint16 // nonce ordering factor: 2bytes + sent uint16 // counter sent messages: 2bytes + recv uint16 // counter received messages: 2bytes + handshakeTime uint32 // how long took the handshake to complete.: 4bytes + bytesRecv uint64 // bytes received: 8bytes + bytesSent uint64 // bytes sent: 8 bytes } // TODO https://community.f5.com/t5/technical-articles/introducing-tcp-analytics/ta-p/290873