From c9444751604775d555ce8d200c2b5c083c3ab5cc Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 8 Oct 2022 20:13:44 +0700 Subject: Tweak allocation strategy --- crypto/src/tls/ByteQueue.cs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs index 74a7104ba..e06ad6346 100644 --- a/crypto/src/tls/ByteQueue.cs +++ b/crypto/src/tls/ByteQueue.cs @@ -9,18 +9,9 @@ namespace Org.BouncyCastle.Tls public sealed class ByteQueue { /// The smallest number which can be written as 2^x which is bigger than i. - public static int NextTwoPow(int i) + private static int GetAllocationSize(int i) { - /* - * This code is based of a lot of code I found on the Internet which mostly - * referenced a book called "Hacking delight". - */ - i |= i >> 1; - i |= i >> 2; - i |= i >> 4; - i |= i >> 8; - i |= i >> 16; - return i + 1; + return Integers.HighestOneBit((256 | i) << 1); } /// The buffer where we store our data. @@ -68,14 +59,14 @@ namespace Org.BouncyCastle.Tls { if (len > m_databuf.Length) { - int desiredSize = NextTwoPow(len | 256); + int desiredSize = GetAllocationSize(len); m_databuf = new byte[desiredSize]; } m_skipped = 0; } else if ((m_skipped + m_available + len) > m_databuf.Length) { - int desiredSize = NextTwoPow(m_available + len); + int desiredSize = GetAllocationSize(m_available + len); if (desiredSize > m_databuf.Length) { byte[] tmp = new byte[desiredSize]; @@ -105,14 +96,14 @@ namespace Org.BouncyCastle.Tls { if (len > m_databuf.Length) { - int desiredSize = NextTwoPow(len | 256); + int desiredSize = GetAllocationSize(len); m_databuf = new byte[desiredSize]; } m_skipped = 0; } else if ((m_skipped + m_available + len) > m_databuf.Length) { - int desiredSize = NextTwoPow(m_available + len); + int desiredSize = GetAllocationSize(m_available + len); if (desiredSize > m_databuf.Length) { byte[] tmp = new byte[desiredSize]; @@ -259,7 +250,7 @@ namespace Org.BouncyCastle.Tls } else { - int desiredSize = NextTwoPow(m_available); + int desiredSize = GetAllocationSize(m_available); if (desiredSize < m_databuf.Length) { byte[] tmp = new byte[desiredSize]; -- cgit 1.4.1