From 79ff0fe1f517751eaf15147739e6153dec03e379 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 20 Nov 2022 09:54:03 +0000 Subject: [PATCH] Use binary search in jit_block_for --- src/jit/jit-optim.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/jit/jit-optim.c b/src/jit/jit-optim.c index a1ca2936..de9812af 100644 --- a/src/jit/jit-optim.c +++ b/src/jit/jit-optim.c @@ -229,10 +229,15 @@ void jit_free_cfg(jit_func_t *f) jit_block_t *jit_block_for(jit_cfg_t *cfg, int pos) { - // TODO: do binary search - for (int i = 0; i < cfg->nblocks; i++) { - jit_block_t *bb = &(cfg->blocks[i]); - if (pos >= bb->first && pos <= bb->last) + for (int low = 0, high = cfg->nblocks - 1; low <= high; ) { + const int mid = (low + high) / 2; + jit_block_t *bb = &(cfg->blocks[mid]); + + if (bb->last < pos) + low = mid + 1; + else if (bb->first > pos) + high = mid - 1; + else return bb; } -- 2.39.2