"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"
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
"context"
"encoding/gob"
"fmt"
- "github.com/anacrolix/torrent/metainfo"
"reflect"
"runtime/pprof"
"time"
"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"
)
+++ /dev/null
-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)))
-}
--- /dev/null
+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)
+}