fix: convention fix, performance improvement in kcp::cstringstream
This commit is contained in:
@@ -12,4 +12,4 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define MONTAUK_BUILD_NUMBER 127
|
#define MONTAUK_BUILD_NUMBER 128
|
||||||
|
|||||||
@@ -13,28 +13,40 @@ kcp::cstringstream::cstringstream()
|
|||||||
{
|
{
|
||||||
this->string = nullptr;
|
this->string = nullptr;
|
||||||
this->size = 0;
|
this->size = 0;
|
||||||
|
this->capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
kcp::cstringstream::~cstringstream()
|
kcp::cstringstream::~cstringstream()
|
||||||
{
|
{
|
||||||
delete this->string;
|
// string is allocated via g_heap->Realloc, so it must be released through
|
||||||
|
// the heap allocator, not operator delete.
|
||||||
|
if (this->string != nullptr) {
|
||||||
|
Memory::g_heap->Free(this->string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
|
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
|
||||||
this->string = (char *)Memory::g_heap->Realloc((void *)this->string, this->size + 2);
|
// Grow geometrically (capacity doubles) so appending N chars is amortized
|
||||||
|
// O(N) instead of a Realloc on every single character.
|
||||||
|
if (this->capacity < this->size + 2) {
|
||||||
|
std::size_t newCapacity = (this->capacity == 0) ? 16 : (this->capacity * 2);
|
||||||
|
if (newCapacity < this->size + 2) {
|
||||||
|
newCapacity = this->size + 2;
|
||||||
|
}
|
||||||
|
|
||||||
if (this->string == nullptr)
|
char* grown = (char *)Memory::g_heap->Realloc((void *)this->string, newCapacity);
|
||||||
{
|
if (grown == nullptr)
|
||||||
Kt::KernelLogStream(Kt::ERROR, "kcp::cstringstream") << "Character streaming failed due to failed allocation.";
|
{
|
||||||
return *this;
|
Kt::KernelLogStream(Kt::ERROR, "kcp::cstringstream") << "Character streaming failed due to failed allocation.";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->string = grown;
|
||||||
|
this->capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* ref = (char *)&string[size];
|
this->string[this->size] = c;
|
||||||
*ref = c;
|
this->string[this->size + 1] = '\0';
|
||||||
|
|
||||||
ref++;
|
|
||||||
*ref = '\0';
|
|
||||||
|
|
||||||
this->size++;
|
this->size++;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ namespace kcp {
|
|||||||
class cstringstream {
|
class cstringstream {
|
||||||
char* string;
|
char* string;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
|
std::size_t capacity;
|
||||||
|
|
||||||
base current_base = base::dec;
|
base current_base = base::dec;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user