[jit][perf] Reduce lookupInModule overhead. (#119145)
It's inefficient to split remaining parts of the module name by '.' just to join it back again. Instead it's more idiomatic and efficient to use `maxsplit=1` to ensure that all remaining parts remain intact. This improves best case time and space complexity since scan can terminate on first encountered `.` and only 2 parts are returned in a list.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/119145
Approved by: https://github.com/Skylion007
diff --git a/torch/_jit_internal.py b/torch/_jit_internal.py
index d11d21b..6450981 100644
--- a/torch/_jit_internal.py
+++ b/torch/_jit_internal.py
@@ -105,9 +105,7 @@
def lookupInModule(qualified_name, module):
if "." in qualified_name:
- parts = qualified_name.split(".")
- base = parts[0]
- remaining_pieces = ".".join(parts[1:])
+ base, remaining_pieces = qualified_name.split(".", maxsplit=1)
module_value = getattr(module, base)
return lookupInModule(remaining_pieces, module_value)
else: