home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / patches / 7.3 / 7.3.1103 < prev    next >
Encoding:
Internet Message Format  |  2013-06-01  |  11.0 KB

  1. To: vim_dev@googlegroups.com
  2. Subject: Patch 7.3.1103
  3. Fcc: outbox
  4. From: Bram Moolenaar <Bram@moolenaar.net>
  5. Mime-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. ------------
  9.  
  10. Patch 7.3.1103
  11. Problem:    New regexp engine: overhead in saving and restoring.
  12. Solution:   Make saving and restoring list IDs faster.  Don't copy or check \z
  13.         subexpressions when they are not used.
  14. Files:        src/regexp_nfa.c
  15.  
  16.  
  17. *** ../vim-7.3.1102/src/regexp_nfa.c    2013-06-02 16:40:44.000000000 +0200
  18. --- src/regexp_nfa.c    2013-06-02 21:00:41.000000000 +0200
  19. ***************
  20. *** 237,242 ****
  21. --- 237,245 ----
  22.   /* NFA regexp \1 .. \9 encountered. */
  23.   static int nfa_has_backref;
  24.   
  25. + /* NFA regexp has \z( ), set zsubexpr. */
  26. + static int nfa_has_zsubexpr;
  27.   /* Number of sub expressions actually being used during execution. 1 if only
  28.    * the whole match (subexpr 0) is used. */
  29.   static int nfa_nsubexpr;
  30. ***************
  31. *** 272,281 ****
  32.   static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
  33.   static int check_char_class __ARGS((int class, int c));
  34.   static void st_error __ARGS((int *postfix, int *end, int *p));
  35. ! static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
  36. ! static void nfa_set_null_listids __ARGS((nfa_state_T *start));
  37. ! static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
  38. ! static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
  39.   static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
  40.   static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
  41.   static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
  42. --- 275,282 ----
  43.   static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
  44.   static int check_char_class __ARGS((int class, int c));
  45.   static void st_error __ARGS((int *postfix, int *end, int *p));
  46. ! static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
  47. ! static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
  48.   static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
  49.   static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
  50.   static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
  51. ***************
  52. *** 3000,3005 ****
  53. --- 3001,3024 ----
  54.       return TRUE;
  55.   }
  56.   
  57. + #ifdef ENABLE_LOG
  58. +     static void
  59. + report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid);
  60. + {
  61. +     int col;
  62. +     if (sub->in_use <= 0)
  63. +     col = -1;
  64. +     else if (REG_MULTI)
  65. +     col = sub->list.multi[0].start.col;
  66. +     else
  67. +     col = (int)(sub->list.line[0].start - regline);
  68. +     nfa_set_code(state->c);
  69. +     fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
  70. +         action, abs(state->id), lid, state->c, code, col);
  71. + }
  72. + #endif
  73.       static void
  74.   addstate(l, state, subs, off)
  75.       nfa_list_T        *l;    /* runtime state list */
  76. ***************
  77. *** 3118,3124 ****
  78.               if (thread->state->id == state->id
  79.                   && sub_equal(&thread->subs.norm, &subs->norm)
  80.   #ifdef FEAT_SYN_HL
  81. !                 && sub_equal(&thread->subs.synt, &subs->synt)
  82.   #endif
  83.                         )
  84.               goto skip_add;
  85. --- 3137,3144 ----
  86.               if (thread->state->id == state->id
  87.                   && sub_equal(&thread->subs.norm, &subs->norm)
  88.   #ifdef FEAT_SYN_HL
  89. !                 && (!nfa_has_zsubexpr ||
  90. !                    sub_equal(&thread->subs.synt, &subs->synt))
  91.   #endif
  92.                         )
  93.               goto skip_add;
  94. ***************
  95. *** 3141,3181 ****
  96.           thread->state = state;
  97.           copy_sub(&thread->subs.norm, &subs->norm);
  98.   #ifdef FEAT_SYN_HL
  99. !         copy_sub(&thread->subs.synt, &subs->synt);
  100.   #endif
  101.   #ifdef ENABLE_LOG
  102. !         {
  103. !         int col;
  104. !         if (thread->subs.norm.in_use <= 0)
  105. !             col = -1;
  106. !         else if (REG_MULTI)
  107. !             col = thread->subs.norm.list.multi[0].start.col;
  108. !         else
  109. !             col = (int)(thread->subs.norm.list.line[0].start - regline);
  110. !         nfa_set_code(state->c);
  111. !         fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
  112. !                 abs(state->id), l->id, state->c, code, col);
  113. !         did_print = TRUE;
  114. !         }
  115.   #endif
  116.       }
  117.   
  118.   #ifdef ENABLE_LOG
  119.       if (!did_print)
  120. !     {
  121. !     int col;
  122. !     if (subs->norm.in_use <= 0)
  123. !         col = -1;
  124. !     else if (REG_MULTI)
  125. !         col = subs->norm.list.multi[0].start.col;
  126. !     else
  127. !         col = (int)(subs->norm.list.line[0].start - regline);
  128. !     nfa_set_code(state->c);
  129. !     fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
  130. !         abs(state->id), l->id, state->c, code, col);
  131. !     }
  132.   #endif
  133.       switch (state->c)
  134.       {
  135. --- 3161,3178 ----
  136.           thread->state = state;
  137.           copy_sub(&thread->subs.norm, &subs->norm);
  138.   #ifdef FEAT_SYN_HL
  139. !         if (nfa_has_zsubexpr)
  140. !         copy_sub(&thread->subs.synt, &subs->synt);
  141.   #endif
  142.   #ifdef ENABLE_LOG
  143. !         report_state("Adding", &thread->subs.norm, state, l->id);
  144. !         did_print = TRUE;
  145.   #endif
  146.       }
  147.   
  148.   #ifdef ENABLE_LOG
  149.       if (!did_print)
  150. !     report_state("Processing", &subs->norm, state, l->id);
  151.   #endif
  152.       switch (state->c)
  153.       {
  154. ***************
  155. *** 3600,3648 ****
  156.   #endif
  157.   
  158.   /*
  159. !  * Set all NFA nodes' list ID equal to -1.
  160.    */
  161.       static void
  162. ! nfa_set_neg_listids(start)
  163. !     nfa_state_T        *start;
  164. ! {
  165. !     if (start != NULL && start->lastlist >= 0)
  166. !     {
  167. !     start->lastlist = -1;
  168. !     nfa_set_neg_listids(start->out);
  169. !     nfa_set_neg_listids(start->out1);
  170. !     }
  171. ! }
  172. ! /*
  173. !  * Set all NFA nodes' list ID equal to 0.
  174. !  */
  175. !     static void
  176. ! nfa_set_null_listids(start)
  177. !     nfa_state_T        *start;
  178. ! {
  179. !     if (start != NULL && start->lastlist == -1)
  180. !     {
  181. !     start->lastlist = 0;
  182. !     nfa_set_null_listids(start->out);
  183. !     nfa_set_null_listids(start->out1);
  184. !     }
  185. ! }
  186. ! /*
  187. !  * Save list IDs for all NFA states in "list".
  188. !  */
  189. !     static void
  190. ! nfa_save_listids(start, list)
  191. !     nfa_state_T        *start;
  192.       int            *list;
  193.   {
  194. !     if (start != NULL && start->lastlist != -1)
  195. !     {
  196. !     list[abs(start->id)] = start->lastlist;
  197. !     start->lastlist = -1;
  198. !     nfa_save_listids(start->out, list);
  199. !     nfa_save_listids(start->out1, list);
  200.       }
  201.   }
  202.   
  203. --- 3597,3620 ----
  204.   #endif
  205.   
  206.   /*
  207. !  * Save list IDs for all NFA states of "prog" into "list".
  208. !  * Also reset the IDs to zero.
  209.    */
  210.       static void
  211. ! nfa_save_listids(prog, list)
  212. !     nfa_regprog_T   *prog;
  213.       int            *list;
  214.   {
  215. !     int            i;
  216. !     nfa_state_T        *p;
  217. !     /* Order in the list is reverse, it's a bit faster that way. */
  218. !     p = &prog->state[0];
  219. !     for (i = prog->nstate; --i >= 0; )
  220. !     {
  221. !     list[i] = p->lastlist;
  222. !     p->lastlist = 0;
  223. !     ++p;
  224.       }
  225.   }
  226.   
  227. ***************
  228. *** 3650,3664 ****
  229.    * Restore list IDs from "list" to all NFA states.
  230.    */
  231.       static void
  232. ! nfa_restore_listids(start, list)
  233. !     nfa_state_T        *start;
  234.       int            *list;
  235.   {
  236. !     if (start != NULL && start->lastlist == -1)
  237.       {
  238. !     start->lastlist = list[abs(start->id)];
  239. !     nfa_restore_listids(start->out, list);
  240. !     nfa_restore_listids(start->out1, list);
  241.       }
  242.   }
  243.   
  244. --- 3622,3639 ----
  245.    * Restore list IDs from "list" to all NFA states.
  246.    */
  247.       static void
  248. ! nfa_restore_listids(prog, list)
  249. !     nfa_regprog_T   *prog;
  250.       int            *list;
  251.   {
  252. !     int            i;
  253. !     nfa_state_T        *p;
  254. !     p = &prog->state[0];
  255. !     for (i = prog->nstate; --i >= 0; )
  256.       {
  257. !     p->lastlist = list[i];
  258. !     ++p;
  259.       }
  260.   }
  261.   
  262. ***************
  263. *** 3673,3679 ****
  264.       return val == pos;
  265.   }
  266.   
  267. ! static int nfa_regmatch __ARGS((nfa_state_T *start, regsubs_T *submatch, regsubs_T *m));
  268.   
  269.   /*
  270.    * Main matching routine.
  271. --- 3648,3654 ----
  272.       return val == pos;
  273.   }
  274.   
  275. ! static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m));
  276.   
  277.   /*
  278.    * Main matching routine.
  279. ***************
  280. *** 3686,3692 ****
  281.    * Note: Caller must ensure that: start != NULL.
  282.    */
  283.       static int
  284. ! nfa_regmatch(start, submatch, m)
  285.       nfa_state_T        *start;
  286.       regsubs_T        *submatch;
  287.       regsubs_T        *m;
  288. --- 3661,3668 ----
  289.    * Note: Caller must ensure that: start != NULL.
  290.    */
  291.       static int
  292. ! nfa_regmatch(prog, start, submatch, m)
  293. !     nfa_regprog_T    *prog;
  294.       nfa_state_T        *start;
  295.       regsubs_T        *submatch;
  296.       regsubs_T        *m;
  297. ***************
  298. *** 3872,3878 ****
  299.           nfa_match = TRUE;
  300.           copy_sub(&submatch->norm, &t->subs.norm);
  301.   #ifdef FEAT_SYN_HL
  302. !         copy_sub(&submatch->synt, &t->subs.synt);
  303.   #endif
  304.   #ifdef ENABLE_LOG
  305.           log_subsexpr(&t->subs);
  306. --- 3848,3855 ----
  307.           nfa_match = TRUE;
  308.           copy_sub(&submatch->norm, &t->subs.norm);
  309.   #ifdef FEAT_SYN_HL
  310. !         if (nfa_has_zsubexpr)
  311. !             copy_sub(&submatch->synt, &t->subs.synt);
  312.   #endif
  313.   #ifdef ENABLE_LOG
  314.           log_subsexpr(&t->subs);
  315. ***************
  316. *** 3928,3934 ****
  317.               {
  318.               copy_sub(&m->norm, &t->subs.norm);
  319.   #ifdef FEAT_SYN_HL
  320. !             copy_sub(&m->synt, &t->subs.synt);
  321.   #endif
  322.               }
  323.               nfa_match = TRUE;
  324. --- 3905,3912 ----
  325.               {
  326.               copy_sub(&m->norm, &t->subs.norm);
  327.   #ifdef FEAT_SYN_HL
  328. !             if (nfa_has_zsubexpr)
  329. !                 copy_sub(&m->synt, &t->subs.synt);
  330.   #endif
  331.               }
  332.               nfa_match = TRUE;
  333. ***************
  334. *** 4024,4035 ****
  335.           /* Have to clear the listid field of the NFA nodes, so that
  336.            * nfa_regmatch() and addstate() can run properly after
  337.            * recursion. */
  338. !         nfa_save_listids(start, listids);
  339. !         nfa_set_null_listids(start);
  340.           nfa_endp = endposp;
  341. !         result = nfa_regmatch(t->state->out, submatch, m);
  342. !         nfa_set_neg_listids(start);
  343. !         nfa_restore_listids(start, listids);
  344.   
  345.           /* restore position in input text */
  346.           reginput = save_reginput;
  347. --- 4002,4011 ----
  348.           /* Have to clear the listid field of the NFA nodes, so that
  349.            * nfa_regmatch() and addstate() can run properly after
  350.            * recursion. */
  351. !         nfa_save_listids(prog, listids);
  352.           nfa_endp = endposp;
  353. !         result = nfa_regmatch(prog, t->state->out, submatch, m);
  354. !         nfa_restore_listids(prog, listids);
  355.   
  356.           /* restore position in input text */
  357.           reginput = save_reginput;
  358. ***************
  359. *** 4665,4671 ****
  360. --- 4641,4652 ----
  361.   #ifdef FEAT_SYN_HL
  362.       /* Clear the external match subpointers if necessary. */
  363.       if (prog->reghasz == REX_SET)
  364. +     {
  365. +     nfa_has_zsubexpr = TRUE;
  366.       need_clear_zsubexpr = TRUE;
  367. +     }
  368. +     else
  369. +     nfa_has_zsubexpr = FALSE;
  370.   #endif
  371.   
  372.   #ifdef ENABLE_LOG
  373. ***************
  374. *** 4694,4700 ****
  375.       clear_sub(&m.synt);
  376.   #endif
  377.   
  378. !     if (nfa_regmatch(start, &subs, &m) == FALSE)
  379.       return 0;
  380.   
  381.       cleanup_subexpr();
  382. --- 4675,4681 ----
  383.       clear_sub(&m.synt);
  384.   #endif
  385.   
  386. !     if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
  387.       return 0;
  388.   
  389.       cleanup_subexpr();
  390. *** ../vim-7.3.1102/src/version.c    2013-06-02 19:22:05.000000000 +0200
  391. --- src/version.c    2013-06-02 21:24:50.000000000 +0200
  392. ***************
  393. *** 730,731 ****
  394. --- 730,733 ----
  395.   {   /* Add new patch number below this line */
  396. + /**/
  397. +     1103,
  398.   /**/
  399.  
  400. -- 
  401. hundred-and-one symptoms of being an internet addict:
  402. 53. To find out what time it is, you send yourself an e-mail and check the
  403.     "Date:" field.
  404.  
  405.  /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
  406. ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
  407. \\\  an exciting new programming language -- http://www.Zimbu.org        ///
  408.  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
  409.