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

Start of the GC implementation. #80

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

Maaarcocr
Copy link
Member

@Maaarcocr Maaarcocr commented Aug 13, 2017

Implemented a function to get the values from the stack.
Implemented allocated, a value in the vm object that keeps track of the memory used.
Implemented a linked list of all the Values which are pointers.

@maximecb
Copy link
Member

maximecb commented Aug 13, 2017

The biggest problem I see is that creating an std::vector<Value> is inefficient for a number of reasons. Many new objects being creared, but also the Value objects add and remove themselves from the linked list every time.

What you probably want is to write a visit() function which takes a refptr argument and a Tag as input. While the GC is doing its thing, we don't need to actively track Value objects, because the program isn't running.

Ideally, we would also use tag bits in the object headers to store which objects were visited (mark bits), but you can use an unordered_set<refptr> for a first version if you want.

Some other remarks:

  • what is the tags file?

  • tests should probably all go under tests/gc

  • vm.setHead(this); => make head a static member of the Value class, and make the VM class a friend of the Value class.

  • We should rename the VM class to GC.

@Maaarcocr
Copy link
Member Author

I was going to implement the visitor soon. The array was just an easy way in order to first understand what to do.

I'm not sure what the tags file is, I'll get rid of it in the next commit.

Oh yes, the static method is very good suggestion! Thanks :)

@maximecb
Copy link
Member

maximecb commented Oct 1, 2017

Comments:

void markValues(Value* root) => you should probably use a Value&.

void mark() { => the humanity! semicolons on their own line.

void Value::unsetMark() => I would rename this to clearMark. Totally a nitpick.

You're missing something here:
https://github.com/zetavm/zetavm/pull/80/files#diff-14e33e1ac98adcf7ec27bee50b2b5dbbR101

Even if this->prev is NULL, you still need to set this->next->prev = NULL.

Also, I would rename the VM class to GC, since that is memory allocation is really all it is handling.

@maximecb
Copy link
Member

maximecb commented Oct 6, 2017

The most obvious flaw I see is that the Value class is missing a copy constructor and assignment operator:
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

Linking you again to the implementation I made for Higgs, which implements all the list management in the assignment operator:
https://github.com/higgsjs/Higgs/blob/master/source/runtime/gc.d#L60

The assignment operator and copy constructor are needed, because otherwise the prev and next pointers will be copied when an assignment happens, which is not what we want.

@maximecb
Copy link
Member

Progressing very well :)

@maximecb
Copy link
Member

@Maaarcocr @krypt-n Made this fix following our discussion. This might fix the bug you were running into (hopefully): a7e9430

@krypt-n
Copy link
Contributor

krypt-n commented Oct 21, 2017

That should fix the first of the issues that valgrind showed, pushVal still uses operator= on potentially uninitialized memory though.

@maximecb
Copy link
Member

@krypt-n Ok noted, will take a look at that too 👍

Marco Rudilosso and others added 7 commits October 21, 2017 19:52
Implemented a function to mark the values on the stack.
Implemented allocated, a value in the vm object that keeps track of the memory used.
Implemented a linked list of all the Values which are pointers.
@Maaarcocr
Copy link
Member Author

Rebasing my latest changes onto master make all the tests pass! 😄 Thanks @krypt-n and @maximecb for the help. I still need to do some more work to better tests that everything is actually fine and also I still need to make the GC understand that when it sweeps something it has to decrease allocated variable.

@maximecb
Copy link
Member

@Maaarcocr it passes? Awesome.

Regarding testing, I put a bunch of GC torture tests I had written for Higgs in:
https://github.com/zetavm/zetavm/tree/master/tests/gc

They're written in JavaScript, so we would need to change some things. They also rely on some higgs-specific primitives to force garbage collections to happen more often. We could add similar things to Zeta, or add command-line arguments to make the heap size artificially small. We need to do a really thorough job with testing, because GC bugs can easily remain hidden for a long time.

@maximecb
Copy link
Member

Oh, one thing we might want to do is, when compiled with assertions, write non-zero bytes over memory before calling free. That will basically force things to crash/fail if an object that has been freed is still in use.

@maximecb
Copy link
Member

@Maaarcocr do you need help porting the GC tests to Plush? If you want, I can start working on that.

@Maaarcocr
Copy link
Member Author

@maximecb that would be lovely 😄 in the meanwhile I'll make sure that the variable holding the amount of memory allocated get reduced on sweep.

@maximecb
Copy link
Member

@Maaarcocr I added a vm.get_gc_count() function for you to implement. This is useful for GC tests: 7dac602

@maximecb
Copy link
Member

Ok. Ported a second test, and added another function: vm.gc_collect(), this will serve to manually trigger a collection for testing.

@Maaarcocr
Copy link
Member Author

@maximecb we are not actually passing the tests. I did run the tests with free commented out. I'm so so sorry for this.

I will implement the functions that you are adding 👍

@maximecb
Copy link
Member

@Maaarcocr don't panic. I believe in you :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants