Skip to content

Commit

Permalink
cache location of session file
Browse files Browse the repository at this point in the history
Function session_file() returns the name of session file.
This function is called for every change in VIM layout.

Caching of return variable of this function gives x100 performance
improvement.

Before change:
FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    6   3.369383   0.015592  g:GitSessionUpdate()
    6   3.353791   0.000861  <SNR>35_session_file()
    6   3.268404   0.001057  <SNR>35_session_dir()
   12   3.263580   0.004735  <SNR>35_in_git_repo()

After this change:
FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
   11   0.026195             g:GitSessionUpdate()
   23   0.025646             <SNR>38_Highlight_Matching_Pair()
   11   0.000956             nerdtree#checkForBrowse()

VIM profiling (http://stackoverflow.com/a/12216578):
        :profile start profile.log
        :profile func *
        :profile file *
        " At this point do slow actions
        :profile pause
        :noautocmd qall!

Signed-off-by: Leon Romanovsky <leon@leon.nu>
  • Loading branch information
rleon committed Jul 24, 2015
1 parent 495bf7d commit b8385a3
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions plugin/gitsessions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ if !exists('s:session_exist')
let s:session_exist = 0
endif

" Cache session file,
" Pros: performance gain (x100) on large repositories
" Cons: switch between git branches will be missed from GitSessionUpdate()
" You are advised to save it manually by calling to GitSessionSave()
" Default - cache disabled
let g:cache_session_file = 1

" HELPER FUNCTIONS

function! s:replace_bad_chars(string)
Expand Down Expand Up @@ -102,17 +109,24 @@ function! s:session_dir()
endif
endfunction

function! s:session_file()
function! s:session_file(invalidate_cache)
if g:cache_session_file && !a:invalidate_cache && exists('s:cached_session_file')
return s:cached_session_file
endif
let l:dir = s:session_dir()
let l:branch = s:git_branch_name()
return (empty(l:branch)) ? l:dir . '/master' : l:dir . '/' . l:branch
if exists('s:cached_session_file')
unlet s:cached_session_file
endif
let s:cached_session_file = (empty(l:branch)) ? l:dir . '/master' : l:dir . '/' . l:branch
return s:cached_session_file
endfunction

" PUBLIC FUNCTIONS

function! g:GitSessionSave()
let l:dir = s:session_dir()
let l:file = s:session_file()
let l:file = s:session_file(1)

if !isdirectory(l:dir)
call mkdir(l:dir, 'p')
Expand Down Expand Up @@ -141,7 +155,7 @@ endfunction

function! g:GitSessionUpdate(...)
let l:show_msg = a:0 > 0 ? a:1 : 1
let l:file = s:session_file()
let l:file = s:session_file(0)

if s:session_exist && filereadable(l:file)
execute 'mksession!' l:file
Expand All @@ -157,7 +171,7 @@ function! g:GitSessionLoad(...)
endif

let l:show_msg = a:0 > 0 ? a:1 : 0
let l:file = s:session_file()
let l:file = s:session_file(1)

if filereadable(l:file)
let s:session_exist = 1
Expand All @@ -170,8 +184,14 @@ function! g:GitSessionLoad(...)
endfunction

function! g:GitSessionDelete()
let l:file = s:session_file()
" Delete is a tricky case, we still need to use cached version if any.
" This version was used and saved by GitSessionUpdate(), however
" we should ensure that session cached variable is cleared.
let l:file = s:session_file(1)
let s:session_exist = 0
if exists('s:cached_session_file')
unlet s:cached_session_file
endif
if filereadable(l:file)
call delete(l:file)
echom "session deleted:" l:file
Expand Down

0 comments on commit b8385a3

Please sign in to comment.