diff options
author | Jeff King <peff@peff.net> | 2021-03-30 18:04:36 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2021-04-01 23:07:37 +0300 |
commit | 30077524611cae8f25111e2c8b8d42136aa58787 (patch) | |
tree | 3f2baaf6817d22018f5d999dd3e70f37d2de57a2 | |
parent | 38ff7cabb6b8e51df78ce20c20632eba24265ee4 (diff) |
midx.c: improve cache locality in midx_pack_order_cmp()
There is a lot of pointer dereferencing in the pre-image version of
'midx_pack_order_cmp()', which this patch gets rid of.
Instead of comparing the pack preferred-ness and then the pack id, both
of these checks are done at the same time by using the high-order bit of
the pack id to represent whether it's preferred. Then the pack id and
offset are compared as usual.
This produces the same result so long as there are less than 2^31 packs,
which seems like a likely assumption to make in practice.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | midx.c | 55 |
1 files changed, 29 insertions, 26 deletions
@@ -818,46 +818,49 @@ static int write_midx_large_offsets(struct hashfile *f, return 0; } -static int midx_pack_order_cmp(const void *va, const void *vb, void *_ctx) -{ - struct write_midx_context *ctx = _ctx; - - struct pack_midx_entry *a = &ctx->entries[*(const uint32_t *)va]; - struct pack_midx_entry *b = &ctx->entries[*(const uint32_t *)vb]; - - uint32_t perm_a = ctx->pack_perm[a->pack_int_id]; - uint32_t perm_b = ctx->pack_perm[b->pack_int_id]; - - /* Sort objects in the preferred pack ahead of any others. */ - if (a->preferred > b->preferred) - return -1; - if (a->preferred < b->preferred) - return 1; +struct midx_pack_order_data { + uint32_t nr; + uint32_t pack; + off_t offset; +}; - /* Then, order objects by which packs they appear in. */ - if (perm_a < perm_b) +static int midx_pack_order_cmp(const void *va, const void *vb) +{ + const struct midx_pack_order_data *a = va, *b = vb; + if (a->pack < b->pack) return -1; - if (perm_a > perm_b) + else if (a->pack > b->pack) return 1; - - /* Then, disambiguate by their offset within each pack. */ - if (a->offset < b->offset) + else if (a->offset < b->offset) return -1; - if (a->offset > b->offset) + else if (a->offset > b->offset) return 1; - - return 0; + else + return 0; } static uint32_t *midx_pack_order(struct write_midx_context *ctx) { + struct midx_pack_order_data *data; uint32_t *pack_order; uint32_t i; + ALLOC_ARRAY(data, ctx->entries_nr); + for (i = 0; i < ctx->entries_nr; i++) { + struct pack_midx_entry *e = &ctx->entries[i]; + data[i].nr = i; + data[i].pack = ctx->pack_perm[e->pack_int_id]; + if (!e->preferred) + data[i].pack |= (1U << 31); + data[i].offset = e->offset; + } + + QSORT(data, ctx->entries_nr, midx_pack_order_cmp); + ALLOC_ARRAY(pack_order, ctx->entries_nr); for (i = 0; i < ctx->entries_nr; i++) - pack_order[i] = i; - QSORT_S(pack_order, ctx->entries_nr, midx_pack_order_cmp, ctx); + pack_order[i] = data[i].nr; + free(data); return pack_order; } |