Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
optimize StringPtrAllocator memory management
  • Loading branch information
mertcanaltin committed Jan 21, 2026
commit b9090c952343849d646176909f5091a8dcbfdde1
18 changes: 10 additions & 8 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,29 @@ class StringPtrAllocator {
// Memory impact: ~8KB per parser (66 StringPtr × 128 bytes).
Comment thread
anonrig marked this conversation as resolved.
static constexpr size_t kSlabSize = 8192;

StringPtrAllocator() { buffer_.SetLength(0); }
StringPtrAllocator() = default;

// Allocate memory from the slab. Returns nullptr if full.
char* TryAllocate(size_t size) {
const size_t current = buffer_.length();
if (current + size > kSlabSize) {
if (length_ + size > kSlabSize) {
return nullptr;
}
buffer_.SetLength(current + size);
return buffer_.out() + current;
char* ptr = buffer_ + length_;
length_ += size;
return ptr;
}

// Check if pointer is within this allocator's buffer.
bool Contains(const char* ptr) const {
return ptr >= buffer_.out() && ptr < buffer_.out() + buffer_.capacity();
return ptr >= buffer_ && ptr < buffer_ + kSlabSize;
}

// Reset allocator for new message.
void Reset() { buffer_.SetLength(0); }
void Reset() { length_ = 0; }

private:
MaybeStackBuffer<char, kSlabSize> buffer_;
char buffer_[kSlabSize];
size_t length_ = 0;
};

struct StringPtr {
Expand Down
Loading