Permalink
Browse files

reuse tmp files and only update git every 5 seconds

  • Loading branch information...
1 parent efa61b1 commit 850498cbb825a73a9deb8d537c300c25a4246870 @jisaacks committed Dec 12, 2012
Showing with 47 additions and 10 deletions.
  1. +14 −9 git_gutter_handler.py
  2. +33 −1 view_collection.py
View
@@ -1,14 +1,14 @@
import git_helper
import sublime
import subprocess
-import tempfile
import re
+import view_collection
class GitGutterHandler:
def __init__(self, view):
self.view = view
- self.git_temp_file = tempfile.NamedTemporaryFile()
- self.buf_temp_file = tempfile.NamedTemporaryFile()
+ self.git_temp_file = view_collection.ViewCollection.git_tmp_file(self.view)
+ self.buf_temp_file = view_collection.ViewCollection.buf_tmp_file(self.view)
if self.on_disk():
self.git_tree = git_helper.git_tree(self.view)
self.git_dir = git_helper.git_dir(self.git_tree)
@@ -34,12 +34,17 @@ def update_buf_file(self):
f.close()
def update_git_file(self):
- self.git_temp_file.truncate()
- args = ['git','--git-dir='+self.git_dir,'--work-tree='+self.git_tree,'show','head:'+self.git_path]
- try:
- subprocess.call(args, stdout=self.git_temp_file)
- except Exception:
- pass
+ # the git repo won't change that often
+ # so we can easily wait 5 seconds
+ # between updates for performance
+ if view_collection.ViewCollection.git_time(self.view) > 5:
+ self.git_temp_file.truncate()
+ args = ['git','--git-dir='+self.git_dir,'--work-tree='+self.git_tree,'show','head:'+self.git_path]
+ try:
+ subprocess.call(args, stdout=self.git_temp_file)
+ view_collection.ViewCollection.update_git_time(self.view)
+ except Exception:
+ pass
def process_diff(self,diff_str):
inserted = []
View
@@ -1,7 +1,13 @@
import git_gutter_handler
+import tempfile
+import time
class ViewCollection:
views = {}
+ git_times = {}
+ git_files = {}
+ buf_files = {}
+
@staticmethod
def add(view):
key = ViewCollection.get_key(view)
@@ -23,4 +29,30 @@ def get_key(view):
@staticmethod
def diff(view):
key = ViewCollection.get_key(view)
- return ViewCollection.views[key].diff()
+ return ViewCollection.views[key].diff()
+
+ @staticmethod
+ def git_time(view):
+ key = ViewCollection.get_key(view)
+ if not key in ViewCollection.git_times:
+ ViewCollection.git_times[key] = 0
+ return time.time() - ViewCollection.git_times[key]
+
+ @staticmethod
+ def update_git_time(view):
+ key = ViewCollection.get_key(view)
+ ViewCollection.git_times[key] = time.time()
+
+ @staticmethod
+ def git_tmp_file(view):
+ key = ViewCollection.get_key(view)
+ if not key in ViewCollection.git_files:
+ ViewCollection.git_files[key] = tempfile.NamedTemporaryFile()
+ return ViewCollection.git_files[key]
+
+ @staticmethod
+ def buf_tmp_file(view):
+ key = ViewCollection.get_key(view)
+ if not key in ViewCollection.buf_files:
+ ViewCollection.buf_files[key] = tempfile.NamedTemporaryFile()
+ return ViewCollection.buf_files[key]

0 comments on commit 850498c

Please sign in to comment.