From a151ef9795c6fb838b56c530c094fe048beec6b2 Mon Sep 17 00:00:00 2001 From: Kiyon Date: Thu, 19 Nov 2020 14:31:33 +0800 Subject: [PATCH] Improve round2 performance --- http.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/http.go b/http.go index 5e3d7d3694..feb63f7f91 100644 --- a/http.go +++ b/http.go @@ -1956,11 +1956,13 @@ func round2(n int) int { if n <= 0 { return 0 } - n-- - x := uint(0) - for n > 0 { - n >>= 1 - x++ - } - return 1 << x + + x := uint32(n - 1) + x |= x >> 1 + x |= x >> 2 + x |= x >> 4 + x |= x >> 8 + x |= x >> 16 + + return int(x + 1) }