From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E2DE21F44D for ; Sat, 6 Apr 2024 21:49:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1712440185; bh=2h7ntLNQtIV9o0anDyfcJT0ZHjKgb8DEWS6xxBk/LX4=; h=From:To:Subject:Date:From; b=iZk2OpMqF7Ulh/cUAngOk4UKEk1KLIriQ4eTUn39ioSFjm7TrP2ITbRA/DhGoQVHj dIodsIRUczrMTIsgsD924ar+1jzaRnPGMK3MLSEED5Gf439JIr0XFYYyxSlzFDi2mv u/Sxg4BSN737A2vlpsEagj+3753EAL0CI7+W4OC0= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH] reduce branches for size class calculation Date: Sat, 6 Apr 2024 21:49:44 +0000 Message-ID: <20240406214944.159562-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We can use bit twiddling to find the next power-of-two without branching for each iteration of the loop (since the new loop is simple enough for a compiler to unroll). --- mwrap_core.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/mwrap_core.h b/mwrap_core.h index 78c14e3..2a50e66 100644 --- a/mwrap_core.h +++ b/mwrap_core.h @@ -676,17 +676,13 @@ static inline size_t size_class_pad(size_t bytes) * minimize fragmentation. This means we pay a 0-20% overhead on * the initial allocations to improve the likelyhood of reuse. */ - size_t max = sizeof(void *) << 5; - size_t nxt; - do { - if (bytes <= max) { - size_t sc_bytes = size_align(bytes, max >> 3); - return sc_bytes <= mm_thresh ? sc_bytes : bytes; - } - nxt = max << 1; - } while (nxt > max && nxt < mm_thresh && (max = nxt)); + size_t np2 = bytes - 1; + for (size_t i = 1; i < sizeof(size_t) * CHAR_BIT; i <<= 1) + np2 |= np2 >> i; + ++np2; // next power-of-two - return bytes; + size_t sc_bytes = size_align(bytes, np2 >> 3); + return sc_bytes < mm_thresh ? sc_bytes : mm_thresh; } void *malloc(size_t size)