fix: convention fix, performance improvement in kcp::cstringstream

This commit is contained in:
2026-06-19 18:37:19 +02:00
parent d68dd6979d
commit 820b908b80
3 changed files with 26 additions and 13 deletions
+1 -1
View File
@@ -12,4 +12,4 @@
#pragma once #pragma once
#define MONTAUK_BUILD_NUMBER 127 #define MONTAUK_BUILD_NUMBER 128
+20 -8
View File
@@ -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."; Kt::KernelLogStream(Kt::ERROR, "kcp::cstringstream") << "Character streaming failed due to failed allocation.";
return *this; return *this;
} }
char* ref = (char *)&string[size]; this->string = grown;
*ref = c; this->capacity = newCapacity;
}
ref++;
*ref = '\0';
this->string[this->size] = c;
this->string[this->size + 1] = '\0';
this->size++; this->size++;
return *this; return *this;
+1
View File
@@ -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;