Decrease startup-time by avoiding iteration over maps
This commit makes adding, removing and finding key maps an O(1) operation instead of O(n), where n is the number of pre-existing maps. In my testing, averaged over 100 iterations, this reduces the time spent initializing NERDTree at Vim startup from ~73ms to ~9.7ms. That's with only the default included key maps.
This commit is contained in:
parent
9af083a6d1
commit
c6d757f198
@ -3,18 +3,23 @@
|
|||||||
let s:KeyMap = {}
|
let s:KeyMap = {}
|
||||||
let g:NERDTreeKeyMap = s:KeyMap
|
let g:NERDTreeKeyMap = s:KeyMap
|
||||||
|
|
||||||
"FUNCTION: KeyMap.All() {{{1
|
"FUNCTION: KeyMap.all() {{{1
|
||||||
function! s:KeyMap.All()
|
function! s:KeyMap.all()
|
||||||
|
if !exists("s:keyMaps")
|
||||||
if !exists('s:keyMaps')
|
let s:keyMaps = {}
|
||||||
let s:keyMaps = []
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
call sort(s:keyMaps, s:KeyMap.Compare, s:KeyMap)
|
|
||||||
|
|
||||||
return s:keyMaps
|
return s:keyMaps
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
"FUNCTION: KeyMap.All() {{{1
|
||||||
|
function! s:KeyMap.All()
|
||||||
|
let sortedKeyMaps = values(s:KeyMap.all())
|
||||||
|
call sort(sortedKeyMaps, s:KeyMap.Compare, s:KeyMap)
|
||||||
|
|
||||||
|
return sortedKeyMaps
|
||||||
|
endfunction
|
||||||
|
|
||||||
"FUNCTION: KeyMap.Compare(keyMap1, keyMap2) {{{1
|
"FUNCTION: KeyMap.Compare(keyMap1, keyMap2) {{{1
|
||||||
function! s:KeyMap.Compare(keyMap1, keyMap2)
|
function! s:KeyMap.Compare(keyMap1, keyMap2)
|
||||||
|
|
||||||
@ -31,17 +36,12 @@ endfunction
|
|||||||
|
|
||||||
"FUNCTION: KeyMap.FindFor(key, scope) {{{1
|
"FUNCTION: KeyMap.FindFor(key, scope) {{{1
|
||||||
function! s:KeyMap.FindFor(key, scope)
|
function! s:KeyMap.FindFor(key, scope)
|
||||||
for i in s:KeyMap.All()
|
return get(s:KeyMap.all(), a:key . a:scope, {})
|
||||||
if i.key ==# a:key && i.scope ==# a:scope
|
|
||||||
return i
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
return {}
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
"FUNCTION: KeyMap.BindAll() {{{1
|
"FUNCTION: KeyMap.BindAll() {{{1
|
||||||
function! s:KeyMap.BindAll()
|
function! s:KeyMap.BindAll()
|
||||||
for i in s:KeyMap.All()
|
for i in values(s:KeyMap.all())
|
||||||
call i.bind()
|
call i.bind()
|
||||||
endfor
|
endfor
|
||||||
endfunction
|
endfunction
|
||||||
@ -67,12 +67,7 @@ endfunction
|
|||||||
|
|
||||||
"FUNCTION: KeyMap.Remove(key, scope) {{{1
|
"FUNCTION: KeyMap.Remove(key, scope) {{{1
|
||||||
function! s:KeyMap.Remove(key, scope)
|
function! s:KeyMap.Remove(key, scope)
|
||||||
let maps = s:KeyMap.All()
|
return remove(s:keyMaps, a:key . a:scope)
|
||||||
for i in range(len(maps))
|
|
||||||
if maps[i].key ==# a:key && maps[i].scope ==# a:scope
|
|
||||||
return remove(maps, i)
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
"FUNCTION: KeyMap.invoke() {{{1
|
"FUNCTION: KeyMap.invoke() {{{1
|
||||||
@ -170,8 +165,7 @@ endfunction
|
|||||||
|
|
||||||
"FUNCTION: KeyMap.Add(keymap) {{{1
|
"FUNCTION: KeyMap.Add(keymap) {{{1
|
||||||
function! s:KeyMap.Add(keymap)
|
function! s:KeyMap.Add(keymap)
|
||||||
call s:KeyMap.Remove(a:keymap.key, a:keymap.scope)
|
let s:keyMaps[a:keymap.key . a:keymap.scope] = a:keymap
|
||||||
call add(s:KeyMap.All(), a:keymap)
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" vim: set sw=4 sts=4 et fdm=marker:
|
" vim: set sw=4 sts=4 et fdm=marker:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user