Skip to content

Commit 30ebcf3

Browse files
committed
Fix several accidentally quadratic functions
Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
1 parent 8050fd9 commit 30ebcf3

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/spdx_tools/spdx/model/document.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ def __init__(
8181
relationships = [] if relationships is None else relationships
8282
extracted_licensing_info = [] if extracted_licensing_info is None else extracted_licensing_info
8383
check_types_and_set_values(self, locals())
84+
85+
def __hash__(self):
86+
return id(self)

src/spdx_tools/spdx/model/relationship.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ def __init__(
7373
comment: Optional[str] = None,
7474
):
7575
check_types_and_set_values(self, locals())
76+
77+
def __hash__(self):
78+
return hash("{} -> {} ({})".format(self.spdx_element_id, str(self.related_spdx_element_id), str(self.relationship_type)))

src/spdx_tools/spdx/parser/jsonlikedict/relationship_parser.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2022 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from beartype.typing import Dict, List, Optional
4+
from beartype.typing import Dict, List, Optional, Set
55

66
from spdx_tools.common.typing.constructor_type_errors import ConstructorTypeErrors
77
from spdx_tools.spdx.model import Relationship, RelationshipType
@@ -35,9 +35,9 @@ def parse_all_relationships(self, input_doc_dict: Dict) -> List[Relationship]:
3535
document_describes: List[str] = delete_duplicates_from_list(input_doc_dict.get("documentDescribes", []))
3636
doc_spdx_id: Optional[str] = input_doc_dict.get("SPDXID")
3737

38-
existing_relationships_without_comments: List[Relationship] = self.get_all_relationships_without_comments(
38+
existing_relationships_without_comments: Set[Relationship] = set(self.get_all_relationships_without_comments(
3939
relationships
40-
)
40+
))
4141
relationships.extend(
4242
parse_field_or_log_error(
4343
self.logger,
@@ -52,9 +52,6 @@ def parse_all_relationships(self, input_doc_dict: Dict) -> List[Relationship]:
5252
)
5353

5454
package_dicts: List[Dict] = input_doc_dict.get("packages", [])
55-
existing_relationships_without_comments: List[Relationship] = self.get_all_relationships_without_comments(
56-
relationships
57-
)
5855

5956
relationships.extend(
6057
parse_field_or_log_error(
@@ -110,7 +107,7 @@ def parse_relationship_type(relationship_type_str: str) -> RelationshipType:
110107
return relationship_type
111108

112109
def parse_document_describes(
113-
self, doc_spdx_id: str, described_spdx_ids: List[str], existing_relationships: List[Relationship]
110+
self, doc_spdx_id: str, described_spdx_ids: List[str], existing_relationships: Set[Relationship]
114111
) -> List[Relationship]:
115112
logger = Logger()
116113
describes_relationships = []
@@ -131,10 +128,11 @@ def parse_document_describes(
131128
return describes_relationships
132129

133130
def parse_has_files(
134-
self, package_dicts: List[Dict], existing_relationships: List[Relationship]
131+
self, package_dicts: List[Dict], existing_relationships: Set[Relationship]
135132
) -> List[Relationship]:
136133
# assume existing relationships are stripped of comments
137134
logger = Logger()
135+
138136
contains_relationships = []
139137
for package in package_dicts:
140138
package_spdx_id: Optional[str] = package.get("SPDXID")
@@ -160,7 +158,7 @@ def parse_has_files(
160158
return contains_relationships
161159

162160
def check_if_relationship_exists(
163-
self, relationship: Relationship, existing_relationships: List[Relationship]
161+
self, relationship: Relationship, existing_relationships: Set[Relationship]
164162
) -> bool:
165163
# assume existing relationships are stripped of comments
166164
if relationship in existing_relationships:

src/spdx_tools/spdx/validation/spdx_id_validators.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import re
66

7-
from beartype.typing import List
7+
from beartype.typing import List, Set
8+
9+
from functools import cache
810

911
from spdx_tools.spdx.document_utils import get_contained_spdx_element_ids
1012
from spdx_tools.spdx.model import Document, File
@@ -22,11 +24,16 @@ def is_spdx_id_present_in_files(spdx_id: str, files: List[File]) -> bool:
2224
return spdx_id in [file.spdx_id for file in files]
2325

2426

27+
@cache
2528
def is_spdx_id_present_in_document(spdx_id: str, document: Document) -> bool:
26-
all_spdx_ids_in_document: List[str] = get_list_of_all_spdx_ids(document)
29+
all_spdx_ids_in_document: Set[str] = get_set_of_all_spdx_ids(document)
2730

2831
return spdx_id in all_spdx_ids_in_document
2932

33+
@cache
34+
def get_set_of_all_spdx_ids(document: Document) -> Set[str]:
35+
return set(get_list_of_all_spdx_ids(document))
36+
3037

3138
def get_list_of_all_spdx_ids(document: Document) -> List[str]:
3239
all_spdx_ids_in_document: List[str] = [document.creation_info.spdx_id]

0 commit comments

Comments
 (0)