Skip to content

Use references instead of clones - #8

Merged
8dcc merged 16 commits into
mainfrom
copy-references
Feb 16, 2025
Merged

Use references instead of clones#8
8dcc merged 16 commits into
mainfrom
copy-references

Conversation

@8dcc

@8dcc 8dcc commented Feb 16, 2025

Copy link
Copy Markdown
Owner

Work with references, instead of always returning copies. This change was possible thanks to #5 (which needed #4), #6 and #7. Some changes were merged into this copy-references branch in #7.

Major changes:

  • Remove most calls to expr_clone, for working with references instead of copies.
  • Return or store references in car, cdr, cons, nth, quote and backquote primitives.
  • Add proper environment closures, setting the parent environments whenever a lambda is created, not when it's called. See Environment closures #7.
  • Change how GC handles lambdas, add is_used member to Env. Mostly included in Environment closures #7, but not completely (see 5758c96).
  • Don't mark globals (g_nil, g_tru and g_debug_trace_list) for garbage collection, since their references are being stored in the environment.

Minor changes:

  • Add gc_mark_env and gc_mark_env_and_parents static functions to garbage_collector.c.
  • Add an Env* parameter to lambdactx_init, remove it from lambdactx_eval_body.
  • Don't make the Expr* parameter of env_bind and env_bind_global constant.
  • Add closure test to test/lambdas.lisp. See Environment closures #7.

8dcc and others added 16 commits January 10, 2025 18:04
These are considered mostly safe when proper cons pairs are added, although they
don't work right now. More unsafe expressions were noted in commit 9ddd84d.

* Change parameter types of 'env_bind' and 'env_bind_global', remove 'const'
  qualifier from 'val' parameter.
* Store references instead of clones in 'env_bind' and 'env_bind_global'.
* Return reference instead of clone in 'env_get'.
* Don't clone self-evaluating expressions in 'eval'.
* Don't clone returned expression in 'prim_print_str'.
* Don't clone expressions when cloning an environment.
* Don't clone expressions of the body when cloning a lambda.
* Don't mark 'g_nil', 'g_tru' and 'g_debug_trace_list' for garbage collection.
* Don't clone expressions in 'cons', 'car', 'cdr' and 'nth' primitives.
* Don't clone expressions when evaluating quoted (or backquoted) expressions.
The code of this commit is a bit messy, as explained in the comments.

* Add 'is_used' member to 'Env', for storing whether or not it's being used
  somewhere else.
* Unmark environments in 'gc_unmark_all'.
* Mark environments in 'gc_mark_expr'.
* Don't collect expressions if their associated environment is still in use.
This commit doesn't currently work, because we still work with copies.
Environment closures.

Changes:

* Add 'is_used' member to 'Env', for storing whether or not it's being used
  somewhere else.
* Unmark environments in 'gc_unmark_all' (by setting 'is_used').
* Mark environments in 'gc_mark_expr' as used.
* Don't collect expressions if their associated environment is marked as in use.
* Set the parent environment on lambda creation, instead of on call.
* Mark each expression of a lambda's environment whenever the lambda is marked
  for garbage collection.
* Add 'pool_item_is_gcmarked', move 'pool_item_is_free' to 'expr_pool.h', make
  arguments of 'pool_item_flags' constant.
Before this commit, we just marked the lambda's environment and its
contents. Now we also mark all parent environments, and their contents.

After this commit, the Y combinator can be evaluated.
@8dcc 8dcc mentioned this pull request Feb 16, 2025
@8dcc
8dcc merged commit 8527988 into main Feb 16, 2025
@8dcc
8dcc deleted the copy-references branch February 16, 2025 12:44
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.

1 participant