Skip to content

Fix race between GC and case object construction#211

Merged
jrincayc merged 3 commits intojrincayc:masterfrom
n1ywb-1:fixcaserace
Mar 31, 2025
Merged

Fix race between GC and case object construction#211
jrincayc merged 3 commits intojrincayc:masterfrom
n1ywb-1:fixcaserace

Conversation

@n1ywb-1
Copy link
Contributor

@n1ywb-1 n1ywb-1 commented Mar 18, 2025

Does a partial GC before constructing new case objects to ensure there are enough free nodes to finish construction and add it to a root node before the GC runs again.

Case objects must be marked twice so if the GC runs after a new case is allocated but before it is added to the hash table AND a root AND the pointer isn't found on the stack, eg because it was optimized out, then the GC may free the cases.

I was concerned this would have a negative performance impact...

It actually improved unit test performance by 10-20%.

And it does. It's way slow. I'm not sure how I produced the "improvement".

So we probably want to find a more performant fix.

Does a partial GC before constructing new case objects to ensure there are
enough free nodes to finish construction and add it to a root node
before the GC runs again.

Case objects must be marked twice so if the GC runs after a new case is
allocated but before it is added to the hash table AND a root AND the
pointer isn't found on the stack, eg because it was optimized out, then
the GC may free the cases.

I was concerned this would have a negative performance impact...

It actually improved unit test performance by 10-20%.

¯\_(ツ)_/¯
@n1ywb-1
Copy link
Contributor Author

n1ywb-1 commented Mar 18, 2025

Adds too much performance overhead. Need more performant fix.

@n1ywb-1 n1ywb-1 closed this Mar 18, 2025
@n1ywb-1 n1ywb-1 reopened this Mar 18, 2025
@n1ywb-1
Copy link
Contributor Author

n1ywb-1 commented Mar 18, 2025

Ok I fixed the performance regression.

There's really only one place this hazard occurs, in one call to cons(). So we just pre-allocate that cons, and then mutate it, avoiding calls to newnode() until the case obj is linked back to it's containing object.

globals.h Outdated

#endif

void do_gc(BOOLEAN full);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@jrincayc jrincayc merged commit 6382f46 into jrincayc:master Mar 31, 2025
1 check passed
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.

2 participants