annotate src/create_cmdidxs.pl @ 11236:62c96fee518e v8.0.0504

patch 8.0.0504: looking up an Ex command is a bit slow commit https://github.com/vim/vim/commit/e5e0fbcd4244d032a0635ad7defe2831f251c639 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Mar 25 14:51:01 2017 +0100 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)
author Christian Brabandt <cb@256bit.org>
date Sat, 25 Mar 2017 15:00:05 +0100
parents
children 17ba19406c50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11236
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 #!/usr/bin/perl -w
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 #
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 # This script generates the tables cmdidxs1[] and cmdidxs2[][] which,
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 # given a Ex command, determine the first value to probe to find
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 # a matching command in cmdnames[] based on the first character
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 # and the first 2 characters of the command.
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 # This is used to speed up lookup in cmdnames[].
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 #
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 # Script should be run every time new Ex commands are added in Vim,
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 # from the src/vim directory, since it reads commands from "ex_cmds.h".
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 # Find the list of Vim commands from cmdnames[] table in ex_cmds.h
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 my @cmds;
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 my @skipped;
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 open(IN, "< ex_cmds.h") or die "can't open ex_cmds.h: $!\n";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 while (<IN>) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 if (/^EX\(CMD_\S*,\s*"([a-z][^"]*)"/) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 push (@cmds, $1);
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 } elsif (/^EX\(CMD_/) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 push (@skipped, $1);
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 my %cmdidxs1;
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 my %cmdidxs2;
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 for (my $i = $#cmds; $i >= 0; --$i) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 my $cmd = $cmds[$i];
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 my $c1 = substr($cmd, 0, 1); # First character of command.
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 $cmdidxs1{$c1} = $i;
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 if (length($cmd) > 1) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 my $c2 = substr($cmd, 1, 1); # Second character of command.
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 $cmdidxs2{$c1}{$c2} = $i if (('a' lt $c2) and ($c2 lt 'z'));
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 print "/* Beginning of automatically generated code by create_cmdidxs.pl\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 " *\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 " * Table giving the index of the first command in cmdnames[] to lookup\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 " * based on the first letter of a command.\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 " */\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 "static const unsigned short cmdidxs1[26] =\n{\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 join(",\n", map(" /* $_ */ $cmdidxs1{$_}", ('a' .. 'z'))),
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 "\n};\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 "\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 "/*\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 " * Table giving the index of the first command in cmdnames[] to lookup\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 " * based on the first 2 letters of a command.\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 " * Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 " * fit in a byte.\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53 " */\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 "static const unsigned char cmdidxs2[26][26] =\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
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";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 for my $c1 ('a' .. 'z') {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 print " /* $c1 */ {";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58 for my $c2 ('a' .. 'z') {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59 if (exists $cmdidxs2{$c1}{$c2}) {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 printf "%3d,", $cmdidxs2{$c1}{$c2} - $cmdidxs1{$c1};
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 } else {
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 printf " 0,";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
63 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65 print " }";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66 print "," unless ($c1 eq 'z');
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 print "\n";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 }
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 print "};\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 "\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71 "static int command_count = ", $#cmds + $#skipped + 2 , ";\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72 "\n",
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 "/* End of automatically generated code by create_cmdidxs.pl */\n";
62c96fee518e patch 8.0.0504: looking up an Ex command is a bit slow
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74