Skip to content

Commit 7c741d5

Browse files
hs0225daeyeon
authored andcommitted
feat: add gc_free_space_divisor config
Signed-off-by: Hosung Kim [email protected]
1 parent 6646919 commit 7c741d5

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

deps/node/src/lwnode/lwnode-public.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ bool Runtime::Configuration::Set(const std::string& key, int value) {
239239
value);
240240
LWNode::GlobalConfiguration::GetInstance().set_gc_interval(value);
241241
return true;
242+
} else if (key == "gc_free_space_divisor") {
243+
LWNODE_DEV_LOGF(
244+
"[Runtime::Configuration::Set] GC free space divisor set to %d", value);
245+
LWNode::GlobalConfiguration::GetInstance().set_gc_free_space_divisor(value);
246+
return true;
242247
}
243248
return false;
244249
}

src/api/engine.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "api/global.h"
2323
#include "handle.h"
24+
#include "lwnode/global-configuration.h"
2425
#include "lwnode/lwnode-loader.h"
2526
#include "utils/misc.h"
2627
#include "utils/string-util.h"
@@ -351,7 +352,7 @@ bool Engine::Dispose() {
351352
return true;
352353
}
353354

354-
#define GC_FREE_SPACE_DIVISOR 4
355+
#define DEFAULT_GC_FREE_SPACE_DIVISOR 4
355356

356357
void Engine::initialize() {
357358
#ifndef NDEBUG
@@ -367,7 +368,13 @@ void Engine::initialize() {
367368
#endif
368369

369370
Globals::initialize(Platform::GetInstance());
370-
Memory::setGCFrequency(GC_FREE_SPACE_DIVISOR);
371+
int gcFreeSpaceDivisor =
372+
LWNode::GlobalConfiguration::GetInstance().gc_free_space_divisor();
373+
if (gcFreeSpaceDivisor < 0) {
374+
gcFreeSpaceDivisor = DEFAULT_GC_FREE_SPACE_DIVISOR;
375+
}
376+
377+
Memory::setGCFrequency(gcFreeSpaceDivisor);
371378
gcHeap_.reset(GCHeap::create());
372379

373380
if (Global::flags()->isOn(Flag::Type::TraceGC)) {

src/lwnode/global-configuration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ class GlobalConfiguration {
3131
void set_gc_interval(int interval) { gc_interval_ = interval; }
3232
int gc_interval() { return gc_interval_; }
3333

34+
void set_gc_free_space_divisor(int divisor) {
35+
gc_free_space_divisor_ = divisor;
36+
}
37+
int gc_free_space_divisor() { return gc_free_space_divisor_; }
38+
3439
private:
3540
GlobalConfiguration() = default;
3641
~GlobalConfiguration() = default;
3742

3843
int gc_interval_ = -1; // TODO: change optional type
44+
int gc_free_space_divisor_ = -1;
3945
};
4046

4147
} // namespace LWNode

test/embedding/example.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ int main(int argc, char* argv[]) {
1616
if (!configuration.Set("gc_interval", 10000)) {
1717
std::cerr << "Failed to set gc_interval" << std::endl;
1818
}
19+
if (!configuration.Set("gc_free_space_divisor", 10)) {
20+
std::cerr << "Failed to set gc_free_space_divisor" << std::endl;
21+
}
22+
1923
auto runtime = std::make_shared<lwnode::Runtime>(std::move(configuration));
2024

2125
std::promise<void> promise;

0 commit comments

Comments
 (0)