Skip to content

Commit e6e5205

Browse files
authored
Avoid an unnecessary async indirection (#1226)
1 parent f1f2d75 commit e6e5205

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

services/comparison.py

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import copy
32
import functools
43
import json
@@ -17,6 +16,7 @@
1716
from shared.api_archive.archive import ArchiveService
1817
from shared.helpers.yaml import walk
1918
from shared.reports.types import ReportTotals
19+
from shared.torngit.base import TorngitBaseAdapter
2020
from shared.utils.merge import LineType, line_type
2121

2222
from compare.models import CommitComparison
@@ -646,6 +646,12 @@ def __init__(self, user, base_commit, head_commit):
646646
self._base_commit = base_commit
647647
self._head_commit = head_commit
648648

649+
@cached_property
650+
def _adapter(self) -> TorngitBaseAdapter:
651+
return RepoProviderService().get_adapter(
652+
owner=self.user, repo=self.base_commit.repository
653+
)
654+
649655
def validate(self):
650656
# make sure head and base reports exist (will throw an error if not)
651657
self.head_report
@@ -676,10 +682,7 @@ def get_file_comparison(self, file_name, with_src=False, bypass_max_diff=False):
676682
base_file = None
677683

678684
if with_src:
679-
adapter = RepoProviderService().get_adapter(
680-
owner=self.user, repo=self.base_commit.repository
681-
)
682-
file_content = async_to_sync(adapter.get_source)(
685+
file_content = async_to_sync(self._adapter.get_source)(
683686
file_name, self.head_commit.commitid
684687
)["content"]
685688
# make sure the file is str utf-8
@@ -697,9 +700,14 @@ def get_file_comparison(self, file_name, with_src=False, bypass_max_diff=False):
697700
bypass_max_diff=bypass_max_diff,
698701
)
699702

700-
@property
701-
def git_comparison(self):
702-
return self._fetch_comparison[0]
703+
@cached_property
704+
def git_comparison(self) -> dict:
705+
"""
706+
Fetches comparison, and caches the result.
707+
"""
708+
return async_to_sync(self._adapter.get_compare)(
709+
self.base_commit.commitid, self.head_commit.commitid
710+
)
703711

704712
@cached_property
705713
def base_report(self):
@@ -803,23 +811,6 @@ def upload_commits(self):
803811
commits_queryset.exclude(deleted=True)
804812
return commits_queryset
805813

806-
@cached_property
807-
def _fetch_comparison(self):
808-
"""
809-
Fetches comparison, and caches the result.
810-
"""
811-
adapter = RepoProviderService().get_adapter(
812-
self.user, self.base_commit.repository
813-
)
814-
comparison_coro = adapter.get_compare(
815-
self.base_commit.commitid, self.head_commit.commitid
816-
)
817-
818-
async def runnable():
819-
return await asyncio.gather(comparison_coro)
820-
821-
return async_to_sync(runnable)()
822-
823814
def flag_comparison(self, flag_name):
824815
return FlagComparison(self, flag_name)
825816

0 commit comments

Comments
 (0)