]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
Improve GC estimation for userdata with attached managed memory.
authorMike Pall <mike>
Thu, 28 Jul 2022 13:40:28 +0000 (15:40 +0200)
committerMike Pall <mike>
Thu, 28 Jul 2022 13:40:28 +0000 (15:40 +0200)
This works well for string.buffers, but not for userdata or
cdata with attached unmanaged memory (malloc/free, mmap/munmap).

src/lj_gc.c

index b35a0d44a9fbddad385556dfe8fa4a12f2bbbe47..2fc52ec141096208952baaf082c51d33486f11e7 100644 (file)
@@ -700,9 +700,12 @@ static size_t gc_onestep(lua_State *L)
     }
   case GCSfinalize:
     if (gcref(g->gc.mmudata) != NULL) {
+      GCSize old = g->gc.total;
       if (tvref(g->jit_base))  /* Don't call finalizers on trace. */
        return LJ_MAX_MEM;
       gc_finalize(L);  /* Finalize one userdata object. */
+      if (old >= g->gc.total && g->gc.estimate > old - g->gc.total)
+       g->gc.estimate -= old - g->gc.total;
       if (g->gc.estimate > GCFINALIZECOST)
        g->gc.estimate -= GCFINALIZECOST;
       return GCFINALIZECOST;