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 57d5bd7731
2 changed files with 29 additions and 1 deletions

View File

@@ -504,7 +504,24 @@ endfunction
"directory priority.
"
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)
endfunction