Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cache file hasher by ignoring env, pycache and use faster hasher #99

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions djlsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,41 @@ def _store_django_data_to_cache(self, django_data):
def _get_cache_file_hash(self, django_data):
start_time = time.time()

patterns = []
if self.project_env_path and self.project_env_path.startswith(
self.project_src_path
):
# Prevent using env directory because this increase the calculation
# time by 100x
for file_ in os.scandir(self.project_src_path):
if file_.is_dir():
full_path = os.path.join(self.project_src_path, file_)
for pattern in django_data.get("file_watcher_globs", []):
if not full_path.startswith(self.project_env_path):
patterns.append(os.path.join(full_path, pattern))
else:
patterns = list(
[
os.path.join(self.project_src_path, pattern)
in django_data.get("file_watcher_globs", [])
]
)

files = set(
f
for p in django_data["file_watcher_globs"]
for f in glob.glob(p, recursive=True)
file_
for pattern in patterns
for file_ in glob.iglob(pattern, recursive=True)
)
files.add(DJANGO_COLLECTOR_SCRIPT_PATH)

h = hashlib.md5()
for f in sorted(files):
if os.path.isfile(f):
h.update(f"{os.stat(f).st_mtime}".encode())
files_hash = hashlib.blake2b(digest_size=16)
for file_path in sorted(files):
if "__pycache__" not in full_path and os.path.isfile(file_path):
files_hash.update(f"{os.stat(file_path).st_mtime}".encode())

logger.debug(f"Caculating cache hash took {time.time() - start_time:.4f}s")

return h.hexdigest()
return files_hash.hexdigest()

def _get_cache_location(self):
if self.cache is True and self.workspace.root_path:
Expand Down
2 changes: 1 addition & 1 deletion tests/django_test/.helix/languages.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[language-server.djlsp]
command = "djlsp"
args = ["--enable-log"]
args = ["--enable-log", "--cache"]

[language-server.djlsp.config]
django_settings_modules="django_test.settings"
Expand Down