unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* `format` slows down my function even though it shouldn't be called at all...
@ 2024-12-15 21:39 Joost Kremers
  2024-12-15 22:31 ` Joost Kremers
  0 siblings, 1 reply; 2+ messages in thread
From: Joost Kremers @ 2024-12-15 21:39 UTC (permalink / raw)
  To: Emacs development discussions.

Hi,

I have a library for parsing `.bib` files[1] and today a user reported an
issue[2] that lead me to a weird discovery. Basically, a call to `format`
that is never even executed slows down execution tremendously.

The reader part of the parser consists of a couple of functions that all
have the following structure:

```
(defun parsebib--chars (chars &optional noerror)
  "Read the character at point.
CHARS is a list of characters.  If the character at point matches
a character in CHARS, return it and move point, otherwise signal
an error, unless NOERROR is non-nil, in which case return nil."
  (parsebib--skip-whitespace)
  (if (memq (char-after) chars)
      (prog1
          (char-after)
        (forward-char 1))
    (unless noerror
      (signal 'parsebib-error (list (format "Expected one of %S, got %c at position %d,%d"
                                            chars
                                            (following-char)
                                            (line-number-at-pos) (current-column)))))))
```

In short, after skipping whitespace, the reader functions try to read some
element (character, keyword, etc. depending on the function), return it if
it's found and signal an error if it's not found.

The call to `signal` contains a call to `format` to provide a useful error
message. It's this `format` that slows down parsing, even though no errors
are ever signalled.

This is an excerpt from a profiler report:

============================================================
       17758  81%                     - parsebib--chars
       17755  80%                      - if
       17749  80%                       - if
       17737  80%                        - signal
       17737  80%                         - list
       17515  79%                            format
============================================================

This is from parsing a 28MB .bib file: The 79% of processing time spent in
`format` seems very weird to me, given that the file contains no errors and
no error is ever signalled.

After removing the `format` calls, replacing them with a simple string, the
parser runs much, much, much faster. To give an idea of the speed increase:
without `format`, the 28MB .bib file I mentioned above is parsed in 1-2
seconds (on my machine); with `format`, I don't even have enough patience
to wait for parsing to finish... (I let it run for at least 20-30 seconds
before interrupting it.)

Anyone know what's going on here? Am I missing something, or could this be
a bug in Emacs? (I'm running Emacs 29.4, BTW).

TIA

Joost



Footnotes:
[1]  At https://github.com/joostkremers/parsebib

[2]  https://github.com/joostkremers/parsebib/issues/34

-- 
Joost Kremers
Life has its moments



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

* Re: `format` slows down my function even though it shouldn't be called at all...
  2024-12-15 21:39 `format` slows down my function even though it shouldn't be called at all Joost Kremers
@ 2024-12-15 22:31 ` Joost Kremers
  0 siblings, 0 replies; 2+ messages in thread
From: Joost Kremers @ 2024-12-15 22:31 UTC (permalink / raw)
  To: Emacs development discussions.

I did it again... Right after posting my message, I find the cause of the
problem. Sigh... Ignore please.

(For anyone wondering: the parser actually signals many errors, but most of
them are caught using `condition-case`...)

On Sun, Dec 15 2024, Joost Kremers wrote:
> Hi,
>
> I have a library for parsing `.bib` files[1] and today a user reported an
> issue[2] that lead me to a weird discovery. Basically, a call to `format`
> that is never even executed slows down execution tremendously.
>
> The reader part of the parser consists of a couple of functions that all
> have the following structure:
>
> ```
> (defun parsebib--chars (chars &optional noerror)
>   "Read the character at point.
> CHARS is a list of characters.  If the character at point matches
> a character in CHARS, return it and move point, otherwise signal
> an error, unless NOERROR is non-nil, in which case return nil."
>   (parsebib--skip-whitespace)
>   (if (memq (char-after) chars)
>       (prog1
>           (char-after)
>         (forward-char 1))
>     (unless noerror
>       (signal 'parsebib-error (list (format "Expected one of %S, got %c at position %d,%d"
>                                             chars
>                                             (following-char)
>                                             (line-number-at-pos) (current-column)))))))
> ```
>
> In short, after skipping whitespace, the reader functions try to read some
> element (character, keyword, etc. depending on the function), return it if
> it's found and signal an error if it's not found.
>
> The call to `signal` contains a call to `format` to provide a useful error
> message. It's this `format` that slows down parsing, even though no errors
> are ever signalled.
>
> This is an excerpt from a profiler report:
>
> ============================================================
>        17758  81%                     - parsebib--chars
>        17755  80%                      - if
>        17749  80%                       - if
>        17737  80%                        - signal
>        17737  80%                         - list
>        17515  79%                            format
> ============================================================
>
> This is from parsing a 28MB .bib file: The 79% of processing time spent in
> `format` seems very weird to me, given that the file contains no errors and
> no error is ever signalled.
>
> After removing the `format` calls, replacing them with a simple string, the
> parser runs much, much, much faster. To give an idea of the speed increase:
> without `format`, the 28MB .bib file I mentioned above is parsed in 1-2
> seconds (on my machine); with `format`, I don't even have enough patience
> to wait for parsing to finish... (I let it run for at least 20-30 seconds
> before interrupting it.)
>
> Anyone know what's going on here? Am I missing something, or could this be
> a bug in Emacs? (I'm running Emacs 29.4, BTW).
>
> TIA
>
> Joost
>
>
>
> Footnotes:
> [1]  At https://github.com/joostkremers/parsebib
>
> [2]  https://github.com/joostkremers/parsebib/issues/34

-- 
Joost Kremers
Life has its moments



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

end of thread, other threads:[~2024-12-15 22:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-15 21:39 `format` slows down my function even though it shouldn't be called at all Joost Kremers
2024-12-15 22:31 ` Joost Kremers

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