[vim/vim] Improve filetype.vim perfomance by moving autocmd to vim9script (PR #18508)

11 views
Skip to first unread message

BenYip

unread,
2:41 AM (8 hours ago) 2:41 AM
to vim/vim, Subscribed

This PR moves a lot of autocmd in filetype.vim into two functions in autoload/dist/ft.vim:

export def DetectByName()
  const amatch = expand("<amatch>")
  const name = fnamemodify(amatch, ':p:t')
  const ft = get(byname, name, '')
  if ft != ''
    execute "setf " .. ft
  endif
enddef

export def DetectByExt()
  const amatch = expand("<amatch>")
  var ext = fnamemodify(amatch, ':e')
  const name = fnamemodify(amatch, ':p:t')
  if ext == '' && name[0] == '.'
    ext = name[1 : ]
  endif
  const ft =  get(byextesion, ext, '')
  if ft != ''
    execute "setf " .. ft
  endif
enddef

const byextesion = {
  # 8th (Firth-derivative)
  "8th": "8th",
...
}
const byname = {
  # Ant
  "build.xml": "ant",
...
}

In filetype.vim, these autocmd is replace by:

au BufNewFile,BufRead *				call dist#ft#DetectByName()
...
au BufNewFile,BufRead *				call dist#ft#DetectByExt()

After this PR, autocmd in filetype.vim reduces from 1137 to 490.
On my machine, the load time of filetype.vim reduces 7.55ms to 3.80ms.

byextesion, byname and the new filetype.vim are generated from this python script(run it under runtime/, after commit d13744b):

import re
from pathlib import Path

lines = open(Path("filetype.vim")).readlines()

ext_regex = r"^au BufNewFile,BufRead (\*\.[a-zA-Z0-9_+-]+(?:,\*\.[a-zA-Z0-9_+-]+)*)\s+setf\s+(\S+)$"
name_regex = r"^au BufNewFile,BufRead ([a-zA-Z0-9_.+-,]+)\s+setf\s+(\S+)$"

outfiletype = open("next-filetype.vim", "w")

outvim9 = open("vim9ft.vim", 'w')


i = 0
N = len(lines)
extlines = ["const byextesion = {"]
namelines = ["const byname = {"]
indent = "  "
while lines[i] != "\n":
    outfiletype.write(lines[i])
    i += 1
outfiletype.write(lines[i])
i += 1

while i < N:
    if lines[i].startswith('"'):
        comments = []

        while i < N and lines[i][0] == '"':
            comments.append(lines[i][1:-1])
            i+=1

        vim8comment = '\n'.join(('"' + x for x in comments))
        vim9comment = '\n'.join((indent + '#' + x for x in comments))

        if i >= N or lines[i] == '\n':
            outfiletype.write(vim8comment + "\n")
            continue


        ext_has_commnet = False
        name_has_commnet = False

        while i < N:
            if mat := re.match(ext_regex, lines[i]):
                ft = mat.group(2)

                if not ext_has_commnet:
                    extlines.append(vim9comment)
                    ext_has_commnet = True

                for ext in mat.group(1).split(","):
                    ext = ext[2:]

                    extlines.append(f'{indent}"{ext}": "{ft}",')

                i += 1
            elif mat := re.match(name_regex, lines[i]):
                name = mat.group(1)
                ft = mat.group(2)

                if not name_has_commnet:
                    namelines.append(vim9comment)
                    name_has_commnet = True

                for name in mat.group(1).split(","):
                    namelines.append(f'{indent}"{name}": "{ft}",')

                i += 1
            elif lines[i] == "\n":
                i += 1
                break
            else:
                outfiletype.write(vim8comment + "\n")
                break

    else:
        outfiletype.write(lines[i])
        i += 1

extlines.append("}")
namelines.append("}")

outvim9.write('vim9script\n')
outvim9.write("\n".join(extlines))
outvim9.write('\n')
outvim9.write("\n".join(namelines))
# outvim9.write('\ndefcompile\n')

outvim9.close()
outfiletype.close()

You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/18508

Commit Summary

  • d13744b Clean filetype.vim
  • 6514260 If we can detect filetype from name and extension, do it in vim9

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/18508@github.com>

BenYip

unread,
3:43 AM (7 hours ago) 3:43 AM
to vim/vim, Push

@bennyyip pushed 1 commit.

  • e64b6c5 Move dist#ft#DetectByExt before scritps.vim


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/18508/before/65142603b355d4c8c87e0e30e02077fb0ccaa100/after/e64b6c5c42ee7266447d934e168a761e48b5fa09@github.com>

BenYip

unread,
4:16 AM (7 hours ago) 4:16 AM
to vim/vim, Push

@bennyyip pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/18508/before/e64b6c5c42ee7266447d934e168a761e48b5fa09/after/1cd319eeddf3059def8da01cc8f8ed3d29f6b4c1@github.com>

Reply all
Reply to author
Forward
0 new messages