Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Friday, August 23, 2013

if_lua accelerates Vim scripts

Vim has some foreign language interfaces available for Vim script, to allow you to make a Vim plugin with using other programming languages besides Vim script, such as Python, Scheme, or Ruby. This is not only handy that you can attempt to make your code more readable with using ways of writing code that Vim script can't or using libraries exclusive for the language, but also provides faster computation. Vim script runs slower than many of you expect.

Lua is a general purpose programming language which works particularly well about embedding to an existing system written in C. It is very natural that Vim has lua interface as if_lua.

vim lua

The significant part of if_lua is that it works really fast.

Comparison between Pure Vim script, if_lua, and if_python: https://github.com/vim-jp/issues/issues/48#issuecomment-15355516

  • Vim script: 1.4985sec
  • Luajit: 0.0013sec (1152 times faster than Vim script)
  • Python: 0.0272sec (20 times faster than Vim script)

Time to start

  • if_lua = 0.000297sec
  • if_python = 0.016656sec
  • if_ruby = 0.039039sec

Now it's default!

gentoo

gentoo

I submitted a patch to Gentoo Linux 10 month ago, and it was accepted finally in a couple days ago!

So if you simply emerge vim or gvim with adding lua and luajit USE flags, the vim will have lua integration!

FAQ

  • Does speed matter? My Vim already works fast enough.
    • Speed matters. Some auto-completion plugins gave up providing powerful functionality since it took too much time. If it's fast enough that users can't recognize, the authors can implement more functionalities.
  • Python is slower than Luajit and Lua, but it's more popular among Vim plugin authors. We should keep using Python.
    • Popularity is just a chicken-or-the-egg problem. If somebody makes a effort to make an egg, that will eventually grows up to a chicken.
  • Should I re-implement all the Vim script codes in my Vim plugin? That's too much work...
    • No. Running profiler and writing the bottleneck part in Lua is usually enough. Keep the pure Vim script part for people who don't have if_lua for a while.
  • Which plugins are using if_lua for performance? I'd like to refer and learn the technique there.
  • What if_lua can't do whereas others like if_python can do?
    • Multithreading. This is pros.
    • Sockets. You may want to ask users to install vimproc to use network connections.
    • Non-blocking IO. Use vimproc instead like above.
    • In short: if_lua is like faster Vim script; it doesn't add superpower.

Past discussions

Vim Advent Calendar 2012

There is a tech advent calendar ongoing since December 2012 which is all about Vim. This article is for that as its 266th day.

I previously posted an article for Vim Advent Calendar 2012 about if_lua and the patch described above on January 17, 2013. The post is http://vim-users.jp/2013/01/vim-advent-calendar-2012-ujihisa-2/ and is written in Japanese.

Followers