Skip to content

Commit

Permalink
perf: delay cache read until it is necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Oct 13, 2022
1 parent fa3431d commit ac78b0c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions autoload/wiki/cache.vim
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ function! s:cache.init(path, opts) dict abort " {{{1
let new.path = a:path
let new.ftime = -1
let new.default = a:opts.default
let new.__validated = 0
let new.__validation_value = deepcopy(a:opts.validate)

if a:opts.persistent
call extend(new, s:cache_persistent)
call new.validate(a:opts.validate)
return new
return extend(new, s:cache_persistent)
endif

return extend(new, s:cache_volatile)
Expand All @@ -163,14 +163,13 @@ let s:cache_persistent = {
\ 'type': 'persistent',
\ 'modified': 0,
\}
function! s:cache_persistent.validate(value) dict abort " {{{1
let self.__validation_value = deepcopy(a:value)
function! s:cache_persistent.validate() dict abort " {{{1
let self.__validated = 1

if type(self.__validation_value) == v:t_dict
let self.__validation_value._version = s:_version
endif

call self.read()

if empty(self.data)
let self.data.__validate = deepcopy(self.__validation_value)
return
Expand Down Expand Up @@ -245,6 +244,10 @@ function! s:cache_persistent.read() dict abort " {{{1
endif

call extend(self.data, l:data, 'keep')

if !self.__validated
call self.validate()
endif
endfunction

" }}}1
Expand Down

0 comments on commit ac78b0c

Please sign in to comment.