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>
Jeff King committed
Mar 30, 2021 at 11:04 UTC
30077524611cae8f25111e2c8b8d42136aa58787
1 file changed
+29
-26
midx.c
+29
-26
@@ -818,46 +818,49 @@ static int write_midx_large_offsets(struct hashfile *f,
818
return 0;
819
}
820
821
-static int midx_pack_order_cmp(const void *va, const void *vb, void *_ctx)
822
-{
823
- struct write_midx_context *ctx = _ctx;
824
-
825
- struct pack_midx_entry *a = &ctx->entries[*(const uint32_t *)va];
826
- struct pack_midx_entry *b = &ctx->entries[*(const uint32_t *)vb];
827
-
828
- uint32_t perm_a = ctx->pack_perm[a->pack_int_id];
829
- uint32_t perm_b = ctx->pack_perm[b->pack_int_id];
830
-
831
- /* Sort objects in the preferred pack ahead of any others. */
832
- if (a->preferred > b->preferred)
833
- return -1;
834
- if (a->preferred < b->preferred)
835
- return 1;
821
+struct midx_pack_order_data {
822
+ uint32_t nr;
823
+ uint32_t pack;
824
+ off_t offset;
825
+};
826
837
- /* Then, order objects by which packs they appear in. */
838
- if (perm_a < perm_b)
827
+static int midx_pack_order_cmp(const void *va, const void *vb)
828
+{
829
+ const struct midx_pack_order_data *a = va, *b = vb;
830
+ if (a->pack < b->pack)
831
return -1;
840
- if (perm_a > perm_b)
832
+ else if (a->pack > b->pack)
833
return 1;
842
-
843
- /* Then, disambiguate by their offset within each pack. */
844
- if (a->offset < b->offset)
834
+ else if (a->offset < b->offset)
835
return -1;
846
- if (a->offset > b->offset)
836
+ else if (a->offset > b->offset)
837
return 1;
848
-
849
- return 0;
838
+ else
839
+ return 0;
840
}
841
842
static uint32_t *midx_pack_order(struct write_midx_context *ctx)
843
{
844
+ struct midx_pack_order_data *data;
845
uint32_t *pack_order;
846
uint32_t i;
847
848
+ ALLOC_ARRAY(data, ctx->entries_nr);
849
+ for (i = 0; i < ctx->entries_nr; i++) {
850
+ struct pack_midx_entry *e = &ctx->entries[i];
851
+ data[i].nr = i;
852
+ data[i].pack = ctx->pack_perm[e->pack_int_id];
853
+ if (!e->preferred)
854
+ data[i].pack |= (1U << 31);
855
+ data[i].offset = e->offset;
856
+ }
857
+
858
+ QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
859
+
860
ALLOC_ARRAY(pack_order, ctx->entries_nr);
861
for (i = 0; i < ctx->entries_nr; i++)
859
- pack_order[i] = i;
860
- QSORT_S(pack_order, ctx->entries_nr, midx_pack_order_cmp, ctx);
862
+ pack_order[i] = data[i].nr;
863
+ free(data);
864
865
return pack_order;
866
}