Skip to content

Commit

Permalink
Fix performance impact of exception handling in _is_annotation_tid() (#…
Browse files Browse the repository at this point in the history
…925)

* After related test/tracing code were added to determine details related exception (including statistics comparison with earlier code). The data shows the newly added exception handling code impact the performance most while the difference in dictionary access is insignificant so only the exception handling code is changed.

* fixed black formatting issue

* (test) fixing CI mypy issue (1.7.1 +pyrhon 3.9) of "Unused" comment
  • Loading branch information
J007X authored Apr 2, 2023
1 parent 6a92c17 commit ae14581
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions forte/data/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ def fetch_entry_type_data(
else:
attr_fields: Dict = self._get_entry_attributes_by_class(type_name)
for attr_name, attr_info in attr_fields.items():

attr_class = get_origin(attr_info.type)
# Since we store the class specified by get_origin,
# if the output it None, we store the class for it,
Expand Down Expand Up @@ -1047,7 +1046,6 @@ def _add_entry_raw(
self._is_subclass(type_name, cls)
for cls in (list(SinglePackEntries) + list(MultiPackEntries))
):

try:
self.__elements[type_name].append(entry)
except KeyError:
Expand Down Expand Up @@ -1081,10 +1079,7 @@ def _is_annotation_tid(self, tid: int) -> bool:
elif tid in self.__tid_idx_dict:
return False
else:
raise KeyError(
f"Entry with tid {tid} not found."
f" Data store content is only {str(self.__dict__)}"
)
raise KeyError(f"Entry with tid {tid} not found.")

def _create_new_entry(
self, type_name: str, tid: Optional[int] = None
Expand Down Expand Up @@ -1246,7 +1241,6 @@ def add_entry_raw(
allow_duplicate: bool = True,
attribute_data: Optional[List] = None,
) -> int:

r"""
This function provides a general implementation to add all
types of entries to the data store. It can add namely
Expand Down Expand Up @@ -1870,7 +1864,9 @@ def co_iterator_annotation_like(
self.get_datastore_attr_idx(tn, constants.BEGIN_ATTR_NAME),
self.get_datastore_attr_idx(tn, constants.END_ATTR_NAME),
)
except IndexError as e: # all_entries_range[tn][0] will be caught here.
except (
IndexError
) as e: # all_entries_range[tn][0] will be caught here.
raise ValueError(
f"Entry list of type name, {tn} which is"
" one list item of input argument `type_names`,"
Expand Down

0 comments on commit ae14581

Please sign in to comment.