From 589cb500fa5a6df13fa9634d0071922ec11fce17 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 7 Nov 2026 19:39:01 +0100 Subject: Unify the peerlists for v4 and v6 This commit finishes what started with unifying v4 and v6 trackers, from then on, torrents carried a peer list per family, with obvious redundancies. With the race to save memory, these became burdening enough that now these peerlist pointers have been moved into a single struct, holding both v4 and v6 peers in their respective ot_vectors, so the per- torrent cost shrunk by another pointer's size. Each peerlist also lost its peer count cache, as in most cases, it's trivial to load it directly from the ot_vector's entry count. The seed counts that were kept per family only to be aggregated later for every consumer, are also merged now. Total savings depend on the tracker's actual load. In some corner cases, where only one address family dominates the torrents, we actually use a few bytes more in the ot_vector for the absent family, but even that mostly becomes noise: before it actually adds to the memory load, the peer payload will be the deciding factor. Most wins are on what often is experienced on trackers: A long tail of abandoned torrents now needs eight fewer bytes, slipping into a smaller allocation unit on glibc's allocator. --- tests/t_vector.c | 63 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) (limited to 'tests/t_vector.c') diff --git a/tests/t_vector.c b/tests/t_vector.c index f0a9bf7..42ce31a 100644 --- a/tests/t_vector.c +++ b/tests/t_vector.c @@ -200,17 +200,16 @@ static void case_insert_compare_prefix(void) { into the first address byte, so the peers are distinct but arrive in no particular order: a list generated in ascending order would come back sorted whether or not vector_redistribute_buckets sorted it. */ -static void build_flat_peerlist(ot_peerlist *peer_list, size_t count) { +static void build_flat_vector(ot_vector *peers, size_t count) { size_t i; - memset(peer_list, 0, sizeof(*peer_list)); - peer_list->peers.data = malloc(count * OT_PEER_SIZE4); - peer_list->peers.size = count; - peer_list->peers.space = count; - peer_list->peer_count = count; + memset(peers, 0, sizeof(*peers)); + peers->data = malloc(count * OT_PEER_SIZE4); + peers->size = count; + peers->space = count; for (i = 0; i < count; ++i) { - uint8_t *peer = (uint8_t *)peer_list->peers.data + i * OT_PEER_SIZE4; + uint8_t *peer = (uint8_t *)peers->data + i * OT_PEER_SIZE4; memset(peer, 0, OT_PEER_SIZE4); peer[0] = (uint8_t)i; @@ -222,19 +221,19 @@ static void build_flat_peerlist(ot_peerlist *peer_list, size_t count) { static void case_redistribute_holds_every_peer(void) { const size_t count = 513; - ot_peerlist peer_list; + ot_vector peers; size_t bucket, total_size = 0, empty = 0, largest = 0; ot_vector *buckets; test_case("a peer list past the threshold is split into buckets"); - build_flat_peerlist(&peer_list, count); - CHECK(peer_list.peers.data != NULL); - vector_redistribute_buckets(&peer_list, OT_PEER_SIZE4); - CHECK_INT(OT_PEERLIST_HASBUCKETS(&peer_list), 1); - CHECK_INT(peer_list.peers.size, 16); - - buckets = peer_list.peers.data; - for (bucket = 0; bucket < peer_list.peers.size; ++bucket) { + build_flat_vector(&peers, count); + CHECK(peers.data != NULL); + vector_redistribute_buckets(&peers, OT_PEER_SIZE4, count); + CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 1); + CHECK_INT(peers.size, 16); + + buckets = peers.data; + for (bucket = 0; bucket < peers.size; ++bucket) { total_size += buckets[bucket].size; if (!buckets[bucket].size) ++empty; @@ -252,23 +251,23 @@ static void case_redistribute_holds_every_peer(void) { CHECK_INT(empty, 0); CHECK(largest < count / 2); - vector_clean_list(peer_list.peers.data, peer_list.peers.size); + vector_clean_list(peers.data, peers.size); } static void case_redistribute_sorts_each_bucket(void) { const size_t count = 1000; - ot_peerlist peer_list; + ot_vector peers; size_t bucket, i, out_of_order = 0; ot_vector *buckets; test_case("every bucket comes back sorted, so bsearch can be used on it"); - build_flat_peerlist(&peer_list, count); - CHECK(peer_list.peers.data != NULL); - vector_redistribute_buckets(&peer_list, OT_PEER_SIZE4); - CHECK_INT(OT_PEERLIST_HASBUCKETS(&peer_list), 1); + build_flat_vector(&peers, count); + CHECK(peers.data != NULL); + vector_redistribute_buckets(&peers, OT_PEER_SIZE4, count); + CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 1); - buckets = peer_list.peers.data; - for (bucket = 0; bucket < peer_list.peers.size; ++bucket) + buckets = peers.data; + for (bucket = 0; bucket < peers.size; ++bucket) for (i = 1; i < buckets[bucket].size; ++i) { const uint8_t *prev = (uint8_t *)buckets[bucket].data + (i - 1) * OT_PEER_SIZE4; const uint8_t *next = (uint8_t *)buckets[bucket].data + i * OT_PEER_SIZE4; @@ -278,19 +277,19 @@ static void case_redistribute_sorts_each_bucket(void) { } CHECK_INT(out_of_order, 0); - vector_clean_list(peer_list.peers.data, peer_list.peers.size); + vector_clean_list(peers.data, peers.size); } static void case_redistribute_below_threshold(void) { - ot_peerlist peer_list; + ot_vector peers; test_case("a small peer list is left flat"); - build_flat_peerlist(&peer_list, 100); - CHECK(peer_list.peers.data != NULL); - vector_redistribute_buckets(&peer_list, OT_PEER_SIZE4); - CHECK_INT(OT_PEERLIST_HASBUCKETS(&peer_list), 0); - CHECK_INT(peer_list.peers.size, 100); - free(peer_list.peers.data); + build_flat_vector(&peers, 100); + CHECK(peers.data != NULL); + vector_redistribute_buckets(&peers, OT_PEER_SIZE4, 100); + CHECK_INT(OT_VECTOR_HASBUCKETS(&peers), 0); + CHECK_INT(peers.size, 100); + free(peers.data); } void test_vector(void) { -- cgit v1.3