-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
http: implement slab allocation for HTTP header parsing #61375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
20bde47
9115556
5def150
56393d3
266bfdb
b9090c9
3d643fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,70 +124,76 @@ class BindingData : public BaseObject { | |
|
|
||
| // helper class for the Parser | ||
| struct StringPtr { | ||
| StringPtr() { | ||
| on_heap_ = false; | ||
| Reset(); | ||
| } | ||
|
|
||
| // Memory impact: ~8KB per parser (66 StringPtr × 128 bytes). | ||
| static constexpr size_t kSlabSize = 128; | ||
|
|
||
| ~StringPtr() { | ||
| Reset(); | ||
| } | ||
| StringPtr() = default; | ||
| ~StringPtr() { Reset(); } | ||
|
|
||
| StringPtr(const StringPtr&) = delete; | ||
| StringPtr& operator=(const StringPtr&) = delete; | ||
|
|
||
| // If str_ does not point to a heap string yet, this function makes it do | ||
| // so. This is called at the end of each http_parser_execute() so as not | ||
| // to leak references. See issue #2438 and test-http-parser-bad-ref.js. | ||
| void Save() { | ||
|
mertcanaltin marked this conversation as resolved.
Outdated
|
||
| if (!on_heap_ && size_ > 0) { | ||
| char* s = new char[size_]; | ||
| memcpy(s, str_, size_); | ||
| str_ = s; | ||
| on_heap_ = true; | ||
| if (!on_heap_ && !using_slab_ && size_ > 0) { | ||
| if (size_ <= kSlabSize) { | ||
| memcpy(slab_, str_, size_); | ||
| str_ = slab_; | ||
| using_slab_ = true; | ||
| } else { | ||
| char* s = new char[size_]; | ||
| memcpy(s, str_, size_); | ||
| str_ = s; | ||
| on_heap_ = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void Reset() { | ||
| if (on_heap_) { | ||
| delete[] str_; | ||
| on_heap_ = false; | ||
| } | ||
|
|
||
| using_slab_ = false; | ||
| str_ = nullptr; | ||
| size_ = 0; | ||
| } | ||
|
|
||
|
|
||
| void Update(const char* str, size_t size) { | ||
| if (str_ == nullptr) { | ||
| str_ = str; | ||
| } else if (on_heap_ || str_ + size_ != str) { | ||
| // Non-consecutive input, make a copy on the heap. | ||
| // TODO(bnoordhuis) Use slab allocation, O(n) allocs is bad. | ||
|
addaleax marked this conversation as resolved.
|
||
| char* s = new char[size_ + size]; | ||
| memcpy(s, str_, size_); | ||
| memcpy(s + size_, str, size); | ||
|
|
||
| if (on_heap_) | ||
| delete[] str_; | ||
| else | ||
| } else if (on_heap_ || using_slab_ || str_ + size_ != str) { | ||
| const size_t total = size_ + size; | ||
|
|
||
| if (!on_heap_ && total <= kSlabSize) { | ||
| if (!using_slab_) { | ||
| memcpy(slab_, str_, size_); | ||
| using_slab_ = true; | ||
| } | ||
| memcpy(slab_ + size_, str, size); | ||
| str_ = slab_; | ||
| } else { | ||
| char* s = new char[total]; | ||
| memcpy(s, str_, size_); | ||
| memcpy(s + size_, str, size); | ||
| if (on_heap_) delete[] str_; | ||
| on_heap_ = true; | ||
|
|
||
| str_ = s; | ||
| using_slab_ = false; | ||
| str_ = s; | ||
| } | ||
| } | ||
| size_ += size; | ||
| } | ||
|
|
||
|
|
||
| Local<String> ToString(Environment* env) const { | ||
| if (size_ != 0) | ||
| return OneByteString(env->isolate(), str_, size_); | ||
| else | ||
| return String::Empty(env->isolate()); | ||
| } | ||
|
|
||
|
|
||
| // Strip trailing OWS (SPC or HTAB) from string. | ||
| Local<String> ToTrimmedString(Environment* env) { | ||
| while (size_ > 0 && IsOWS(str_[size_ - 1])) { | ||
|
|
@@ -196,10 +202,11 @@ struct StringPtr { | |
| return ToString(env); | ||
| } | ||
|
|
||
|
|
||
| const char* str_; | ||
| bool on_heap_; | ||
| size_t size_; | ||
| const char* str_ = nullptr; | ||
| bool on_heap_ = false; | ||
| bool using_slab_ = false; | ||
| size_t size_ = 0; | ||
| char slab_[kSlabSize]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overall seems like we're re-inventing
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! , I've refactored this to use MaybeStackBuffer as the backing store for a allocator.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new benchmark result : asis (main): ./out/Release/node benchmark/http/bench-parser.js ./out/Release/node benchmark/http/bench-parser-fragmented.js new: ./out/Release/node benchmark/http/bench-parser.js ./out/Release/node benchmark/http/bench-parser-fragmented.js |
||
| }; | ||
|
|
||
| class Parser; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.