~ chicken-core (chicken-5) cfd75ecd9a59a00f372a729f06b8007893745b37


commit cfd75ecd9a59a00f372a729f06b8007893745b37
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Wed May 22 13:53:28 2024 +0200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Wed May 22 15:40:03 2024 +0200

    Don't free the memory for the scratchspace on minor GC
    
    This is too costly if we're in a loop that's operating on the
    scratchspace, because it will just reallocate the scratchspace, and
    use a small scratchspace which will cause excessive reallocation.
    
    Signed-off-by: felix <felix@call-with-current-continuation.org>

diff --git a/runtime.c b/runtime.c
index 9becd2aa..5116ed37 100644
--- a/runtime.c
+++ b/runtime.c
@@ -3675,13 +3675,16 @@ C_regparm void C_fcall C_reclaim(void *trampoline, C_word c)
   }
 
   /* GC will have copied any live objects out of scratch space: clear it */
-  if (C_scratchspace_start != NULL) {
-    C_free(C_scratchspace_start);
-    C_scratchspace_start = NULL;
-    C_scratchspace_top = NULL;
-    C_scratchspace_limit = NULL;
+  if (C_scratchspace_start != C_scratchspace_top) {
+    /* And drop the scratchspace in case of a major or reallocating collection */
+    if (gc_mode != GC_MINOR) {
+      C_free(C_scratchspace_start);
+      C_scratchspace_start = NULL;
+      C_scratchspace_limit = NULL;
+      scratchspace_size = 0;
+    }
+    C_scratchspace_top = C_scratchspace_start;
     C_scratch_usage = 0;
-    scratchspace_size = 0;
   }
 
   if(gc_mode == GC_MAJOR) {
Trap