mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-05-08 00:09:15 -05:00
Improve performance on pre-commit -r pyflakes by 40%
This commit is contained in:
+5
-3
@@ -1,14 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
|
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
from pre_commit.util import memoize_by_cwd
|
||||||
|
|
||||||
# TODO: optimization: memoize based on local.cwd.getpath()
|
|
||||||
|
@memoize_by_cwd
|
||||||
def get_root():
|
def get_root():
|
||||||
return local['git']['rev-parse', '--show-toplevel']().strip()
|
return local['git']['rev-parse', '--show-toplevel']().strip()
|
||||||
|
|
||||||
|
|
||||||
|
@memoize_by_cwd
|
||||||
def get_pre_commit_path():
|
def get_pre_commit_path():
|
||||||
return os.path.join(get_root(), '.git/hooks/pre-commit')
|
return os.path.join(get_root(), '.git/hooks/pre-commit')
|
||||||
|
|
||||||
@@ -25,4 +27,4 @@ def remove_pre_commit():
|
|||||||
|
|
||||||
def get_head_sha(git_repo_path):
|
def get_head_sha(git_repo_path):
|
||||||
with local.cwd(git_repo_path):
|
with local.cwd(git_repo_path):
|
||||||
return (local['git']['rev-parse', 'HEAD'])().strip()
|
return (local['git']['rev-parse', 'HEAD'])().strip()
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
|
|
||||||
|
import functools
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class cached_property(object):
|
class cached_property(object):
|
||||||
"""Like @property, but caches the value."""
|
"""Like @property, but caches the value."""
|
||||||
|
|
||||||
@@ -14,3 +18,19 @@ class cached_property(object):
|
|||||||
value = self._func(obj)
|
value = self._func(obj)
|
||||||
obj.__dict__[self.__name__] = value
|
obj.__dict__[self.__name__] = value
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def memoize_by_cwd(func):
|
||||||
|
"""Memoize a function call based on os.getcwd()."""
|
||||||
|
cache = {}
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args):
|
||||||
|
cwd = os.getcwd()
|
||||||
|
key = (cwd,) + args
|
||||||
|
try:
|
||||||
|
return cache[key]
|
||||||
|
except KeyError:
|
||||||
|
ret = cache[key] = func(*args)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|||||||
+34
-2
@@ -1,8 +1,10 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import time
|
import random
|
||||||
|
from plumbum import local
|
||||||
|
|
||||||
from pre_commit.util import cached_property
|
from pre_commit.util import cached_property
|
||||||
|
from pre_commit.util import memoize_by_cwd
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -10,7 +12,7 @@ def class_with_cached_property():
|
|||||||
class Foo(object):
|
class Foo(object):
|
||||||
@cached_property
|
@cached_property
|
||||||
def foo(self):
|
def foo(self):
|
||||||
return "Foo" + str(time.time())
|
return "Foo" + str(random.getrandbits(64))
|
||||||
|
|
||||||
return Foo
|
return Foo
|
||||||
|
|
||||||
@@ -27,3 +29,33 @@ def test_unbound_cached_property(class_with_cached_property):
|
|||||||
prop = class_with_cached_property.foo
|
prop = class_with_cached_property.foo
|
||||||
assert isinstance(prop, cached_property)
|
assert isinstance(prop, cached_property)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def memoized_by_cwd():
|
||||||
|
@memoize_by_cwd
|
||||||
|
def func(arg):
|
||||||
|
return arg + str(random.getrandbits(64))
|
||||||
|
|
||||||
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoized_by_cwd_returns_same_twice_in_a_row(memoized_by_cwd):
|
||||||
|
ret = memoized_by_cwd('baz')
|
||||||
|
ret2 = memoized_by_cwd('baz')
|
||||||
|
assert ret is ret2
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoized_by_cwd_returns_different_for_different_args(memoized_by_cwd):
|
||||||
|
ret = memoized_by_cwd('baz')
|
||||||
|
ret2 = memoized_by_cwd('bar')
|
||||||
|
assert ret.startswith('baz')
|
||||||
|
assert ret2.startswith('bar')
|
||||||
|
assert ret != ret2
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoized_by_cwd_changes_with_different_cwd(memoized_by_cwd):
|
||||||
|
ret = memoized_by_cwd('baz')
|
||||||
|
with local.cwd('.git'):
|
||||||
|
ret2 = memoized_by_cwd('baz')
|
||||||
|
|
||||||
|
assert ret != ret2
|
||||||
|
|||||||
Reference in New Issue
Block a user