Skip to content

Commit 5ceebd3

Browse files
author
Overmind
committed
perf: Branchless BWT inverse — +6% faster (31→33 MB/s)
Replace conditional (r < pidx ? r : r-1) with branchless r - (r >= pidx). Compiler generates cmov instead of branch. 204 roundtrip tests pass.
1 parent 2b6eeb2 commit 5ceebd3

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

lib/preprocess/bwt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,20 @@ size_t mcx_bwt_inverse(uint8_t* dst, size_t primary_idx,
296296
}
297297

298298
/* Build LF mapping vector T.
299-
* Use uint32_t instead of size_t — block size ≤ 16MB fits in 32 bits.
300-
* Halves memory (80MB→40MB for 10MB block) and improves cache behavior. */
299+
* Use uint32_t — block size ≤ 64MB fits in 32 bits. */
301300
uint32_t* T = (uint32_t*)malloc(size * sizeof(uint32_t));
302301
if (T == NULL) return MCX_ERROR(MCX_ERR_ALLOC_FAILED);
303302

304303
for (i = 0; i < size; i++) {
305304
T[i] = (uint32_t)cumul[src[i]]++;
306305
}
307306

308-
/* Reconstruct original data tracing backwards from true row 0.
309-
* The missing sentinel is conceptually at primary_idx. */
307+
/* Reconstruct original data tracing backwards.
308+
* Branchless sentinel adjustment: idx_L = r - (r >= primary_idx) */
310309
uint32_t r = 0;
310+
uint32_t pidx32 = (uint32_t)primary_idx;
311311
for (i = 0; i < size; i++) {
312-
uint32_t idx_L = (r < (uint32_t)primary_idx) ? r : r - 1;
312+
uint32_t idx_L = r - (r >= pidx32);
313313
dst[size - 1 - i] = src[idx_L];
314314
r = T[idx_L] + 1;
315315
}

0 commit comments

Comments
 (0)