Skip to content

Commit

Permalink
[gc_heap] examples/cartesian benchmark works with small heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Dec 13, 2020
1 parent c592cf3 commit 1366cdb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
6 changes: 3 additions & 3 deletions mycpp/gc_heap.cc
Expand Up @@ -111,7 +111,7 @@ void Heap::Collect() {
// i.e. for other objects that are pointing to it
Obj* new_location = Relocate(root);

log(" new location %p", new_location);
// log(" new location %p", new_location);

// This update is for the "double indirection", so future accesses to a
// local variable use the new location
Expand All @@ -130,9 +130,9 @@ void Heap::Collect() {
Obj* child = fixed->children_[i];
// log("i = %d, p = %p, heap_tag = %d", i, child, child->heap_tag_);
if (child) {
log(" fixed: child %d from %p", i, child);
// log(" fixed: child %d from %p", i, child);
fixed->children_[i] = Relocate(child);
log(" to %p", fixed->children_[i]);
// log(" to %p", fixed->children_[i]);
}
}
}
Expand Down
46 changes: 16 additions & 30 deletions mycpp/gc_heap.h
Expand Up @@ -174,48 +174,39 @@ class Heap {
#endif
}

void* Bump(int n) {
char* p = free_;
free_ += n;
#if GC_DEBUG
num_live_objs_++;
#endif
return p;
}

void* Allocate(int num_bytes) {
int n = aligned(num_bytes);
// log("n = %d, p = %p", n, p);

if (free_ + n <= limit_) { // Common case: we have space for it.
char* p = free_;
free_ += n;
#if GC_DEBUG
num_live_objs_++;
#endif
return p;
return Bump(n);
}

#if GC_DEBUG
// log("GC free_ %p, from_space_ %p, space_size_ %d", free_, from_space_,
// space_size_);
#endif

Collect();
// Three cases at the end of Collect:
// - We have more than 20% free space, and we didn't grow.
// - We have less than 20% free space, but more than zero, and we grew.
// A future collection will grow (or it may never happen).
// - NOTHING was collected. We have zero space, and we need to collect
// NOW with must_grow=true. This is unlikely.
Collect(); // Try to free some space.

// log("after GC: from begin %p, free_ %p, n %d, limit_ %p",
// from_space_.begin_, free_, n, limit_);

if (free_ + n <= limit_) { // After collection, we have space for it.
char* p = free_;
free_ += n;
#if GC_DEBUG
num_live_objs_++;
#endif
return p;
if (free_ + n <= limit_) { // Now we have space for it.
return Bump(n);
}

// After collection, it's STILL too small. So resize to_space_ to ENSURE
// that allocation will succeed, copy the heap to it, then allocate the
// object.
// Ensure there will be enough space.
// It's STILL too small. Resize to_space_ to ENSURE that allocation will
// succeed, copy the heap to it, then allocate the object.
int multiple = 2;
while (from_space_.size_ + n > to_space_.size_ * multiple) {
multiple *= 2;
Expand All @@ -230,12 +221,7 @@ class Heap {
num_forced_growths_++;
#endif

char* p = free_;
free_ += n;
#if GC_DEBUG
num_live_objs_++;
#endif
return p;
return Bump(n);
}

void Swap() {
Expand Down
4 changes: 2 additions & 2 deletions mycpp/harness.sh
Expand Up @@ -8,8 +8,8 @@ gen-main() {
cat <<EOF
int main(int argc, char **argv) {
// gc_heap::gHeap.Init(512 << 10); // 512 KiB; doubling in size
gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection
gc_heap::gHeap.Init(128 << 10); // 128 KiB; doubling in size
// gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection
if (getenv("BENCHMARK")) {
fprintf(stderr, "Benchmarking...\n");
Expand Down
8 changes: 4 additions & 4 deletions mycpp/run.sh
Expand Up @@ -259,6 +259,10 @@ gen-ctags() {
}

gc-examples() {
BENCHMARK=1 GC=1 example-both cartesian
#GC=1 example-both cartesian
return

# ASAN failure on Heap::Collect()
GC=1 example-both loops
return
Expand Down Expand Up @@ -286,10 +290,6 @@ gc-examples() {
fi
return

# FAILS with a small heap. Some List invaraitsn?
BENCHMARK=1 GC=1 example-both cartesian
return

# %5d doesn't work in either mylib or my_runtime
GC=1 example-both strings

Expand Down

0 comments on commit 1366cdb

Please sign in to comment.