Skip to content

Commit

Permalink
Change to using std::min() when allocating Buffers on the C++ side. T…
Browse files Browse the repository at this point in the history
…his will result in Buffers being allocated to the size requested up to kMaxLength (0x3fffffff) instead of the current behavior of always allocating kMaxLength. Since these buffers are allocated in the node external memory (limited to 64M) the old behavior would very quickly cause massive GC pressure as the GC is triggered on each new Buffer allocation once the node external memory limit is reached. It appears the old behavior was not intentional and should have always been std::min(). This should fix node-ffi-napi#72
  • Loading branch information
doggkruse authored and dfreertandem committed Aug 30, 2023
1 parent a7f62a4 commit 8193f14
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class InstanceData final : public RefNapi::Instance {
ab = it->second.ab.Value();

if (ab.IsEmpty()) {
length = std::max<size_t>(length, kMaxLength);
length = std::min<size_t>(length, kMaxLength);
ab = Buffer<char>::New(env, ptr, length, [this](Env env, char* ptr) {
UnregisterArrayBuffer(ptr);
}).ArrayBuffer();
Expand Down

0 comments on commit 8193f14

Please sign in to comment.