unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#65700: time when gcs-done is updated needs to be clarified
@ 2023-09-02 13:00 Shynur Xie
  2023-09-07  9:14 ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Shynur Xie @ 2023-09-02 13:00 UTC (permalink / raw)
  To: 65700

I wrote a function which runs each time Emacs collects garbage, so I
added it to `post-gc-hook'.

That function cares about the value of `gcs-done' and the value of
`gc-elapsed'.
But it seems that `gcs-done' is updated after `post-gc-hook' is
executed?

Is this an intended behavior?  If so, I think the docstring of these
variables (such as `gcs-done', `gc-elapsed', ...) need to clarify when
the variable is updated.


v29.1




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-02 13:00 bug#65700: time when gcs-done is updated needs to be clarified Shynur Xie
@ 2023-09-07  9:14 ` Eli Zaretskii
  2023-09-07 13:38   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2023-09-07  9:14 UTC (permalink / raw)
  To: Shynur Xie, Stefan Monnier; +Cc: 65700

> From: Shynur Xie <one.last.kiss@outlook.com>
> Date: Sat, 2 Sep 2023 13:00:59 +0000
> msip_labels: 
> 
> I wrote a function which runs each time Emacs collects garbage, so I
> added it to `post-gc-hook'.
> 
> That function cares about the value of `gcs-done' and the value of
> `gc-elapsed'.
> But it seems that `gcs-done' is updated after `post-gc-hook' is
> executed?
> 
> Is this an intended behavior?  If so, I think the docstring of these
> variables (such as `gcs-done', `gc-elapsed', ...) need to clarify when
> the variable is updated.

Stefan, any comments?





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07  9:14 ` Eli Zaretskii
@ 2023-09-07 13:38   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-07 14:35     ` Shynur Xie
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-07 13:38 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Shynur Xie, 65700

>> I wrote a function which runs each time Emacs collects garbage, so I
>> added it to `post-gc-hook'.
>> 
>> That function cares about the value of `gcs-done' and the value of
>> `gc-elapsed'.  But it seems that `gcs-done' is updated after
>> `post-gc-hook' is executed?

Indeed, from what I see in the code, `gc-elapsed` and `gcs-done` are
updated *after* running `post-gc-hook`.
This means for example that the time to run `post-gc-hook` is included
in the `gc-elapsed` time.

>> Is this an intended behavior?

Good question.  AFAICT this was added by the following commit:

    commit 2c5bd60800ce4f2702f263eda2145be342208ffe
    Author: Dave Love <fx@gnu.org>
    Date:   Thu Jan 30 14:15:58 2003 +0000
    
        (Vgc_elapsed, gcs_done): New variables.
        (Fgarbage_collect): Use them.
        (init_alloc, syms_of_alloc): Set them up.
    
    diff --git a/src/alloc.c b/src/alloc.c
    --- a/src/alloc.c
    +++ b/src/alloc.c
    @@ -4367,7 +4373,15 @@
       if (!NILP (Vpost_gc_hook))
         {
           int count = inhibit_garbage_collection ();
           safe_run_hooks (Qpost_gc_hook);
           unbind_to (count, Qnil);
         }
    -  
    +
    +  /* Accumulate statistics.  */
    +  EMACS_GET_TIME (t2);
    +  EMACS_SUB_TIME (t3, t2, t1);
    +  if (FLOATP (Vgc_elapsed))
    +    XSETFLOAT (Vgc_elapsed, make_float (XFLOAT_DATA (Vgc_elapsed) +
    +					EMACS_SECS (t3) +
    +					EMACS_USECS (t3) * 1.0e-6));
    +  gcs_done++;

The only related commit I see around that time is the associated NEWS
message:

    ** New variables `gc-elapsed' and `gcs-done' provide extra information
    on garbage collection.

So I strongly suspect that it was largely accidental.  "Philosophically",
both choices make sense (either consider `post-gc-hook` as being part of
the GC or consider it as external to the GC).

I'd be curious to know how it affects your code.


        Stefan






^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 13:38   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-07 14:35     ` Shynur Xie
  2023-09-07 14:47       ` Eli Zaretskii
  2023-09-07 14:58       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 13+ messages in thread
From: Shynur Xie @ 2023-09-07 14:35 UTC (permalink / raw)
  To: Stefan Monnier, Eli Zaretskii; +Cc: 65700@debbugs.gnu.org

> Stefan:
> 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.

I believe I'm not the first person to do this; why others didn't
report the bug may be that they don't care whether the number of GCs
is exactly the same as they expect or one less, so they didn't really
check when `gcs-done' is updated.
IOW, if the update time of `gcs-done' is changed, I think most people
won't notice this change at all.

> Stefan:
> "Philosophically", both choices make sense (either consider
> `post-gc-hook` as being part of the GC or consider it as external to
> the GC).

I'm not a native English speaker.  Does the POST in `post-gc-hook'
mean the functions will run when GC is DONE?

> shynur:
> Is this an intended behavior?  If so, I think the docstring of these
> variables (such as `gcs-done', `gc-elapsed', ...) need to clarify
> when the variable is updated.

And `garbage-collection-messages'.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 14:35     ` Shynur Xie
@ 2023-09-07 14:47       ` Eli Zaretskii
  2023-09-07 15:23         ` Shynur Xie
  2023-09-12 10:14         ` Shynur Xie
  2023-09-07 14:58       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 2 replies; 13+ messages in thread
From: Eli Zaretskii @ 2023-09-07 14:47 UTC (permalink / raw)
  To: Shynur Xie; +Cc: monnier, 65700

> From: Shynur Xie <one.last.kiss@outlook.com>
> CC: "65700@debbugs.gnu.org" <65700@debbugs.gnu.org>
> Date: Thu, 7 Sep 2023 14:35:32 +0000
> msip_labels:
> 
> > Stefan:
> > 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.

That's just causes off-by-one count of GC cycles, no?  Easy enough to
fix.

> > Stefan:
> > "Philosophically", both choices make sense (either consider
> > `post-gc-hook` as being part of the GC or consider it as external to
> > the GC).
> 
> I'm not a native English speaker.  Does the POST in `post-gc-hook'
> mean the functions will run when GC is DONE?

Its being "done" doesn't necessarily have to include updating the
count of GCs.  The main job of GC is already done by the time the hook
is called.

This is what Stefan meant when he said that philosophically it is not
clear-cut whether updating the count should be done before or after
calling the hook.

> > shynur:
> > Is this an intended behavior?  If so, I think the docstring of these
> > variables (such as `gcs-done', `gc-elapsed', ...) need to clarify
> > when the variable is updated.
> 
> And `garbage-collection-messages'.

What about it?





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 14:35     ` Shynur Xie
  2023-09-07 14:47       ` Eli Zaretskii
@ 2023-09-07 14:58       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-07 15:08         ` Eli Zaretskii
  1 sibling, 1 reply; 13+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-07 14:58 UTC (permalink / raw)
  To: Shynur Xie; +Cc: Eli Zaretskii, 65700@debbugs.gnu.org

>> 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, "",






^ permalink raw reply related	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 14:58       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-07 15:08         ` Eli Zaretskii
  0 siblings, 0 replies; 13+ messages in thread
From: Eli Zaretskii @ 2023-09-07 15:08 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: one.last.kiss, 65700

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: Eli Zaretskii <eliz@gnu.org>,  "65700@debbugs.gnu.org"
>  <65700@debbugs.gnu.org>
> Date: Thu, 07 Sep 2023 10:58:46 -0400
> 
> 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?

None here, but please call out this incompatible change in NEWS.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 14:47       ` Eli Zaretskii
@ 2023-09-07 15:23         ` Shynur Xie
  2023-09-07 16:52           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-12 10:14         ` Shynur Xie
  1 sibling, 1 reply; 13+ messages in thread
From: Shynur Xie @ 2023-09-07 15:23 UTC (permalink / raw)
  To: Eli Zaretskii, Stefan Monnier; +Cc: 65700@debbugs.gnu.org

> Eli:
>
> > I'm not a native English speaker.  Does the POST in `post-gc-hook'
> > mean the functions will run when GC is DONE?
>
> Its being "done" doesn't necessarily have to include updating the
> count of GCs.  The main job of GC is already done by the time the
> hook is called.

Thanks for explaining.  Yes, "Its being 'done' doesn't necessarily
have to include updating the count of GCs."

My point is, IF "the functions will run when GC is DONE" THEN when
these functions are being executed, the `gcs-DONE' should have been
updated.

> Eli:
>
> > > Is this an intended behavior?  If so, I think the docstring of
> > > these variables (such as `gcs-done', `gc-elapsed', ...) need to
> > > clarify when the variable is updated.
> 
> > And `garbage-collection-messages'.
>
> What about it?

Mentioning it here is just for reference.  Maybe you want clarify when
the variable/message is updated/generated in the docstring.


> Stefan:
> Hmm... that doesn't really explain in which way this affects your
> code.

It does.

> `gcs-done` will be always smaller by 1

That's it.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 15:23         ` Shynur Xie
@ 2023-09-07 16:52           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-07 17:19             ` Shynur Xie
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-07 16:52 UTC (permalink / raw)
  To: Shynur Xie; +Cc: Eli Zaretskii, 65700@debbugs.gnu.org

>> Hmm... that doesn't really explain in which way this affects your
>> code.
> It does.

At least not in a way that I understand, then :-(

>> `gcs-done` will be always smaller by 1
> That's it.

Why do you care if it starts at 4 rather than at 5?
Maybe we should make it start at some random number instead, to make it
clear that 0 is meaningless :-)


        Stefan






^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 16:52           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-07 17:19             ` Shynur Xie
  0 siblings, 0 replies; 13+ messages in thread
From: Shynur Xie @ 2023-09-07 17:19 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 65700@debbugs.gnu.org

> Stefan:
> Why do you care if it starts at 4 rather than at 5?

To be honest, I don't really care; it's quite easy for me to fix this
in my function (although I'm a little freaked out that the behavior
doesn't match the docstring: "accumulated number of garbage
collections done.").  Even if I just leave it there, it's only one
less than it actually is.

But at least I should let Emacs developers know this.  If this is not
an intended behavior, they can fix it; if it is, they may make it
clear in docstring.

So the question is:  do you care?  or, do you think users care?

If the answer is no, then there's no need to change it, just clarify
this behavior in the docstring of `post-gc-hook'.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-07 14:47       ` Eli Zaretskii
  2023-09-07 15:23         ` Shynur Xie
@ 2023-09-12 10:14         ` Shynur Xie
  2023-09-12 12:53           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 1 reply; 13+ messages in thread
From: Shynur Xie @ 2023-09-12 10:14 UTC (permalink / raw)
  To: Eli Zaretskii, Stefan Monnier; +Cc: 65700@debbugs.gnu.org

> shynur:
> 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.

> Eli:
> That's just causes off-by-one count of GC cycles, no?  Easy enough
> to fix.

> Stefan:
> the only difference is that `gcs-done` will be always smaller by 1

We forgot the `gc-elapsed'.  It's a little hard to fix (dirty work is
needed).




^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-12 10:14         ` Shynur Xie
@ 2023-09-12 12:53           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-09-12 18:07             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-12 12:53 UTC (permalink / raw)
  To: Shynur Xie; +Cc: Eli Zaretskii, 65700@debbugs.gnu.org

>> 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.
>
>> Eli:
>> That's just causes off-by-one count of GC cycles, no?  Easy enough
>> to fix.
>
>> Stefan:
>> the only difference is that `gcs-done` will be always smaller by 1
>
> We forgot the `gc-elapsed'.  It's a little hard to fix (dirty work is
> needed).

Yup, for `gcs-done`, it doesn't matter very much, but with the current
code `post-gc-hook` simply can't know how much time the last GC took,
which can be a useful information there.
I'll push my patch to `master` soon, I just need to clean it up first.


        Stefan






^ permalink raw reply	[flat|nested] 13+ messages in thread

* bug#65700: time when gcs-done is updated needs to be clarified
  2023-09-12 12:53           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-09-12 18:07             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-12 18:07 UTC (permalink / raw)
  To: Shynur Xie; +Cc: Eli Zaretskii, 65700@debbugs.gnu.org

> I'll push my patch to `master` soon, I just need to clean it up first.

Done, closing,


        Stefan






^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-09-12 18:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-02 13:00 bug#65700: time when gcs-done is updated needs to be clarified Shynur Xie
2023-09-07  9:14 ` Eli Zaretskii
2023-09-07 13:38   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-07 14:35     ` Shynur Xie
2023-09-07 14:47       ` Eli Zaretskii
2023-09-07 15:23         ` Shynur Xie
2023-09-07 16:52           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-07 17:19             ` Shynur Xie
2023-09-12 10:14         ` Shynur Xie
2023-09-12 12:53           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12 18:07             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-07 14:58       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-07 15:08         ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).