Reducing RAM consumption by deleting unused caches

When the user deletes a buffer, we can delete the clang caches for that file.
Fixes #184.
This commit is contained in:
Strahinja Val Markovic 2013-03-14 20:39:44 -07:00
parent 6d53c1cf5a
commit c391bdcc62
8 changed files with 40 additions and 0 deletions

View File

@ -61,6 +61,7 @@ function! youcompleteme#Enable()
" that happens *after" BufRead/BufEnter has already triggered for the
" initial file.
autocmd BufRead,BufEnter * call s:OnBufferVisit()
autocmd BufDelete * call s:OnBufferDelete( expand( '<afile>:p' ) )
autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
autocmd InsertLeave * call s:OnInsertLeave()
autocmd InsertEnter * call s:OnInsertEnter()
@ -183,6 +184,15 @@ function! s:OnBufferVisit()
endfunction
function! s:OnBufferDelete( deleted_buffer_file )
if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
return
endif
py ycm_state.OnBufferDelete( vim.eval( 'a:deleted_buffer_file' ) )
endfunction
function! s:OnCursorHold()
if !s:AllowedToCompleteInCurrentFile()
return

View File

@ -262,6 +262,16 @@ ClangCompleter::CandidatesForQueryAndLocationInFileAsync(
}
void ClangCompleter::DeleteCachesForFile( const std::string &filename ) {
// If the clang thread is currently parsing the file when the user deletes the
// buffer and thus we try to delete the caches, Vim's GUI thread would block
// until the clang thread releases the mutex. Move this operation to the clang
// thread.
lock_guard< mutex > lock( filename_to_translation_unit_mutex_ );
filename_to_translation_unit_.erase( filename );
}
bool ClangCompleter::ShouldSkipClangResultCache( const std::string &query,
int line,
int column ) {

View File

@ -93,6 +93,8 @@ public:
const std::vector< UnsavedFile > &unsaved_files,
const std::vector< std::string > &flags );
void DeleteCachesForFile( const std::string &filename );
private:
// This is basically a union. Only one of the two tasks is set to something

View File

@ -81,6 +81,8 @@ TranslationUnit::~TranslationUnit() {
}
void TranslationUnit::Destroy() {
unique_lock< mutex > lock( clang_access_mutex_ );
if ( clang_translation_unit_ ) {
clang_disposeTranslationUnit( clang_translation_unit_ );
clang_translation_unit_ = NULL;

View File

@ -115,6 +115,7 @@ BOOST_PYTHON_MODULE(ycm_core)
class_< ClangCompleter, boost::noncopyable >( "ClangCompleter" )
.def( "EnableThreading", &ClangCompleter::EnableThreading )
.def( "DiagnosticsForFile", &ClangCompleter::DiagnosticsForFile )
.def( "DeleteCachesForFile", &ClangCompleter::DeleteCachesForFile )
.def( "UpdatingTranslationUnit", &ClangCompleter::UpdatingTranslationUnit )
.def( "UpdateTranslationUnitAsync",
&ClangCompleter::UpdateTranslationUnitAsync )

View File

@ -235,6 +235,10 @@ class Completer( object ):
pass
def OnBufferDelete( self, deleted_buffer_file ):
pass
def OnCursorHold( self ):
pass

View File

@ -147,6 +147,10 @@ class ClangCompleter( Completer ):
flags )
def OnBufferDelete( self, deleted_buffer_file ):
self.completer.DeleteCachesForFile( deleted_buffer_file )
def DiagnosticsForCurrentFileReady( self ):
if not self.parse_future:
return False

View File

@ -139,6 +139,13 @@ class YouCompleteMe( object ):
self.GetFiletypeCompleter().OnFileReadyToParse()
def OnBufferDelete( self, deleted_buffer_file ):
self.identcomp.OnBufferDelete( deleted_buffer_file )
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnBufferDelete( deleted_buffer_file )
def OnInsertLeave( self ):
self.identcomp.OnInsertLeave()