Make implementation match intention in comment.

We were doing forward instead of a backward search. This is slower, and
for the unrootall case was also incorrect. unrootall was not commonly
used but should still work as expected.

Bug found by @pepe with help from Claude.
This commit is contained in:
Calvin Rose
2026-09-28 17:15:26 -05:00
parent b437ae3feb
commit 38ef5b53ba
+4 -5
View File
@@ -645,9 +645,9 @@ static int janet_gc_idequals(Janet lhs, Janet rhs) {
/* Remove a root value from the GC. This allows the gc to potentially reclaim
* a value and all its children. */
int janet_gcunroot(Janet root) {
Janet *vtop = janet_vm.roots + janet_vm.root_count;
/* Search from top to bottom as access is most likely LIFO */
for (Janet *v = janet_vm.roots; v < vtop; v++) {
for (Janet *v = janet_vm.roots + janet_vm.root_count; v > janet_vm.roots;) {
v--;
if (janet_gc_idequals(root, *v)) {
*v = janet_vm.roots[--janet_vm.root_count];
return 1;
@@ -658,13 +658,12 @@ int janet_gcunroot(Janet root) {
/* Remove a root value from the GC. This sets the effective reference count to 0. */
int janet_gcunrootall(Janet root) {
Janet *vtop = janet_vm.roots + janet_vm.root_count;
int ret = 0;
/* Search from top to bottom as access is most likely LIFO */
for (Janet *v = janet_vm.roots; v < vtop; v++) {
for (Janet *v = janet_vm.roots + janet_vm.root_count; v > janet_vm.roots;) {
v--;
if (janet_gc_idequals(root, *v)) {
*v = janet_vm.roots[--janet_vm.root_count];
vtop--;
ret = 1;
}
}