Speed up sortChildren() by using sorting token

This improves the sorting functions from 12 seconds to 0.66 seconds for
~4000 objects
This commit is contained in:
Vincent Tsang 2015-05-09 22:38:16 +08:00
parent a87b1bf3c5
commit 1f2531ec3a
2 changed files with 30 additions and 1 deletions

View File

@ -34,6 +34,18 @@ function! nerdtree#compareNodes(n1, n2)
return a:n1.path.compareTo(a:n2.path) return a:n1.path.compareTo(a:n2.path)
endfunction endfunction
"FUNCTION: nerdtree#compareNodesBySortingToken(n1, n2) {{{2
function! nerdtree#compareNodesBySortingToken(n1, n2)
return a:n1.path.compareTo(a:n2.path)
if a:n1.sorting_token < a:n2.sorting_token
return -1
elseif a:n1.sorting_token > a:n2.sorting_token
return 1
else
return 0
endif
endfunction
" FUNCTION: nerdtree#deprecated(func, [msg]) {{{2 " FUNCTION: nerdtree#deprecated(func, [msg]) {{{2
" Issue a deprecation warning for a:func. If a second arg is given, use this " Issue a deprecation warning for a:func. If a second arg is given, use this
" as the deprecation message " as the deprecation message

View File

@ -504,7 +504,24 @@ endfunction
"directory priority. "directory priority.
" "
function! s:TreeDirNode.sortChildren() function! s:TreeDirNode.sortChildren()
let CompareFunc = function("nerdtree#compareNodes") let CompareFunc = function("nerdtree#compareNodesBySortingToken")
" To optimize sorting, let's generate the sorting token for comparison
" calculate how large number is needed to represent " order index
let digit = ceil(log10(len(g:NERDTreeSortOrder)))
let format = "%0" . float2nr(digit) . "d" " e.g. '%04d'
for child in self.children
let path = child.path.getLastPathComponent(1)
if !g:NERDTreeSortHiddenFirst
let path = substitute(path, '^[._]', '', '')
endif
if !g:NERDTreeCaseSensitiveSort
let path = tolower(path)
endif
let child.sorting_token = printf(format, child.path.getSortOrderIndex()) . path
endfor
call sort(self.children, CompareFunc) call sort(self.children, CompareFunc)
endfunction endfunction