Skip to content

Skip loading syntax/pandoc.vim if it's loaded #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025

Conversation

jez
Copy link
Contributor

@jez jez commented Apr 16, 2025

I noticed that opening markdown files was slow and was trying to
optimize the startup.

When I run

echo "" > foo.md
vim --startuptime startup.log foo.md +q

I noticed in the startup.log that some of the heavier lines in the
trace looked redundant. Specifically, I noticed that it was running
syntax/pandoc.vim multiple times for multiple paths, because Neovim
seems to now distribute a syntax/pandoc.vim in the distribution's
$VIMRUNTIME path.

What's happening is that the syntax/pandoc.vim file loads a bunch of
other syntax files to be able to highlight embedded languages in code
blocks.

That happens every time the files are run, regardless of whether we're
already loaded a syntax definition for pandoc once, because there is no
b:current_syntax guard at the top of the file.

Adding the guard in this plugin doesn't actually speed anything up. The
speedup only happens when it's made both here and in the $VIMRUNTIME
distribution files:

❯ rg syntax/pandoc.vim startup.{old,new,both}.log
startup.old.log
272.540  025.063  003.008: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
296.343  023.082  002.412: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
297.630  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
361.296  024.097  002.412: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
385.468  023.663  002.443: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
385.853  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
146.991  028.210  003.364: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
175.621  027.840  003.237: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
176.466  000.011  000.011: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
241.812  025.036  002.764: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
269.082  026.633  003.041: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
269.506  000.009  000.009: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

startup.new.log
160.321  024.925  003.534: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
184.483  022.947  002.397: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
186.524  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
251.792  022.884  002.144: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
275.302  023.001  002.316: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
275.634  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
152.428  025.671  003.712: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
176.968  023.461  002.483: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
179.083  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
243.327  023.138  002.234: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
266.798  022.957  002.340: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
267.128  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

startup.both.log
175.965  027.418  003.803: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
177.070  000.007  000.007: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
178.088  000.009  000.009: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
242.550  024.351  002.335: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
243.092  000.006  000.006: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
243.447  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
147.441  026.421  003.213: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
148.317  000.008  000.008: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
149.163  000.010  000.010: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
219.914  026.378  002.627: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
220.423  000.006  000.006: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
220.797  000.010  000.010: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

❯ wc -l startup.{old,new,both}.log
   624 startup.old.log
   624 startup.new.log
   520 startup.both.log

What we see happening in these files is that only adding the
b:current_syntax guard in this plugin only saves ~2ms (269ms → 267ms).

But once we add it to both the plugin and the distribution, then it
completely skips running the syntax/pandoc.vim the second time, which
also skips loading all the embedded syntax files (represented by a drop
in the number of entries in the startup.log files from 624 lines to 520
lines). This drops the total load time from 269ms → 220ms.

For the time being, I have manually edited my
$VIMRUNTIME/syntax/pandoc.vim file to also include these changes, but
it will be a subsequent priority to make sure that these plugin changes
get pulled into the distribution.

I noticed that opening markdown files was slow and was trying to
optimize the startup.

When I run

    echo "" > foo.md
    vim --startuptime startup.log foo.md +q

I noticed in the `startup.log` that some of the heavier lines in the
trace looked redundant. Specifically, I noticed that it was running
`syntax/pandoc.vim` multiple times for multiple paths, because Neovim
seems to now distribute a `syntax/pandoc.vim` in the distribution's
`$VIMRUNTIME` path.

What's happening is that the `syntax/pandoc.vim` file loads a bunch of
other syntax files to be able to highlight embedded languages in code
blocks.

That happens every time the files are run, regardless of whether we're
already loaded a syntax definition for pandoc once, because there is no
`b:current_syntax` guard at the top of the file.

Adding the guard in this plugin doesn't actually speed anything up. The
speedup only happens when it's made both here and in the `$VIMRUNTIME`
distribution files:

    ❯ rg syntax/pandoc.vim startup.{old,new,both}.log
    startup.old.log
    272.540  025.063  003.008: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    296.343  023.082  002.412: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    297.630  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    361.296  024.097  002.412: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    385.468  023.663  002.443: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    385.853  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    146.991  028.210  003.364: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    175.621  027.840  003.237: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    176.466  000.011  000.011: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    241.812  025.036  002.764: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    269.082  026.633  003.041: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    269.506  000.009  000.009: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

    startup.new.log
    160.321  024.925  003.534: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    184.483  022.947  002.397: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    186.524  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    251.792  022.884  002.144: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    275.302  023.001  002.316: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    275.634  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    152.428  025.671  003.712: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    176.968  023.461  002.483: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    179.083  000.008  000.008: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    243.327  023.138  002.234: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    266.798  022.957  002.340: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    267.128  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

    startup.both.log
    175.965  027.418  003.803: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    177.070  000.007  000.007: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    178.088  000.009  000.009: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    242.550  024.351  002.335: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    243.092  000.006  000.006: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    243.447  000.007  000.007: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    147.441  026.421  003.213: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    148.317  000.008  000.008: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    149.163  000.010  000.010: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim
    219.914  026.378  002.627: sourcing /Users/jez/.config/nvim/bundle/vim-pandoc-syntax/syntax/pandoc.vim
    220.423  000.006  000.006: sourcing /opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime/syntax/pandoc.vim
    220.797  000.010  000.010: sourcing /Users/jez/.config/nvim/after/syntax/pandoc.vim

    ❯ wc -l startup.{old,new,both}.log
       624 startup.old.log
       624 startup.new.log
       520 startup.both.log

What we see happening in these files is that **only** adding the
`b:current_syntax` guard in this plugin only saves ~2ms (269ms → 267ms).

But once we add it to both the plugin and the distribution, then it
completely skips running the `syntax/pandoc.vim` the second time, which
also skips loading all the embedded syntax files (represented by a drop
in the number of entries in the startup.log files from 624 lines to 520
lines). This drops the total load time from 269ms → 220ms.

For the time being, I have manually edited my
`$VIMRUNTIME/syntax/pandoc.vim` file to also include these changes, but
it will be a subsequent priority to make sure that these plugin changes
get pulled into the distribution.
@jez
Copy link
Contributor Author

jez commented Apr 16, 2025

Note: the linter failure on this change is an infrastructure problem, not a problem with this PR.

See #398.

@alerque alerque merged commit f3f2040 into vim-pandoc:master Apr 17, 2025
1 check failed
jez added a commit to jez/vim-pandoc-syntax that referenced this pull request Apr 17, 2025
This was a bad merge that resulted from vim-pandoc#393 being opened before vim-pandoc#397
but landing after.

If we override the `&cpo` option and then `finish`, it will never get
set back to the saved cpo.

Instead, let's just only do the `set` if we're sure we're not going to
early exit.
alerque pushed a commit that referenced this pull request Apr 18, 2025
This was a bad merge that resulted from #393 being opened before #397
but landing after.

If we override the `&cpo` option and then `finish`, it will never get
set back to the saved cpo.

Instead, let's just only do the `set` if we're sure we're not going to
early exit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants