Skip to content

Commit e5e0fbc

Browse files
committed
patch 8.0.0504: looking up an Ex command is a bit slow
Problem: Looking up an Ex command is a bit slow. Solution: Instead of just using the first letter, also use the second letter to skip ahead in the list of commands. Generate the table with a Perl script. (Dominique Pelle, closes #1589)
1 parent 9d20ce6 commit e5e0fbc

5 files changed

Lines changed: 176 additions & 35 deletions

File tree

Filelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ SRC_UNIX = \
215215
src/config.mk.in \
216216
src/configure \
217217
src/configure.ac \
218+
src/create_cmdidxs.pl \
218219
src/gui_at_fs.c \
219220
src/gui_at_sb.c \
220221
src/gui_at_sb.h \

src/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,16 @@ autoconf:
18841884
-rm -rf autom4te.cache
18851885
-rm -f auto/config.status auto/config.cache
18861886

1887+
# Run Perl to generate the Ex command lookup table. This only needs to be run
1888+
# when a command name has been added or changed.
1889+
# NOTE: Only works when perl and vim executables are available
1890+
cmdidxs: ex_cmds.h
1891+
if test X`perl -e "print 123"` = "X123"; then \
1892+
vim ex_docmd.c -c '/Beginning.*create_cmdidxs/,/End.*create_cmdidxs/! perl create_cmdidxs.pl' -c wq; \
1893+
else \
1894+
echo Cannot run Perl; \
1895+
fi
1896+
18871897
# Re-execute this Makefile to include the new auto/config.mk produced by
18881898
# configure Only used when typing "make" with a fresh auto/config.mk.
18891899
myself:

src/create_cmdidxs.pl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/perl -w
2+
#
3+
# This script generates the tables cmdidxs1[] and cmdidxs2[][] which,
4+
# given a Ex command, determine the first value to probe to find
5+
# a matching command in cmdnames[] based on the first character
6+
# and the first 2 characters of the command.
7+
# This is used to speed up lookup in cmdnames[].
8+
#
9+
# Script should be run every time new Ex commands are added in Vim,
10+
# from the src/vim directory, since it reads commands from "ex_cmds.h".
11+
12+
# Find the list of Vim commands from cmdnames[] table in ex_cmds.h
13+
my @cmds;
14+
my @skipped;
15+
open(IN, "< ex_cmds.h") or die "can't open ex_cmds.h: $!\n";
16+
while (<IN>) {
17+
if (/^EX\(CMD_\S*,\s*"([a-z][^"]*)"/) {
18+
push (@cmds, $1);
19+
} elsif (/^EX\(CMD_/) {
20+
push (@skipped, $1);
21+
}
22+
}
23+
24+
my %cmdidxs1;
25+
my %cmdidxs2;
26+
27+
for (my $i = $#cmds; $i >= 0; --$i) {
28+
my $cmd = $cmds[$i];
29+
my $c1 = substr($cmd, 0, 1); # First character of command.
30+
31+
$cmdidxs1{$c1} = $i;
32+
33+
if (length($cmd) > 1) {
34+
my $c2 = substr($cmd, 1, 1); # Second character of command.
35+
$cmdidxs2{$c1}{$c2} = $i if (('a' lt $c2) and ($c2 lt 'z'));
36+
}
37+
}
38+
39+
print "/* Beginning of automatically generated code by create_cmdidxs.pl\n",
40+
" *\n",
41+
" * Table giving the index of the first command in cmdnames[] to lookup\n",
42+
" * based on the first letter of a command.\n",
43+
" */\n",
44+
"static const unsigned short cmdidxs1[26] =\n{\n",
45+
join(",\n", map(" /* $_ */ $cmdidxs1{$_}", ('a' .. 'z'))),
46+
"\n};\n",
47+
"\n",
48+
"/*\n",
49+
" * Table giving the index of the first command in cmdnames[] to lookup\n",
50+
" * based on the first 2 letters of a command.\n",
51+
" * Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they\n",
52+
" * fit in a byte.\n",
53+
" */\n",
54+
"static const unsigned char cmdidxs2[26][26] =\n",
55+
"{ /* a b c d e f g h i j k l m n o p q r s t u v w x y z */\n";
56+
for my $c1 ('a' .. 'z') {
57+
print " /* $c1 */ {";
58+
for my $c2 ('a' .. 'z') {
59+
if (exists $cmdidxs2{$c1}{$c2}) {
60+
printf "%3d,", $cmdidxs2{$c1}{$c2} - $cmdidxs1{$c1};
61+
} else {
62+
printf " 0,";
63+
}
64+
}
65+
print " }";
66+
print "," unless ($c1 eq 'z');
67+
print "\n";
68+
}
69+
print "};\n",
70+
"\n",
71+
"static int command_count = ", $#cmds + $#skipped + 2 , ";\n",
72+
"\n",
73+
"/* End of automatically generated code by create_cmdidxs.pl */\n";
74+

src/ex_docmd.c

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -495,40 +495,81 @@ static void ex_folddo(exarg_T *eap);
495495
#define DO_DECLARE_EXCMD
496496
#include "ex_cmds.h"
497497

498+
/* Beginning of automatically generated code by create_cmdidxs.pl
499+
*
500+
* Table giving the index of the first command in cmdnames[] to lookup
501+
* based on the first letter of a command.
502+
*/
503+
static const unsigned short cmdidxs1[26] =
504+
{
505+
/* a */ 0,
506+
/* b */ 19,
507+
/* c */ 42,
508+
/* d */ 103,
509+
/* e */ 125,
510+
/* f */ 145,
511+
/* g */ 161,
512+
/* h */ 167,
513+
/* i */ 176,
514+
/* j */ 194,
515+
/* k */ 196,
516+
/* l */ 201,
517+
/* m */ 259,
518+
/* n */ 277,
519+
/* o */ 297,
520+
/* p */ 309,
521+
/* q */ 348,
522+
/* r */ 351,
523+
/* s */ 370,
524+
/* t */ 437,
525+
/* u */ 472,
526+
/* v */ 483,
527+
/* w */ 501,
528+
/* x */ 516,
529+
/* y */ 525,
530+
/* z */ 526
531+
};
532+
498533
/*
499-
* Table used to quickly search for a command, based on its first character.
500-
*/
501-
static cmdidx_T cmdidxs[27] =
502-
{
503-
CMD_append,
504-
CMD_buffer,
505-
CMD_change,
506-
CMD_delete,
507-
CMD_edit,
508-
CMD_file,
509-
CMD_global,
510-
CMD_help,
511-
CMD_insert,
512-
CMD_join,
513-
CMD_k,
514-
CMD_list,
515-
CMD_move,
516-
CMD_next,
517-
CMD_open,
518-
CMD_print,
519-
CMD_quit,
520-
CMD_read,
521-
CMD_substitute,
522-
CMD_t,
523-
CMD_undo,
524-
CMD_vglobal,
525-
CMD_write,
526-
CMD_xit,
527-
CMD_yank,
528-
CMD_z,
529-
CMD_bang
534+
* Table giving the index of the first command in cmdnames[] to lookup
535+
* based on the first 2 letters of a command.
536+
* Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they
537+
* fit in a byte.
538+
*/
539+
static const unsigned char cmdidxs2[26][26] =
540+
{ /* a b c d e f g h i j k l m n o p q r s t u v w x y z */
541+
/* a */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 7, 15, 0, 16, 0, 0, 0, 0, 0, },
542+
/* b */ { 0, 0, 0, 4, 5, 7, 0, 0, 0, 0, 0, 8, 9, 10, 11, 12, 0, 13, 0, 0, 0, 0, 22, 0, 0, 0, },
543+
/* c */ { 0, 10, 12, 14, 16, 18, 21, 0, 0, 0, 0, 29, 33, 36, 42, 51, 53, 54, 55, 0, 57, 0, 60, 0, 0, 0, },
544+
/* d */ { 0, 0, 0, 0, 0, 0, 0, 0, 6, 15, 0, 16, 0, 0, 17, 0, 0, 19, 20, 0, 0, 0, 0, 0, 0, 0, },
545+
/* e */ { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, },
546+
/* f */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, },
547+
/* g */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 4, 5, 0, 0, 0, 0, },
548+
/* h */ { 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
549+
/* i */ { 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 5, 6, 0, 0, 0, 0, 0, 13, 0, 15, 0, 0, 0, 0, 0, },
550+
/* j */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, },
551+
/* k */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
552+
/* l */ { 0, 9, 11, 15, 16, 20, 23, 28, 0, 0, 0, 30, 33, 36, 40, 46, 0, 48, 57, 49, 50, 54, 56, 0, 0, 0, },
553+
/* m */ { 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
554+
/* n */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, },
555+
/* o */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 5, 0, 0, 0, 0, 0, 0, 9, 0, 11, 0, 0, 0, },
556+
/* p */ { 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 0, 0, 16, 17, 26, 0, 27, 0, 28, 0, },
557+
/* q */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
558+
/* r */ { 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 18, 0, 0, 0, 0, },
559+
/* s */ { 0, 6, 15, 0, 18, 22, 0, 24, 25, 0, 0, 28, 30, 34, 38, 40, 0, 48, 0, 49, 0, 61, 62, 0, 63, 0, },
560+
/* t */ { 0, 0, 19, 0, 22, 23, 0, 24, 0, 25, 0, 26, 27, 28, 29, 30, 0, 31, 33, 0, 34, 0, 0, 0, 0, 0, },
561+
/* u */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
562+
/* v */ { 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 12, 0, 0, 0, 0, 15, 0, 16, 0, 0, 0, 0, 0, },
563+
/* w */ { 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 8, 0, 9, 10, 0, 12, 0, 13, 14, 0, 0, 0, 0, },
564+
/* x */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, },
565+
/* y */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
566+
/* z */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
530567
};
531568

