Live data from Hacker News

Vim's 400 line function to wait for keyboard input

geoff.greer.fm

1–10 of 239 posts

Re: Vim's 400 line function to wait for keyboard input

#4

To my knowledge, maintaining compatibility is one of the stated goals of Vim, even at the expense of performance. NeoVim is supposed to strip out some of these antiquated features.

But the ifdefs likely negate performance concerns for the most part.

Decomposing this function into different implementations with appropriate abstraction doesn't need to break compatibility.

Re: Vim's 400 line function to wait for keyboard input

#5
post #2

The question is do you think it could have been done better given that they support multiple OS/UI, etc...

You can refactor it into multiple functions (#ifdefs around a line containing just "||"? seriously??), some of which are no-ops on some platforms / configurations, and let the compiler deal with inlining the functions that exist and optimizing out those that don't. (Not that you would have noticed the overhead anyway, probably.)

Re: Vim's 400 line function to wait for keyboard input

#6
To answer most questions here: Yes, nowadays there are cross-platform libraries you can use instead of implementing that yourself. And yes code that has grown for centuries and contains unnecessary things or patterns that aren't used nowadays can be refactored. Or at least that's how I'd interpret the article.

Re: Vim's 400 line function to wait for keyboard input

#7
I'm a bit surprised that they have code like this:

        if (msec > 0 && (
    #  ifdef FEAT_XCLIPBOARD
            xterm_Shell != (Widget)0
    #   if defined(USE_XSMP) || defined(FEAT_MZSCHEME)
            ||
    #   endif
    #  endif
    #  ifdef USE_XSMP
            xsmp_icefd != -1
    #   ifdef FEAT_MZSCHEME
            ||
    #   endif
    #  endif
    #  ifdef FEAT_MZSCHEME
        (mzthreads_allowed() && p_mzq > 0)
    #  endif
            ))
When they could have written:

        if (msec > 0 && (
    #  ifdef FEAT_XCLIPBOARD
            xterm_Shell != (Widget)0 ||
    #  endif
    #  ifdef USE_XSMP
            xsmp_icefd != -1 ||
    #  endif
    #  ifdef FEAT_MZSCHEME
        (mzthreads_allowed() && p_mzq > 0) ||
    #  endif
            0))
Perhaps they were targeting compilers so primitive that they could not optimize out a "|| 0".

(edited to hide my shame)

Re: Vim's 400 line function to wait for keyboard input

#8
This is a classic case of why legacy code is a nightmare to maintain. It's the OpenSSL situation all over again, and one can definitely see the appeal of going through there and ripping out all of the functionality that nobody uses anymore just to make maintenance less of a nightmare. Luckily vim doesn't run suid root on any sane system, so all of this legacy cruft is not as huge of a threat surface on the machine.

Re: Vim's 400 line function to wait for keyboard input

#9
Does this have a noticeable performance impact for a typical vim end user, using it in an interactive file editing workflow? On any modern hardware?

I suppose there may be some negative performance impact if we need to use it for an automated/batch workflow (I can't think of many where something like 'sed' won't be better suited).

Re: Vim's 400 line function to wait for keyboard input

#10

To my knowledge, maintaining compatibility is one of the stated goals of Vim, even at the expense of performance. NeoVim is supposed to strip out some of these antiquated features.

Also, it just so happens that the people working on NeoVim are currently porting all IO to libuv (the cross-platform library mentioned in the article).

https://github.com/neovim/neovim#whats-being-worked-on-now

Post reply on HN