From 9a93bf8d4a212b35ade4b61c707b7f3df608823e Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 6 Aug 2021 13:47:56 +0900 Subject: [PATCH] [qfvis] Make cluster reconstruction O(N) For most (if not all) maps. The heapsort is needed only if the clustered leafs are not contiguous, but most bsp compilers output contiguous leaf clusters, so is just a bit of protection. The difference isn't really noticeable on a fast machine, but no point in doing more work than necessary. --- tools/qfvis/source/fatpvs.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/qfvis/source/fatpvs.c b/tools/qfvis/source/fatpvs.c index 0cf42d462..faabace0a 100644 --- a/tools/qfvis/source/fatpvs.c +++ b/tools/qfvis/source/fatpvs.c @@ -260,15 +260,24 @@ static void reconstruct_clusters (void) { leafvis = malloc (num_leafs * sizeof (leafvis_t)); + int sorted = 1; + num_clusters = 1; for (unsigned i = 0; i < num_leafs; i++) { leafvis[i].visoffs = bsp->leafs[i + 1].visofs; leafvis[i].leafnum = i; + if (i > 0) { + num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs; + if (leafvis[i].visoffs < leafvis[i - 1].visoffs) { + sorted = 0; + } + } } - heapsort (leafvis, num_leafs, sizeof (leafvis_t), leaf_compare); - - num_clusters = 1; - for (unsigned i = 1; i < num_leafs; i++) { - num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs; + if (!sorted) { + heapsort (leafvis, num_leafs, sizeof (leafvis_t), leaf_compare); + num_clusters = 1; + for (unsigned i = 1; i < num_leafs; i++) { + num_clusters += leafvis[i].visoffs != leafvis[i - 1].visoffs; + } } leafcluster = malloc (num_leafs * sizeof (uint32_t));