mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-02-08 21:04:26 +00:00
Allocate memory on stack whenever feasible
The fast path gives another 20% speed up than the slower path.
This commit is contained in:
parent
4e2daf117b
commit
aafee60033
@ -47,9 +47,22 @@ namespace BitTorrent::LT
|
|||||||
{
|
{
|
||||||
QBitArray toQBitArray(const lt::bitfield &bits)
|
QBitArray toQBitArray(const lt::bitfield &bits)
|
||||||
{
|
{
|
||||||
|
const int STACK_ALLOC_SIZE = 10 * 1024;
|
||||||
|
|
||||||
const char *bitsData = bits.data();
|
const char *bitsData = bits.data();
|
||||||
const int dataLength = (bits.size() + 7) / 8;
|
const int dataLength = (bits.size() + 7) / 8;
|
||||||
|
|
||||||
|
if (dataLength <= STACK_ALLOC_SIZE)
|
||||||
|
{
|
||||||
|
// fast path for small bitfields
|
||||||
|
char tmp[STACK_ALLOC_SIZE]; // uninitialized for faster allocation
|
||||||
|
for (int i = 0; i < dataLength; ++i)
|
||||||
|
tmp[i] = reverseByte(bitsData[i]);
|
||||||
|
|
||||||
|
return QBitArray::fromBits(tmp, bits.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// slow path for big bitfields
|
||||||
auto tmp = std::make_unique<char []>(dataLength);
|
auto tmp = std::make_unique<char []>(dataLength);
|
||||||
for (int i = 0; i < dataLength; ++i)
|
for (int i = 0; i < dataLength; ++i)
|
||||||
tmp[i] = reverseByte(bitsData[i]);
|
tmp[i] = reverseByte(bitsData[i]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user