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. --- src/trackerlogic.c | 118 +++++++++++++++++++++++++---------------------------- 1 file changed, 55 insertions(+), 63 deletions(-) (limited to 'src/trackerlogic.c') diff --git a/src/trackerlogic.c b/src/trackerlogic.c index 6b0659c..4ba3a41 100644 --- a/src/trackerlogic.c +++ b/src/trackerlogic.c @@ -28,13 +28,18 @@ /* Forward declaration */ size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto); +void free_peer_vector(ot_vector *peer_vector) { + if (OT_VECTOR_HASBUCKETS(peer_vector)) + vector_clean_list((ot_vector *)peer_vector->data, peer_vector->size); + else + free(peer_vector->data); /* free(NULL) on an absent family is fine */ +} + void free_peerlist(ot_peerlist *peer_list) { - if (peer_list && peer_list->peers.data) { - if (OT_PEERLIST_HASBUCKETS(peer_list)) - vector_clean_list((ot_vector *)peer_list->peers.data, peer_list->peers.size); - else - free(peer_list->peers.data); - } + if (!peer_list) + return; + free_peer_vector(&peer_list->peers6); + free_peer_vector(&peer_list->peers4); free(peer_list); } @@ -113,20 +118,17 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru torrent->base = time_now_minutes(); - /* The announcing peer's family list is created on first use */ - { - ot_peerlist **slot = peer_size == OT_PEER_SIZE6 ? &torrent->peer_list6 : &torrent->peer_list4; - if (!*slot && !(*slot = calloc(1, sizeof(ot_peerlist)))) { - if (delta_torrentcount) - vector_remove_torrent(torrents_list, torrent); - mutex_bucket_unlock_by_hash(*ws->hash, 0); - return 0; - } - peer_list = *slot; + /* The peerlist, holding both families, is created on the first peer */ + if (!torrent->peer_list && !(torrent->peer_list = calloc(1, sizeof(ot_peerlist)))) { + if (delta_torrentcount) + vector_remove_torrent(torrents_list, torrent); + mutex_bucket_unlock_by_hash(*ws->hash, 0); + return 0; } + peer_list = torrent->peer_list; - /* Check for peer in torrent */ - peer_dest = vector_find_or_insert_peer(&(peer_list->peers), peer_src, peer_size, &exactmatch); + /* Check for peer in its family's vector */ + peer_dest = vector_find_or_insert_peer(peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4, peer_src, peer_size, &exactmatch); if (!peer_dest) { mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); return 0; @@ -149,7 +151,6 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru livesync_tell(ws); #endif - peer_list->peer_count++; if (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED) { torrent->down_count++; stats_issue_event(ws, EVENT_COMPLETED, 0, 0); @@ -198,14 +199,14 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru return ws->reply_size; } -static size_t return_peers_all(ot_peerlist *peer_list, size_t peer_size, char *reply) { +static size_t return_peers_all(ot_vector *peer_vector, size_t peer_size, size_t peer_count, char *reply) { size_t bucket, num_buckets = 1; - ot_vector *bucket_list = &peer_list->peers; + ot_vector *bucket_list = peer_vector; size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); - size_t result = compare_size * peer_list->peer_count; + size_t result = compare_size * peer_count; char *r_end = reply + result; - if (OT_PEERLIST_HASBUCKETS(peer_list)) { + if (OT_VECTOR_HASBUCKETS(peer_vector)) { num_buckets = bucket_list->size; bucket_list = (ot_vector *)bucket_list->data; } @@ -227,17 +228,17 @@ static size_t return_peers_all(ot_peerlist *peer_list, size_t peer_size, char *r return result; } -static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer_list, size_t peer_size, size_t amount, char *reply) { +static size_t return_peers_selection(struct ot_workstruct *ws, ot_vector *peer_vector, size_t peer_size, size_t amount, size_t peer_count, char *reply) { size_t bucket_offset, bucket_index = 0, num_buckets = 1; - ot_vector *bucket_list = &peer_list->peers; - unsigned int shifted_pc = peer_list->peer_count; + ot_vector *bucket_list = peer_vector; + unsigned int shifted_pc = peer_count; unsigned int shifted_step = 0; unsigned int shift = 0; size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); size_t result = compare_size * amount; char *r_end = reply + result; - if (OT_PEERLIST_HASBUCKETS(peer_list)) { + if (OT_VECTOR_HASBUCKETS(peer_vector)) { num_buckets = bucket_list->size; bucket_list = (ot_vector *)bucket_list->data; } @@ -253,7 +254,7 @@ static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer /* Initialize somewhere in the middle of peers so that fixpoint's aliasing doesn't alway miss the same peers */ - bucket_offset = nrand48(ws->rand48_state) % peer_list->peer_count; + bucket_offset = nrand48(ws->rand48_state) % peer_count; while (amount--) { ot_peer *peer; @@ -281,10 +282,11 @@ static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) { char *r = reply; size_t peer_size = peer_size_from_peer6(&ws->peer); - ot_peerlist *peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; - size_t list_count = peerlist_peer_count(peer_list); - size_t peer_count = peerlist_peer_count(torrent->peer_list6) + peerlist_peer_count(torrent->peer_list4); - size_t seed_count = peerlist_seed_count(torrent->peer_list6) + peerlist_seed_count(torrent->peer_list4); + ot_peerlist *peer_list = torrent->peer_list; + ot_vector *family = peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4; + size_t list_count = vector_peer_count(family); + size_t peer_count = torrent_peer_count(torrent); + size_t seed_count = torrent_seed_count(torrent); if (amount > list_count) amount = list_count; @@ -296,9 +298,9 @@ static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent if (amount) { if (amount == list_count) - r += return_peers_all(peer_list, peer_size, r); + r += return_peers_all(family, peer_size, list_count, r); else - r += return_peers_selection(ws, peer_list, peer_size, amount, r); + r += return_peers_selection(ws, family, peer_size, amount, list_count, r); } return r - reply; } @@ -307,9 +309,10 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent char *r = reply; const char *r_end = ws->outbuf + G_OUTBUF_SIZE; int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; - size_t count6 = peerlist_peer_count(torrent->peer_list6); - size_t count4 = peerlist_peer_count(torrent->peer_list4); - size_t seed_count = peerlist_seed_count(torrent->peer_list6) + peerlist_seed_count(torrent->peer_list4); + ot_peerlist *peer_list = torrent->peer_list; + size_t count6 = vector_peer_count(&peer_list->peers6); + size_t count4 = vector_peer_count(&peer_list->peers4); + size_t seed_count = peer_list->seed_count; size_t down_count = torrent->down_count; size_t peer_count = count6 + count4 - seed_count; @@ -355,17 +358,17 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent if (amount_v4) { fmt_append(&r, r_end, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4); if (amount_v4 == count4) - r += return_peers_all(torrent->peer_list4, OT_PEER_SIZE4, r); + r += return_peers_all(&peer_list->peers4, OT_PEER_SIZE4, count4, r); else - r += return_peers_selection(ws, torrent->peer_list4, OT_PEER_SIZE4, amount_v4, r); + r += return_peers_selection(ws, &peer_list->peers4, OT_PEER_SIZE4, amount_v4, count4, r); } if (amount_v6) { fmt_append(&r, r_end, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6); if (amount_v6 == count6) - r += return_peers_all(torrent->peer_list6, OT_PEER_SIZE6, r); + r += return_peers_all(&peer_list->peers6, OT_PEER_SIZE6, count6, r); else - r += return_peers_selection(ws, torrent->peer_list6, OT_PEER_SIZE6, amount_v6, r); + r += return_peers_selection(ws, &peer_list->peers6, OT_PEER_SIZE6, amount_v6, count6, r); } fmt_append(&r, r_end, "e"); @@ -401,9 +404,9 @@ size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) { memset(reply, 0, 12); delta_torrentcount = -1; } else { - r[0] = htonl(peerlist_seed_count(torrent->peer_list6) + peerlist_seed_count(torrent->peer_list4)); + r[0] = htonl(torrent_seed_count(torrent)); r[1] = htonl(torrent->down_count); - r[2] = htonl(peerlist_peer_count(torrent->peer_list6) + peerlist_peer_count(torrent->peer_list4) - peerlist_seed_count(torrent->peer_list6) - peerlist_seed_count(torrent->peer_list4)); + r[2] = htonl(torrent_peer_count(torrent) - torrent_seed_count(torrent)); } } mutex_bucket_unlock_by_hash(hash, delta_torrentcount); @@ -430,8 +433,8 @@ void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char } else { fmt_append(reply, reply_end, "20:"); fmt_bytes(reply, reply_end, hash, sizeof(ot_hash)); - size_t seed_count = peerlist_seed_count(torrent->peer_list6) + peerlist_seed_count(torrent->peer_list4); - size_t peer_count = peerlist_peer_count(torrent->peer_list6) + peerlist_peer_count(torrent->peer_list4); + size_t seed_count = torrent_seed_count(torrent); + size_t peer_count = torrent_peer_count(torrent); fmt_append(reply, reply_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, (size_t)torrent->down_count, peer_count - seed_count); } } @@ -441,12 +444,10 @@ void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char fmt_append(reply, reply_end, "ee"); } -static ot_peerlist dummy_list; size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { int exactmatch; ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); ot_torrent *torrent = vector_binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); - ot_peerlist *peer_list = &dummy_list; size_t peer_size; /* initialized in next line */ ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size); size_t peer_count = 0, seed_count = 0; @@ -458,21 +459,13 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { } #endif - if (exactmatch) { - peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; - if (peer_list) { - switch (vector_remove_peer(&peer_list->peers, peer_src, peer_size)) { - case 2: - peer_list->seed_count--; /* Intentional fallthrough */ - case 1: - peer_list->peer_count--; /* Intentional fallthrough */ - default: - break; - } - } + if (exactmatch && torrent->peer_list) { + ot_vector *family = peer_size == OT_PEER_SIZE6 ? &torrent->peer_list->peers6 : &torrent->peer_list->peers4; + if (vector_remove_peer(family, peer_src, peer_size) == 2) + torrent->peer_list->seed_count--; - peer_count = peerlist_peer_count(torrent->peer_list6) + peerlist_peer_count(torrent->peer_list4); - seed_count = peerlist_seed_count(torrent->peer_list6) + peerlist_seed_count(torrent->peer_list4); + peer_count = torrent_peer_count(torrent); + seed_count = torrent_seed_count(torrent); } if (proto == FLAG_TCP) { @@ -621,8 +614,7 @@ void trackerlogic_deinit(void) { if (torrents_list->size) { for (j = 0; j < torrents_list->size; ++j) { ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + j; - free_peerlist(torrent->peer_list6); - free_peerlist(torrent->peer_list4); + free_peerlist(torrent->peer_list); delta_torrentcount -= 1; } free(torrents_list->data); -- cgit v1.3