unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#45178: buffer-hash docstring should explain difference to secure-hash
@ 2020-12-11 14:41 Stefan Kangas
  2020-12-11 16:00 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Kangas @ 2020-12-11 14:41 UTC (permalink / raw)
  To: 45178; +Cc: larsi

Severity: minor

It is not clear to me when I would want to use:

    (buffer-hash (current-buffer))

instead of

    (secure-hash 'sha1 (current-buffer))

One obvious difference is that secure-hash respects narrowing, while
buffer-hash doesn't.  But it's not clear to me why I can't just use
save-restrictions+widen instead.  The Elisp manual says: "It should be
somewhat more efficient on larger buffers than ‘secure-hash’ is, and
should not allocate more memory."

First, I think we should add something about this to the docstring of
`buffer-hash'.  Perhaps we should also mention it in the `secure-hash'
docstring.

Second, is the difference all that important?  If so, perhaps we should
look over our uses of `md5', `sha1' and `secure-hash' in our code-base
to ensure we use the more efficient version where applicable.  (For
example in savehist.el and desktop.el.)





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-11 14:41 bug#45178: buffer-hash docstring should explain difference to secure-hash Stefan Kangas
@ 2020-12-11 16:00 ` Lars Ingebrigtsen
  2020-12-11 16:40   ` Stefan Kangas
  0 siblings, 1 reply; 7+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-11 16:00 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 45178

Stefan Kangas <stefan@marxist.se> writes:

> Severity: minor
>
> It is not clear to me when I would want to use:
>
>     (buffer-hash (current-buffer))
>
> instead of
>
>     (secure-hash 'sha1 (current-buffer))
>
> One obvious difference is that secure-hash respects narrowing, while
> buffer-hash doesn't.

I thought the buffer-hash doc string made the use case pretty clear:

----
This function is useful for comparing two buffers running in the same
Emacs, but is not guaranteed to return the same hash between different
Emacs versions.
----

The only use case for the function is to compare two buffers in the same
running Emacs image.  It shouldn't be used for anything else.

> First, I think we should add something about this to the docstring of
> `buffer-hash'.  Perhaps we should also mention it in the `secure-hash'
> docstring.

I think that would be pointing people in the wrong direction.
`secure-hash' is the right thing to use in 99% of the cases.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-11 16:00 ` Lars Ingebrigtsen
@ 2020-12-11 16:40   ` Stefan Kangas
  2020-12-11 16:49     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Kangas @ 2020-12-11 16:40 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 45178

Lars Ingebrigtsen <larsi@gnus.org> writes:

> The only use case for the function is to compare two buffers in the same
> running Emacs image.  It shouldn't be used for anything else.

I guess what I don't understand is why I would _ever_ want to use this
function.  I mean, why not just compare buffers using `secure-hash'?





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-11 16:40   ` Stefan Kangas
@ 2020-12-11 16:49     ` Lars Ingebrigtsen
  2020-12-11 19:19       ` Stefan Kangas
  2020-12-20 11:47       ` Basil L. Contovounesios
  0 siblings, 2 replies; 7+ messages in thread
From: Lars Ingebrigtsen @ 2020-12-11 16:49 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 45178

Stefan Kangas <stefan@marxist.se> writes:

> I guess what I don't understand is why I would _ever_ want to use this
> function.  I mean, why not just compare buffers using `secure-hash'?

`buffer-hash' is fast, so you can use it to compare the buffer contents
before and after doing things like `indent-sexp' to see whether that
changed anything.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-11 16:49     ` Lars Ingebrigtsen
@ 2020-12-11 19:19       ` Stefan Kangas
  2020-12-20 11:47       ` Basil L. Contovounesios
  1 sibling, 0 replies; 7+ messages in thread
From: Stefan Kangas @ 2020-12-11 19:19 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 45178

close 45178 28.1
thanks

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Stefan Kangas <stefan@marxist.se> writes:
>
>> I guess what I don't understand is why I would _ever_ want to use this
>> function.  I mean, why not just compare buffers using `secure-hash'?
>
> `buffer-hash' is fast, so you can use it to compare the buffer contents
> before and after doing things like `indent-sexp' to see whether that
> changed anything.

OK, I've benchmarked this and can confirm it is indeed much faster.  So
I updated its docstring on master by copying the explanation from the
manual.  I'm therefore closing this bug report.





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-11 16:49     ` Lars Ingebrigtsen
  2020-12-11 19:19       ` Stefan Kangas
@ 2020-12-20 11:47       ` Basil L. Contovounesios
  2020-12-20 16:31         ` Stefan Kangas
  1 sibling, 1 reply; 7+ messages in thread
From: Basil L. Contovounesios @ 2020-12-20 11:47 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Kangas, 45178

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Stefan Kangas <stefan@marxist.se> writes:
>
>> I guess what I don't understand is why I would _ever_ want to use this
>> function.  I mean, why not just compare buffers using `secure-hash'?
>
> `buffer-hash' is fast, so you can use it to compare the buffer contents
> before and after doing things like `indent-sexp' to see whether that
> changed anything.

[I get that this is just an example, but in this case wouldn't you use
 buffer-modified-tick or buffer-chars-modified-tick instead?]

-- 
Basil





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

* bug#45178: buffer-hash docstring should explain difference to secure-hash
  2020-12-20 11:47       ` Basil L. Contovounesios
@ 2020-12-20 16:31         ` Stefan Kangas
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Kangas @ 2020-12-20 16:31 UTC (permalink / raw)
  To: Basil L. Contovounesios, Lars Ingebrigtsen; +Cc: 45178

"Basil L. Contovounesios" <contovob@tcd.ie> writes:

> Lars Ingebrigtsen <larsi@gnus.org> writes:
>
>> `buffer-hash' is fast, so you can use it to compare the buffer contents
>> before and after doing things like `indent-sexp' to see whether that
>> changed anything.
>
> [I get that this is just an example, but in this case wouldn't you use
>  buffer-modified-tick or buffer-chars-modified-tick instead?]

AFAIU, this is used to detect when a command changed a buffer but the
buffer still is the same before and after the changes.  In that case, we
don't want it to be marked as changed even if there were changes taking
place in the background.  For example, see `fill-paragraph'.





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

end of thread, other threads:[~2020-12-20 16:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-11 14:41 bug#45178: buffer-hash docstring should explain difference to secure-hash Stefan Kangas
2020-12-11 16:00 ` Lars Ingebrigtsen
2020-12-11 16:40   ` Stefan Kangas
2020-12-11 16:49     ` Lars Ingebrigtsen
2020-12-11 19:19       ` Stefan Kangas
2020-12-20 11:47       ` Basil L. Contovounesios
2020-12-20 16:31         ` Stefan Kangas

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).