emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

`format` slows down my function even though it shouldn't be called at al


From: Joost Kremers
Subject: `format` slows down my function even though it shouldn't be called at all...
Date: Sun, 15 Dec 2024 22:39:31 +0100
User-agent: mu4e 1.12.7; emacs 29.4

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]