mkdir -p ~/.vim/ftdetect
echo 'autocmd BufNew,BufNewFile,BufRead *.md :set filetype=markdown' > ~/.vim/ftdetect/markdown.vim
mkdir testing
cd testing
touch {1..500}.md
vim
:args *
Here's vim --version:
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 17 2017 12:13:35)
Included patches: 1-95
Modified by pkg-vim-m...@lists.alioth.debian.org
Compiled by pkg-vim-m...@lists.alioth.debian.org
Huge version with GTK3 GUI. Features included (+) or not (-):
+acl +file_in_path +mouse_sgr +tag_old_static
+arabic +find_in_path -mouse_sysmouse -tag_any_white
+autocmd +float +mouse_urxvt +tcl
+balloon_eval +folding +mouse_xterm +termguicolors
+browse -footer +multi_byte +terminfo
++builtin_terms +fork() +multi_lang +termresponse
+byte_offset +gettext -mzscheme +textobjects
+channel -hangul_input +netbeans_intg +timers
+cindent +iconv +num64 +title
+clientserver +insert_expand +packages +toolbar
+clipboard +job +path_extra +user_commands
+cmdline_compl +jumplist +perl +vertsplit
+cmdline_hist +keymap +persistent_undo +virtualedit
+cmdline_info +lambda +postscript +visual
+comments +langmap +printer +visualextra
+conceal +libcall +profile +viminfo
+cryptv +linebreak -python +vreplace
+cscope +lispindent +python3 +wildignore
+cursorbind +listcmds +quickfix +wildmenu
+cursorshape +localmap +reltime +windows
+dialog_con_gui +lua +rightleft +writebackup
+diff +menu +ruby +X11
+digraphs +mksession +scrollbind -xfontset
+dnd +modify_fname +signs +xim
-ebcdic +mouse +smartindent +xpm
+emacs_tags +mouseshape +startuptime +xsmp_interact
+eval +mouse_dec +statusline +xterm_clipboard
+ex_extra +mouse_gpm -sun_workshop -xterm_save
+extra_search -mouse_jsbterm +syntax
+farsi +mouse_netterm +tag_binary
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
system gvimrc file: "$VIM/gvimrc"
user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
defaults file: "$VIMRUNTIME/defaults.vim"
system menu file: "$VIMRUNTIME/menu.vim"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/mirclient -I/usr/include/mircore -I/usr/include/mircookie -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -Wdate-time -g -O2 -fdebug-prefix-map=/build/vim-8krYYf/vim-8.0.0095=. -fPIE -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lnsl -lselinux -lacl -lattr -lgpm -ldl -L/usr/lib -llua5.2 -Wl,-E -fstack-protector-strong -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu/perl/5.24/CORE -lperl -ldl -lm -lpthread -lcrypt -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5m -lpthread -ldl -lutil -lm -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lieee -lm -lruby-2.3 -lpthread -lgmp -ldl -lcrypt -lm
The docs *do* say that if you are detecting files based purely on names you should instead use an autload group in a filetype.vim file, but it's pretty far down in the docs and a *lot* of people seem to have made this mistake: https://github.com/search?p=4&q=%22set+ft%3Dmarkdown%22+language%3Avim&type=Code&utf8=%E2%9C%93
I would like to know why this is slower, but more, if filetype.vim needs to be used for filename based filetype detection, and functions need to be defined in scripts.vim for content detection, what good is ftdetect anyway?
Thanks,
fREW
Frew Schmidt wrote:
> I recently discovered that using ftdetect is absurdly slower than filetype.vim. To reproduce, try this:
>
> mkdir -p ~/.vim/ftdetect
> echo 'autocmd BufNew,BufNewFile,BufRead *.md :set filetype=markdown' > ~/.vim/ftdetect/markdown.vim
> mkdir testing
> cd testing
> touch {1..500}.md
> vim
> :args *
What do you compare this with? Filetype detection already has this rule
for markdown, thus the extra markdown.vim file isn't needed.
Perhaps you can get some more information by setting 'verbose' to a
non-zero number, e.g. ten.
From your logs it looks like one run detects the filetype only for the
one file that is loaded, while the other does it for every file in the
argiument list. Looks like something causes loading of every file.
> Here's vim --version:
>
> VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 17 2017 12:13:35)
> Included patches: 1-95
> Modified by pkg-vim-maintainers@lists.alioth.debian.org
> Compiled by pkg-vim-maintainers@lists.alioth.debian.org
Well, I ran it with -V10 and even after deleting both ~/.vim/ (except for the ftdetect file) and ~/.vimrc and I can still reproduce it. It looks about the same as before: the markdown support is getting reloaded for every file instead of once at the beginning. I don't know how to store :messages in a file or I'd attach them.
Another interesting detail: it's not slow if I set the filetype to sillybonk or perl, so this is somehow related to the markdown support. I think I'll bisect the markdown files and see which one causes it, and then bisect the file itself.
If I replace the contents of /usr/share/vim/vim80/syntax/vb.vim with `let b:current_syntax = "vb"` everything is fast and, as I would hope, the support files are not loaded N times per buffer but instead exactly once.
I verified as well by putting the contents back and setting the filetype to `vb` and I can continue to reproduce the issue.
I still don't know why ftdetect causes this and filetype.vim does not, and I also don't see what's wrong with the vb support files.
Well I have already repro'd this on a fresh 7.3, 7.4, 8.0, and system 8.0. Here's a reproduction using docker, with no dotfiles or even system vim installed:
$ docker run -it --rm library/buildpack-deps bash -l
$ git clone git://github.com/vim/vim
$ make
$ make install
$ mkdir -p ~/.vim/ftdetect
$ echo 'autocmd BufNew,BufNewFile,BufRead *.md :set filetype=markdown' > ~/.vim/ftdetect/markdown.vim
$ mkdir testing
$ cd testing
$ touch {1..500}.md
$ vim
:args *
It still happens, and this is from master, today.
If you don't have time to check this yourself I get it. I have multiple
workarounds at this point.
Thanks,
fREW Schmidt
--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/m7t32w_n6wc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+unsubscribe@googlegroups.com.
Maybe a dumb question, but why would BufNew be more expensive than BufRead? Shouldn't the filetype stuff get run either way? I wouldn't expect BufRead to take 500ms and BufNew to take multiple seconds, would you?
> [...]
>
> I am not sure, if this behaviour is a bug. However after
> thinking some more about it, I am wondering, whether the
> fact that the BufNew autocommand triggers a FileType
> autocommand is a bug itself, since autocommands shouldn't
> nest (see :h autocmd-nested). But I haven't looked into
> the detail, why the FileType autocommand is triggered.
> (And I have a feeling, that when this behaviour is
> changed, it might actually break a lot of ftdetect
> scripts.)
I was under the impression that nesting only applied to :e
and :w. The documentation at `:h setfiletype` implies to me
that it does nest, hence the inclusion of that command. You
can even set 'ft' within a FileType autocommand and then
trigger a different FileType autocommand. --Antony