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

optimize how we check whether gc should happen #1146

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmake/RuntimeConfig.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(GC_THRESHOLD 2097152
set(GC_THRESHOLD 3
CACHE STRING "Initial Young Generation Size")

set(NOT_YOUNG_OBJECT_BIT 0x10000000000000)
Expand Down
5 changes: 0 additions & 5 deletions config/llvm_header.inc
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,6 @@ declare void @write_configuration_to_proof_trace(ptr, ptr)

@gc_roots = global [256 x ptr] zeroinitializer

define void @set_gc_threshold(i64 %threshold) {
store i64 %threshold, ptr @GC_THRESHOLD
ret void
}

define i64 @get_gc_threshold() {
%threshold = load i64, ptr @GC_THRESHOLD
ret i64 %threshold
Expand Down
4 changes: 4 additions & 0 deletions include/runtime/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ using memory_block_header = struct {
#define MEM_BLOCK_START(ptr) \
((char *)(((uintptr_t)(ptr)-1) & ~(BLOCK_SIZE - 1)))

extern bool time_for_collection;

size_t get_gc_threshold();

// Resets the given arena.
void arena_reset(struct arena *);

Expand Down
11 changes: 5 additions & 6 deletions lib/codegen/Decision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,11 @@ std::pair<std::vector<llvm::Value *>, llvm::BasicBlock *> step_function_header(
module->getContext(), "checkCollect", block->getParent());
llvm::BranchInst::Create(stuck, check_collect, is_finished, block);

auto *collection = get_or_insert_function(
module, "is_collection",
llvm::FunctionType::get(
llvm::Type::getInt1Ty(module->getContext()), {}, false));
auto *is_collection
= llvm::CallInst::Create(collection, {}, "", check_collect);
auto *collection = module->getOrInsertGlobal(
"time_for_collection", llvm::Type::getInt1Ty(module->getContext()));
auto *is_collection = new llvm::LoadInst(
llvm::Type::getInt1Ty(module->getContext()), collection, "is_collection",
check_collect);
set_debug_loc(is_collection);
auto *collect = llvm::BasicBlock::Create(
module->getContext(), "isCollect", block->getParent());
Expand Down
6 changes: 6 additions & 0 deletions runtime/alloc/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ static void *megabyte_malloc() {
return result;
}

bool time_for_collection;

static void fresh_block(struct arena *arena) {
char *next_block = nullptr;
if (arena->block_start == nullptr) {
Expand Down Expand Up @@ -106,8 +108,12 @@ static void fresh_block(struct arena *arena) {
next_header->next_block = nullptr;
next_header->semispace = arena->allocation_semispace_id;
arena->num_blocks++;
time_for_collection = true;
}
}
if (!*(char **)next_block && arena->num_blocks >= get_gc_threshold()) {
time_for_collection = true;
}
arena->block = next_block + sizeof(memory_block_header);
arena->block_start = next_block;
arena->block_end = next_block + BLOCK_SIZE;
Expand Down
10 changes: 1 addition & 9 deletions runtime/collect/collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ static char *last_alloc_ptr;
#endif

size_t numBytesLiveAtCollection[1 << AGE_WIDTH];
void set_gc_threshold(size_t);
size_t get_gc_threshold(void);
bool youngspace_almost_full(size_t);

bool during_gc() {
return is_gc;
Expand Down Expand Up @@ -285,6 +282,7 @@ void init_static_objects(void) {

void kore_collect(void **roots, uint8_t nroots, layoutitem *type_info) {
is_gc = true;
time_for_collection = false;
collect_old = should_collect_old_gen();
MEM_LOG("Starting garbage collection\n");
#ifdef GC_DBG
Expand Down Expand Up @@ -349,16 +347,10 @@ void kore_collect(void **roots, uint8_t nroots, layoutitem *type_info) {
#endif
MEM_LOG("Finishing garbage collection\n");
is_gc = false;
set_gc_threshold(youngspace_size());
}

void free_all_kore_mem() {
kore_collect(nullptr, 0, nullptr);
kore_clear();
}

bool is_collection() {
size_t threshold = get_gc_threshold();
return youngspace_almost_full(threshold);
}
}
10 changes: 1 addition & 9 deletions runtime/lto/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ size_t youngspace_size(void) {

bool youngspace_almost_full(size_t threshold) {
char *next_block = *(char **)youngspace.block_start;
if (next_block) {
// not on the last block, so short circuit and assume that we can keep
// allocating for now.
return false;
}
ptrdiff_t free_bytes = youngspace.block_end - youngspace.block;
size_t total_bytes
= youngspace.num_blocks * (BLOCK_SIZE - sizeof(memory_block_header));
return (total_bytes - free_bytes) * 100 > threshold * 95;
return !next_block;
}

void kore_alloc_swap(bool swap_old) {
Expand Down
3 changes: 3 additions & 0 deletions unittests/runtime-collections/lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ block *DUMMY1 = &D1;
}

bool gc_enabled;
size_t get_gc_threshold() {
return SIZE_MAX;
}

BOOST_AUTO_TEST_SUITE(ListTest)

Expand Down
4 changes: 4 additions & 0 deletions unittests/runtime-ffi/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ bool during_gc() {
return false;
}

size_t get_gc_threshold() {
return SIZE_MAX;
}

void print_configuration_internal(
writer *file, block *subject, char const *sort, bool, void *) { }
void sfprintf(writer *, char const *, ...) { }
Expand Down
4 changes: 4 additions & 0 deletions unittests/runtime-io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ bool during_gc() {

void add_hash64(void *, uint64_t) { }

size_t get_gc_threshold() {
return SIZE_MAX;
}

void flush_io_logs();
string *make_string(const KCHAR *, int64_t len = -1);
blockheader header_err();
Expand Down
4 changes: 4 additions & 0 deletions unittests/runtime-strings/stringtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ floating *move_float(floating *i) {

void add_hash64(void *, uint64_t) { }

size_t get_gc_threshold() {
return SIZE_MAX;
}

struct blockheader get_block_header_for_symbol(uint32_t tag) {
return blockheader{tag};
}
Expand Down
Loading