Repository navigation
Expand file tree
/
Copy pathcore.py
More file actions
47 lines (36 loc) · 1.78 KB
/
Copy pathcore.py
File metadata and controls
47 lines (36 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import time
from functools import wraps, lru_cache
from typing import Callable, Any, List
class AdaptiveMemoizer:
"""Performance optimizer that dynamically enables caching based on execution latency."""
def __init__(self, sample_size: int = 10, latency_threshold_ms: float = 0.5):
self.sample_size = sample_size
self.threshold = latency_threshold_ms / 1000.0
def __call__(self, func: Callable) -> Callable:
cached_variant = lru_cache(maxsize=2048)(func)
samples: List[float] = []
@wraps(func)
def adaptive_wrapper(*args: Any, **kwargs: Any) -> Any:
if getattr(adaptive_wrapper, "_is_hot", False):
return cached_variant(*args, **kwargs)
if len(samples) < self.sample_size:
start = time.perf_counter()
res = func(*args, **kwargs)
samples.append(time.perf_counter() - start)
return res
avg_latency = sum(samples) / len(samples)
if avg_latency > self.threshold:
setattr(adaptive_wrapper, "_is_hot", True)
return cached_variant(*args, **kwargs)
return func(*args, **kwargs)
def clear_stats():
samples.clear()
setattr(adaptive_wrapper, "_is_hot", False)
cached_variant.cache_clear()
adaptive_wrapper.clear_stats = clear_stats
adaptive_wrapper._is_hot = False
return adaptive_wrapper
def batch_process_optimized(items: List[Any], processor_func: Callable) -> List[Any]:
"""High-throughput batch utility leveraging adaptive execution path optimization."""
memoized = AdaptiveMemoizer(sample_size=5, latency_threshold_ms=0.1)(processor_func)
return [memoized(item) for item in items]