Subject: bug#65700: time when gcs-done is updated needs to be clarified

From: Stefan Monnier <[email-masked]>
Date: Thu, 07 Sep 2023 10:58:46 -0400

>> I'd be curious to know how it affects your code.
> My Emacs displays its runtime information like this:
>   <github.com/shynur/.emacs.d/issues/1#issuecomment-1671815270>
> Since it's mainly related to GC, I have it updated after each GC.

Hmm... that doesn't really explain in which way this affects your code.
If you only ever run that from `post-gc-hook` the only difference is
that `gcs-done` will be always smaller by 1 and since it doesn't start
at 0 anyway it's not really relevant: it will still increase by 1 every
time `post-gc-hook` is run.

FWIW, I tend to agree that it would be more useful to run `post-gc-hook`
after those values have been updated.  So that `post-gc-hook` can know
the exact duration of the GC that we just finished, rather than the one
before that.

Any objection to the patch below?

        Stefan

diff --git a/src/alloc.c b/src/alloc.c
index c77bdc6372d..cee43783387 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -6595,13 +6595,6 @@ garbage_collect (void)
   image_prune_animation_caches (false);
 #endif
 
-  if (!NILP (Vpost_gc_hook))
-    {
-      specpdl_ref gc_count = inhibit_garbage_collection ();
-      safe_run_hooks (Qpost_gc_hook);
-      unbind_to (gc_count, Qnil);
-    }
-
   /* Accumulate statistics.  */
   if (FLOATP (Vgc_elapsed))
     {
@@ -6620,6 +6613,13 @@ garbage_collect (void)
       if (tot_after < tot_before)
 	malloc_probe (min (tot_before - tot_after, SIZE_MAX));
     }
+
+  if (!NILP (Vpost_gc_hook))
+    {
+      specpdl_ref gc_count = inhibit_garbage_collection ();
+      safe_run_hooks (Qpost_gc_hook);
+      unbind_to (gc_count, Qnil);
+    }
 }
 
 DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",