From b703eee96e9f2f9db8c98423f1f25d8051f99586 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 9 Aug 2021 10:33:47 -0700 Subject: [PATCH] Fix obvious Python performance suboptimality --- third_party/python/Parser/node.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/third_party/python/Parser/node.c b/third_party/python/Parser/node.c index 240d29057..f4411325c 100644 --- a/third_party/python/Parser/node.c +++ b/third_party/python/Parser/node.c @@ -1,7 +1,9 @@ /* Parse tree node implementation */ +/* clang-format off */ #include "Python.h" #include "node.h" +#include "libc/nexgen32e/bsr.h" #include "errcode.h" node * @@ -20,17 +22,13 @@ PyNode_New(int type) /* See comments at XXXROUNDUP below. Returns -1 on overflow. */ static int -fancy_roundup(int n) +fancy_roundup(int x) { /* Round up to the closest power of 2 >= n. */ - int result = 256; - assert(n > 128); - while (result < n) { - result <<= 1; - if (result <= 0) - return -1; - } - return result; + int r; + assert(x > 128); + r = 1u << (bsr(x - 1) + 1); /* hacker's delight */ + return r > 0 ? r : -1; } /* A gimmick to make massive numbers of reallocs quicker. The result is