buffer.c: use a map instead of hashtab

This commit is contained in:
rover 2017-01-04 22:21:10 +08:00 committed by lonerover
parent 1984e784fb
commit d204cbc0ce
2 changed files with 12 additions and 20 deletions

View File

@ -279,23 +279,24 @@ bool buf_valid(buf_T *buf)
return false; return false;
} }
/// A hash table used to quickly lookup a buffer by its number. // Map used to quickly lookup a buffer by its number.
static hashtab_T buf_hashtab; static PMap(handle_T) *buf_map = NULL;
static void buf_hashtab_add(buf_T *buf) static void buf_hashtab_add(buf_T *buf)
FUNC_ATTR_NONNULL_ALL
{ {
snprintf((char *)buf->b_key, sizeof(buf->b_key), "%x", buf->b_fnum); if (pmap_has(handle_T)(buf_map, buf->handle)) {
if (hash_add(&buf_hashtab, buf->b_key) == FAIL) {
EMSG(_("E931: Buffer cannot be registered")); EMSG(_("E931: Buffer cannot be registered"));
} else {
pmap_put(handle_T)(buf_map, buf->handle, buf);
} }
} }
static void buf_hashtab_remove(buf_T *buf) static void buf_hashtab_remove(buf_T *buf)
FUNC_ATTR_NONNULL_ALL
{ {
hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key); if (pmap_has(handle_T)(buf_map, buf->handle)) {
pmap_del(handle_T)(buf_map, buf->handle);
if (!HASHITEM_EMPTY(hi)) {
hash_remove(&buf_hashtab, hi);
} }
} }
@ -1386,7 +1387,7 @@ buf_T * buflist_new(char_u *ffname, char_u *sfname, linenr_T lnum, int flags)
buf_T *buf; buf_T *buf;
if (top_file_num == 1) { if (top_file_num == 1) {
hash_init(&buf_hashtab); buf_map = pmap_new(handle_T)();
} }
fname_expand(curbuf, &ffname, &sfname); // will allocate ffname fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
@ -2025,19 +2026,12 @@ static char_u *fname_match(regmatch_T *rmp, char_u *name, bool ignore_case)
/// Find a file in the buffer list by buffer number. /// Find a file in the buffer list by buffer number.
buf_T *buflist_findnr(int nr) buf_T *buflist_findnr(int nr)
{ {
char_u key[sizeof(handle_T) * 2 + 1];
hashitem_T *hi;
if (nr == 0) { if (nr == 0) {
nr = curwin->w_alt_fnum; nr = curwin->w_alt_fnum;
} }
snprintf((char *)key, sizeof(key), "%x", nr); if (pmap_has(handle_T)(buf_map, (handle_T)nr)) {
hi = hash_find(&buf_hashtab, key); return pmap_get(handle_T)(buf_map, (handle_T)nr);
if (!HASHITEM_EMPTY(hi)) {
return (buf_T *)(hi->hi_key -
((unsigned)(curbuf->b_key - (char_u *)curbuf)));
} }
return NULL; return NULL;
} }

View File

@ -490,8 +490,6 @@ struct file_buffer {
bool file_id_valid; bool file_id_valid;
FileID file_id; FileID file_id;
char_u b_key[sizeof(handle_T) * 2 + 1]; // key used for buf_hashtab, holds
// b_fnum as hex string
int b_changed; // 'modified': Set to true if something in the int b_changed; // 'modified': Set to true if something in the
// file has been changed and not written out. // file has been changed and not written out.
int b_changedtick; // incremented for each change, also for undo int b_changedtick; // incremented for each change, also for undo