Skip to content
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

Adjust external allocated memory less often #429

Merged
Merged
Changes from 2 commits
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
39 changes: 37 additions & 2 deletions src/fibers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ class Fiber {
static vector<Fiber*> orphaned_fibers;
static Persistent<Value> fatal_stack;

static size_t external_bytes_used;
static size_t external_bytes_reported;
// We report external memory usage only when the difference
// between used and reported exceeds a threshold, to avoid calling
// uni::AdjustAmountOfExternalAllocatedMemory too often.
static int external_bytes_threshold_factor;

Isolate* isolate;
Persistent<Object> handle;
Persistent<Function> cb;
Expand Down Expand Up @@ -578,7 +585,7 @@ class Fiber {
THROW(Exception::RangeError, "Out of memory");
}
that.started = true;
uni::AdjustAmountOfExternalAllocatedMemory(that.isolate, that.this_fiber->size() * GC_ADJUST);
AdjustExternalMemoryOnFiberStart(that);
} else {
// If the fiber is currently running put the first parameter to `run()` on `yielded`, then
// the pending call to `yield()` will return that value. `yielded` in this case is just a
Expand Down Expand Up @@ -760,7 +767,7 @@ class Fiber {

// Do not invoke the garbage collector if there's no context on the stack. It will seg fault
// otherwise.
uni::AdjustAmountOfExternalAllocatedMemory(that.isolate, -(int)(that.this_fiber->size() * GC_ADJUST));
AdjustExternalMemoryOnFiberExit(that);

// Don't make weak until after notifying the garbage collector. Otherwise it may try and
// free this very fiber!
Expand Down Expand Up @@ -859,6 +866,26 @@ class Fiber {
return uni::Return(uni::NewNumber(Isolate::GetCurrent(), Coroutine::coroutines_created()), info);
}

static void AdjustExternalMemoryOnFiberStart(const Fiber& fiber) {
int size = fiber.this_fiber->size();
external_bytes_used += size * GC_ADJUST;
int diff = external_bytes_used - external_bytes_reported;
if (diff > size * external_bytes_threshold_factor) {
uni::AdjustAmountOfExternalAllocatedMemory(fiber.isolate, diff);
external_bytes_reported = external_bytes_used;
}
}

static void AdjustExternalMemoryOnFiberExit(const Fiber& fiber) {
int size = fiber.this_fiber->size();
external_bytes_used -= size * GC_ADJUST;
int diff = external_bytes_used - external_bytes_reported;
if (-diff > size * external_bytes_threshold_factor) {
uni::AdjustAmountOfExternalAllocatedMemory(fiber.isolate, diff);
external_bytes_reported = external_bytes_used;
}
}
benjamn marked this conversation as resolved.
Show resolved Hide resolved

public:
/**
* Initialize the Fiber library.
Expand Down Expand Up @@ -918,6 +945,14 @@ Locker* Fiber::global_locker;
Fiber* Fiber::current = NULL;
vector<Fiber*> Fiber::orphaned_fibers;
Persistent<Value> Fiber::fatal_stack;

// Used by AdjustExternalMemoryOnFiber{Start,Exit} to avoid calling
// uni::AdjustAmountOfExternalAllocatedMemory every time a Fiber starts
// or exits.
size_t Fiber::external_bytes_used = 0;
size_t Fiber::external_bytes_reported = 0;
int Fiber::external_bytes_threshold_factor = 10;

bool did_init = false;

#if !NODE_VERSION_AT_LEAST(0,10,0)
Expand Down