Giter Club home page Giter Club logo

Comments (3)

zxul767 avatar zxul767 commented on June 17, 2024

after thinking about this more, i think it's a bad idea to keep using this pattern. it is prone to errors and results in tedious and verbose code.

an alternative is what i've called "the nursery pattern", which is the idea of having some means to keep track of newly allocated objects for a specific duration. for example, instead of:

push_value(OBJECT_VAL(string__copy(name, (int)strlen(name), vm)), vm);
push_value(OBJECT_VAL(native_function__new(function, vm)), vm);
table__set(&vm->global_vars, AS_STRING(vm->value_stack[0]), vm->value_stack[1]);
pop_value(vm);
pop_value(vm);

we might do:

memory__open_object_nursery(vm);
// e.g., `vm->nursery_start = vm->objects`

ObjectString* function_name = string__copy(name, (int)strlen(name), vm));
Value function = OBJECT_VAL(native_function__new(function, vm)));
// during a GC run (which might happen in `table__set`), we expose objects 
// between `vm->nursery_start` and `vm->objects` as roots so they are not 
// incorrectly disposed
table__set(&vm->global_vars, function_name, function);

memory__close_object_nursery(vm);
// e.g., `vm->nursery_start = NULL`

which is both more performant (no more pushing and popping, nor unnecessary value creation), and less error prone.

from lox.

zxul767 avatar zxul767 commented on June 17, 2024

marked as p0 due to the potential, subtle bugs that not implementing this and continuing to rely on the push/pop pattern can bring

from lox.

zxul767 avatar zxul767 commented on June 17, 2024

#34 will fix this...

from lox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.