]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add a test for sizeof(Piece)
authorMatt Joiner <anacrolix@gmail.com>
Tue, 13 May 2025 10:45:24 +0000 (20:45 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 13 May 2025 10:45:24 +0000 (20:45 +1000)
piece.go
requesting.go
struct_test.go [deleted file]
testing.go
type-sizes_test.go [new file with mode: 0644]

index 17a4ed8940e6f835a4cdbb1ec13b8cf86f1183f2..6e53fb252a6de4b5025a07a00b0aed022410326c 100644 (file)
--- 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
index 0d72733d59517e091c036afee5b478c81f91949f..00a8a6597dfccafa8fc6b96a3a6200a9ca5cd1c3 100644 (file)
@@ -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 (file)
index cee91e1..0000000
+++ /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)))
-}
index ac70b1c9e2e19def0eb17124a1ba7749dbfc70a5..a49092730814fa8632f8db3c2f3c0caa083b2b32 100644 (file)
@@ -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 (file)
index 0000000..116d2aa
--- /dev/null
@@ -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)
+}