569+
static int command_count = 539;
570+
571+
/* End of automatically generated code by create_cmdidxs.pl */
572+
532573
static char_u dollar_command[2] = {'$', 0};
533574

534575

@@ -614,7 +655,6 @@ restore_dbg_stuff(struct dbg_stuff *dsp)
614655
}
615656
#endif
616657

617-
618658
/*
619659
* do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620660
* command is given.
@@ -3208,10 +3248,24 @@ find_command(exarg_T *eap, int *full UNUSED)
32083248
}
32093249
}
32103250

3211-
if (ASCII_ISLOWER(*eap->cmd))
3212-
eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3251+
if (ASCII_ISLOWER(eap->cmd[0]))
3252+
{
3253+
if (command_count != (int)CMD_SIZE)
3254+
{
3255+
iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3256+
getout(1);
3257+
}
3258+
3259+
/* Use a precomputed index for fast look-up in cmdnames[]
3260+
* taking into account the first 2 letters of eap->cmd. */
3261+
int c1 = eap->cmd[0];
3262+
int c2 = eap->cmd[1];
3263+
eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3264+
if (ASCII_ISLOWER(c2))
3265+
eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3266+
}
32133267
else
3214-
eap->cmdidx = cmdidxs[26];
3268+
eap->cmdidx = CMD_bang;
32153269

32163270
for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
32173271
eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
504,
767769
/**/
768770
503,
769771
/**/

0 commit comments

Comments
 (0)