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

Improving class loading efficiency and considering dynamic type list #919

Merged
merged 17 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 2 additions & 2 deletions forte/data/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sortedcontainers import SortedList
from typing_inspect import get_origin, get_args, is_generic_type

from forte.utils import get_class
from forte.utils import get_class, get_class_nc
from forte.utils.utils import get_full_module_name
from forte.data.ontology.code_generation_objects import EntryTree
from forte.data.ontology.ontology_code_generator import OntologyCodeGenerator
Expand Down Expand Up @@ -895,7 +895,7 @@ def _is_subclass(
if cls_qualified_name in type_name_parent_class:
return True
else:
entry_class = get_class(type_name)
entry_class = get_class_nc(type_name)
if issubclass(entry_class, cls):
type_name_parent_class.add(cls_qualified_name)
return True
Expand Down
60 changes: 57 additions & 3 deletions forte/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""
import sys
import difflib
from functools import wraps
from functools import wraps, lru_cache
from inspect import getfullargspec
from pydoc import locate
from typing import Dict, List, Optional, get_type_hints, Tuple
Expand All @@ -28,6 +28,7 @@
"get_full_module_name",
"get_class_name",
"get_class",
"get_class_nc",
"get_qual_name",
"create_class_with_kwargs",
"check_type",
Expand Down Expand Up @@ -78,8 +79,10 @@ def get_class_name(o, lower: bool = False) -> str:
return o.__name__


def get_class(full_class_name: str, module_paths: Optional[List[str]] = None):
r"""Returns the class based on class name.
def get_class_nc(
full_class_name: str, module_paths: Optional[List[str]] = None
):
r"""Returns the class based on class name, not cached.
J007X marked this conversation as resolved.
Show resolved Hide resolved

Args:
full_class_name (str): Name or full path to the class.
Expand Down Expand Up @@ -127,6 +130,57 @@ def get_class(full_class_name: str, module_paths: Optional[List[str]] = None):
return class_


@lru_cache()
def cached_locate(name_to_locate_class):
return locate(name_to_locate_class)


def get_class(full_class_name: str, module_paths: Optional[List[str]] = None):
r"""Returns the class based on class name, with cache to improve speed.
J007X marked this conversation as resolved.
Show resolved Hide resolved

Args:
class_name (str): Name or full path to the class.
module_paths (list): Paths to candidate modules to search for the
class. This is used if the class cannot be located solely based on
``class_name``. The first module in the list that contains the class
is used.

Returns:
The target class.

"""
class_ = cached_locate(full_class_name)
if (class_ is None) and (module_paths is not None):
for module_path in module_paths:
class_ = cached_locate(".".join([module_path, full_class_name]))
if class_ is not None:
break

# Try to find classes that are dynamically loaded, class_ will still be None if failed.
if class_ is None:
try:
module_name, class_name = full_class_name.rsplit(".", 1)
try:
class_ = getattr(sys.modules[module_name], class_name)
except (AttributeError, KeyError):
# ignore when cannot find the module in sys.modules or cannot find the class
# in the module.
pass
except ValueError:
# ignoring when the full class name doesn't have multiple parts.
pass

if class_ is None:
if module_paths:
raise ValueError(
f"Class not found in {module_paths}: {full_class_name}"
)
else:
raise ValueError(f"Class not found in {full_class_name}")

return class_
J007X marked this conversation as resolved.
Show resolved Hide resolved


def get_qual_name(o: object, lower: bool = False) -> str:
r"""Returns the qualified name of an object ``o``.

Expand Down