To: vim_dev@googlegroups.com Subject: Patch 7.4.2036 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2036 Problem: Looking up a buffer by number is slow if there are many. Solution: Use a hashtab. Files: src/structs.h, src/buffer.c *** ../vim-7.4.2035/src/structs.h 2016-07-10 22:11:11.858751521 +0200 --- src/structs.h 2016-07-14 21:37:09.432147507 +0200 *************** *** 1753,1758 **** --- 1753,1761 ---- unsigned int b_fab_mrs; /* Max record size */ #endif int b_fnum; /* buffer number for this file. */ + char_u b_key[VIM_SIZEOF_INT * 2 + 1]; + /* key used for buf_hashtab, holds b_fnum as + hex string */ int b_changed; /* 'modified': Set to TRUE if something in the file has been changed and not written out. */ *** ../vim-7.4.2035/src/buffer.c 2016-07-10 22:11:11.854751580 +0200 --- src/buffer.c 2016-07-14 22:03:42.204827897 +0200 *************** *** 350,355 **** --- 350,377 ---- } /* + * A hash table used to quickly lookup a buffer by its number. + */ + static hashtab_T buf_hashtab; + + static void + buf_hashtab_add(buf_T *buf) + { + sprintf((char *)buf->b_key, "%x", buf->b_fnum); + if (hash_add(&buf_hashtab, buf->b_key) == FAIL) + EMSG(_("E931: Buffer cannot be registered")); + } + + static void + buf_hashtab_remove(buf_T *buf) + { + hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key); + + if (!HASHITEM_EMPTY(hi)) + hash_remove(&buf_hashtab, hi); + } + + /* * Close the link to a buffer. * "action" is used when there is no longer a window for the buffer. * It can be: *************** *** 723,728 **** --- 745,753 ---- #endif #ifdef FEAT_AUTOCMD aubuflocal_remove(buf); + + buf_hashtab_remove(buf); + if (autocmd_busy) { /* Do not free the buffer structure while autocommands are executing, *************** *** 1703,1708 **** --- 1728,1735 ---- * functions for dealing with the buffer list */ + static int top_file_num = 1; /* highest file number */ + /* * Add a file name to the buffer list. Return a pointer to the buffer. * If the same file name already exists return a pointer to that buffer. *************** *** 1715,1722 **** * if the buffer already exists. * This is the ONLY way to create a new buffer. */ - static int top_file_num = 1; /* highest file number */ - buf_T * buflist_new( char_u *ffname, /* full path of fname or relative */ --- 1742,1747 ---- *************** *** 1729,1734 **** --- 1754,1762 ---- stat_T st; #endif + if (top_file_num == 1) + hash_init(&buf_hashtab); + fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */ /* *************** *** 1907,1912 **** --- 1935,1941 ---- } top_file_num = 1; } + buf_hashtab_add(buf); /* * Always copy the options from the current buffer. *************** *** 2579,2597 **** #endif /* ! * find file in buffer list by number */ buf_T * buflist_findnr(int nr) { ! buf_T *buf; if (nr == 0) nr = curwin->w_alt_fnum; ! /* Assume newer buffers are used more often, start from the end. */ ! for (buf = lastbuf; buf != NULL; buf = buf->b_prev) ! if (buf->b_fnum == nr) ! return buf; return NULL; } --- 2608,2629 ---- #endif /* ! * Find a file in the buffer list by buffer number. */ buf_T * buflist_findnr(int nr) { ! char_u key[VIM_SIZEOF_INT * 2 + 1]; ! hashitem_T *hi; if (nr == 0) nr = curwin->w_alt_fnum; ! sprintf((char *)key, "%x", nr); ! hi = hash_find(&buf_hashtab, key); ! ! if (!HASHITEM_EMPTY(hi)) ! return (buf_T *)(hi->hi_key ! - ((unsigned)(curbuf->b_key - (char_u *)curbuf))); return NULL; } *** ../vim-7.4.2035/src/version.c 2016-07-14 20:37:03.884826159 +0200 --- src/version.c 2016-07-14 22:06:04.862738847 +0200 *************** *** 760,761 **** --- 760,763 ---- { /* Add new patch number below this line */ + /**/ + 2036, /**/ -- GALAHAD: No. Look, I can tackle this lot single-handed! GIRLS: Yes, yes, let him Tackle us single-handed! "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///