From d30279e0a97cadee804694a3c39b24ab4c841fea Mon Sep 17 00:00:00 2001 From: linearcombination <4829djaskdfj@gmail.com> Date: Fri, 6 Dec 2024 19:28:48 -0800 Subject: [PATCH] Small code change to save memory and speed up a code path --- backend/document/domain/document_generator.py | 6 +++--- backend/document/domain/resource_lookup.py | 10 +++++----- backend/document/stet/stet.py | 8 ++++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/document/domain/document_generator.py b/backend/document/domain/document_generator.py index d51c9b08..0b9e3939 100755 --- a/backend/document/domain/document_generator.py +++ b/backend/document/domain/document_generator.py @@ -329,7 +329,7 @@ def fetch_usfm_book_content_units( for dto in found_usfm_resource_lookup_dtos ] for resource_dir, dto in zip(resource_dirs, found_usfm_resource_lookup_dtos): - resource_lookup.provision_asset_files(dto, resource_dir) + resource_lookup.provision_asset_files(dto.url, resource_dir) t1 = time.time() logger.debug( "Time to provision USFM asset files (acquire and write to disk) for TW resource: %s", @@ -1003,7 +1003,7 @@ def generate_document( for dto in found_resource_lookup_dtos ] for resource_dir, dto in zip(resource_dirs, found_resource_lookup_dtos): - resource_lookup.provision_asset_files(dto, resource_dir) + resource_lookup.provision_asset_files(dto.url, resource_dir) t1 = time.time() logger.debug( "Time to provision asset files (acquire and write to disk): %s", t1 - t0 @@ -1178,7 +1178,7 @@ def generate_docx_document( for dto in found_resource_lookup_dtos ] for resource_dir, dto in zip(resource_dirs, found_resource_lookup_dtos): - resource_lookup.provision_asset_files(dto, resource_dir) + resource_lookup.provision_asset_files(dto.url, resource_dir) t1 = time.time() logger.debug( "Time to provision asset files (acquire and write to disk): %s", t1 - t0 diff --git a/backend/document/domain/resource_lookup.py b/backend/document/domain/resource_lookup.py index c0309629..e6a60908 100644 --- a/backend/document/domain/resource_lookup.py +++ b/backend/document/domain/resource_lookup.py @@ -491,6 +491,7 @@ def resource_types( # Used by some tests +@lru_cache(maxsize=100) def usfm_resource_types_and_book_tuples( lang_code: str, book_codes_str: str, @@ -532,7 +533,7 @@ def usfm_resource_types_and_book_tuples( ) logger.debug("dto: %s", dto) resource_filepath = prepare_resource_filepath(dto) - provision_asset_files(dto, resource_filepath) + provision_asset_files(dto.url, resource_filepath) content_file = parsing.usfm_asset_file( dto, resource_filepath, @@ -935,20 +936,19 @@ def resource_lookup_dto( def provision_asset_files( - resource_lookup_dto: ResourceLookupDto, + url: Optional[str], resource_filepath: str, ) -> None: if ( - resource_lookup_dto.url is not None + url is not None ): # We know that resource_url is not None because of how we got here, but mypy isn't convinced. Let's convince mypy. - clone_git_repo(resource_lookup_dto.url, resource_filepath) + clone_git_repo(url, resource_filepath) def prepare_resource_filepath( resource_lookup_dto: ResourceLookupDto, working_dir: str = settings.RESOURCE_ASSETS_DIR, ) -> str: - resource_filepath = "" if ( resource_lookup_dto.url is not None diff --git a/backend/document/stet/stet.py b/backend/document/stet/stet.py index b149c790..fe215a9d 100644 --- a/backend/document/stet/stet.py +++ b/backend/document/stet/stet.py @@ -355,7 +355,9 @@ def generate_docx_document( lang0_resource_dir = prepare_resource_filepath( lang0_resource_lookup_dto_ ) - provision_asset_files(lang0_resource_lookup_dto_, lang0_resource_dir) + provision_asset_files( + lang0_resource_lookup_dto_.url, lang0_resource_dir + ) current_task.update_state(state="Parsing asset files") source_usfm_book = usfm_book_content( lang0_resource_lookup_dto_, @@ -373,7 +375,9 @@ def generate_docx_document( lang1_resource_dir = prepare_resource_filepath( lang1_resource_lookup_dto_ ) - provision_asset_files(lang1_resource_lookup_dto_, lang1_resource_dir) + provision_asset_files( + lang1_resource_lookup_dto_.url, lang1_resource_dir + ) target_usfm_book = usfm_book_content( lang1_resource_lookup_dto_, lang1_resource_dir,