From 220278ee89b556521e4bda7530460405a4ec467d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 13 May 2025 20:45:24 +1000 Subject: [PATCH] Add a test for sizeof(Piece) --- piece.go | 6 +++--- requesting.go | 2 +- struct_test.go | 12 ------------ testing.go | 1 + type-sizes_test.go | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 16 deletions(-) delete mode 100644 struct_test.go create mode 100644 type-sizes_test.go diff --git a/piece.go b/piece.go index 17a4ed89..6e53fb25 100644 --- a/piece.go +++ b/piece.go @@ -4,11 +4,10 @@ import ( "context" "errors" "fmt" - "sync" - "github.com/anacrolix/chansync" g "github.com/anacrolix/generics" "github.com/anacrolix/missinggo/v2/bitmap" + "sync" "github.com/anacrolix/torrent/merkle" "github.com/anacrolix/torrent/metainfo" @@ -22,7 +21,8 @@ type pieceVerifyCount = int64 type Piece struct { // The completed piece SHA1 hash, from the metainfo "pieces" field. Nil if the info is not V1 // compatible. - hash *metainfo.Hash + hash *metainfo.Hash + // Not easy to use unique.Handle because we need this as a slice sometimes. hashV2 g.Option[[32]byte] t *Torrent index pieceIndex diff --git a/requesting.go b/requesting.go index 0d72733d..00a8a659 100644 --- a/requesting.go +++ b/requesting.go @@ -4,7 +4,6 @@ import ( "context" "encoding/gob" "fmt" - "github.com/anacrolix/torrent/metainfo" "reflect" "runtime/pprof" "time" @@ -16,6 +15,7 @@ import ( "github.com/anacrolix/log" "github.com/anacrolix/multiless" + "github.com/anacrolix/torrent/metainfo" requestStrategy "github.com/anacrolix/torrent/request-strategy" typedRoaring "github.com/anacrolix/torrent/typed-roaring" ) diff --git a/struct_test.go b/struct_test.go deleted file mode 100644 index cee91e10..00000000 --- a/struct_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package torrent - -import ( - "testing" - "unsafe" -) - -func TestStructSizes(t *testing.T) { - t.Log("[]*File", unsafe.Sizeof([]*File(nil))) - t.Log("Piece", unsafe.Sizeof(Piece{})) - t.Log("map[*peer]struct{}", unsafe.Sizeof(map[*Peer]struct{}(nil))) -} diff --git a/testing.go b/testing.go index ac70b1c9..a4909273 100644 --- a/testing.go +++ b/testing.go @@ -5,6 +5,7 @@ import ( "time" "github.com/anacrolix/log" + pp "github.com/anacrolix/torrent/peer_protocol" ) diff --git a/type-sizes_test.go b/type-sizes_test.go new file mode 100644 index 00000000..116d2aa2 --- /dev/null +++ b/type-sizes_test.go @@ -0,0 +1,37 @@ +package torrent + +import ( + "github.com/anacrolix/chansync" + "reflect" + "testing" + + g "github.com/anacrolix/generics" + "github.com/go-quicktest/qt" +) + +func testSizeof[T any](t *testing.T, max g.Option[uintptr]) { + ty := reflect.TypeFor[T]() + size := ty.Size() + t.Logf("%v has size %v", ty, size) + if max.Ok { + qt.Check(t, qt.IsTrue(size <= max.Value), qt.Commentf("size of %v is %v, expected <= %v", ty, size, max.Value)) + } +} + +func checkSizeLessThan[T any](t *testing.T, max uintptr) { + testSizeof[T](t, g.Some(max)) +} + +func justLogSizeof[T any](t *testing.T) { + testSizeof[T](t, g.None[uintptr]()) +} + +func TestTypeSizes(t *testing.T) { + justLogSizeof[[]*File](t) + checkSizeLessThan[Piece](t, 296) + justLogSizeof[map[*Peer]struct{}](t) + justLogSizeof[chansync.BroadcastCond](t) + + justLogSizeof[g.Option[[32]byte]](t) + justLogSizeof[[]byte](t) +} -- 2.51.0