Skip to content

Commit ebd1c70

Browse files
committed
Optimize memory backend path lookups
Avoid constructing pathlib Path objects when a memory backend key is already canonical POSIX text. OpenHCS hot paths repeatedly probe memory-backed artifacts with normalized keys, so the previous unconditional normalization added avoidable overhead across load/save/existence checks. Also add a direct exists() implementation for the in-memory store so callers that only need key presence do not pay for exception-oriented file probing. The change is storage-local and preserves the existing normalization path for non-canonical strings and Path inputs.
1 parent 6609400 commit ebd1c70

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/polystore/memory.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,20 @@ def __init__(self, shared_dict: Optional[Dict[str, Any]] = None):
3636
self._memory_store = shared_dict if shared_dict is not None else {}
3737
self._prefixes = set() # Declared directory-like namespaces
3838

39-
def _normalize(self, path: Union[str, Path],bypass_normalization=False) -> str:
39+
@staticmethod
40+
def _is_canonical_posix_path_text(path: str) -> bool:
41+
"""Return whether Path(path).as_posix() would preserve path exactly."""
42+
if not path:
43+
return False
44+
if "\\" in path or "//" in path:
45+
return False
46+
if path.startswith("./") or "/./" in path:
47+
return False
48+
if path != "/" and path.endswith(("/", "/.")):
49+
return False
50+
return True
51+
52+
def _normalize(self, path: Union[str, Path], bypass_normalization=False) -> str:
4053
"""
4154
Normalize paths for memory backend storage.
4255
@@ -50,6 +63,9 @@ def _normalize(self, path: Union[str, Path],bypass_normalization=False) -> str:
5063
Returns:
5164
Normalized relative path string
5265
"""
66+
if isinstance(path, str) and self._is_canonical_posix_path_text(path):
67+
return path
68+
5369
path_obj = Path(path)
5470

5571
if bypass_normalization:
@@ -363,6 +379,10 @@ def is_file(self, path: Union[str, Path]) -> bool:
363379
raise IsADirectoryError(f"Path is a directory: {path}")
364380

365381
return True
382+
383+
def exists(self, path: Union[str, Path]) -> bool:
384+
"""Check whether a normalized memory key exists without exception probes."""
385+
return self._normalize(path) in self._memory_store
366386

367387
def is_dir(self, path: Union[str, Path]) -> bool:
368388
"""

0 commit comments

Comments
 (0)