When the variable `org-highlight-latex-and-related' is set to `(latex)',
opening an Org file and inserting or deleting characters may cause too
much time lag, making text editing difficult.

For example, in my environment, when I opened the `text.org' created
with the command below and inserted a character at the beginning of the
first line, it took about 1 second for the typed character to appear.

┌────
│ yes `printf 'ABCDEFGHIJ%.0s' {1..8}` | head -c 10MB > text.org
└────

This issue was confirmed with Org 9.7.28 and
`80268c0e075bb69e7890146a8d7240e920fc786c', but it did not occur with an
older version `114de1571af27a5dbf7b103971ceec61de296637'.

The results measured with `profiler.el' by editing `text.org' on Org
9.7.28 are as follows.

┌────
│ 71332  99% - redisplay_internal (C function)
│ 71323  99%  - jit-lock-function
│ 71323  99%   - jit-lock-fontify-now
│ 71323  99%    - jit-lock--run-functions
│ 71323  99%     - #<compiled -0x156e10410a1857c3>
│ 71323  99%      - font-lock-fontify-region
│ 71323  99%       - font-lock-default-fontify-region
│ 71323  99%        - font-lock-fontify-keywords-region
│ 71277  99%         - org-do-latex-and-related
│ 71277  99%          - if
│ 71277  99%           - progn
│ 71277  99%            - let
│ 71277  99%             - catch
│ 71277  99%              - while
│ 71277  99%                 and
│    46   0%         + org-cite-activate
│    70   0% + command-execute
│    15   0% + ...
└────

Below are some other things I noticed and my thoughts.

1. The time lag increases in proportion to the file size, intuitively.

2. The closer the edit position is to the end of the file, the smaller
   the time lag.

3. If there is even one LaTeX formula a few lines below the editing
   position, the time lag will almost disappear.

Basically, every time a character is inserted or deleted,
`org-do-latex-and-related' is called. When the variable
`org-highlight-latex-and-related' is non-nil, `re-search-forward' is
called from this function.

When editing the beginning of `text.org', the `re-search-forward' call
took about 1.26 seconds. If the file had no LaTeX expressions, this time
was roughly proportional to the file size.

Versions prior to `80268c0e075bb69e7890146a8d7240e920fc786c' specify the
argument `BOUND' for `re-search-forward', so they are less likely to be
affected by file size and distance to the next LaTeX formula.

My execution environment is as follows.

• Ubuntu 24.04
• Emacs 29.3
• CPU: AMD Ryzen 7 5700G 3.8GHz

Reply via email to