Skip to content

Commit 85da08a

Browse files
committed
Run ruff over code to get to pyright complaints
1 parent 8e2bf47 commit 85da08a

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

tdom/profiling/benchmark.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,48 +111,41 @@ def run_benchmark():
111111

112112
# End-to-end: Template -> DOM -> String
113113
results["full_pipeline"] = benchmark_operation(
114-
"Full pipeline (template → DOM → HTML)",
115-
lambda: str(html(template))
114+
"Full pipeline (template → DOM → HTML)", lambda: str(html(template))
116115
)
117116

118117
# Just parsing: Template -> DOM
119118
results["parse_only"] = benchmark_operation(
120-
"Parse only (template → DOM)",
121-
lambda: html(template)
119+
"Parse only (template → DOM)", lambda: html(template)
122120
)
123121

124122
# Just serialization: DOM -> String
125123
dom = html(template)
126124
results["serialize_only"] = benchmark_operation(
127-
"Serialize only (DOM → HTML)",
128-
lambda: str(dom)
125+
"Serialize only (DOM → HTML)", lambda: str(dom)
129126
)
130127

131128
# Small template (overhead measurement)
132129
small_template = t"<div><p>Hello</p></div>"
133130
results["small_template"] = benchmark_operation(
134-
"Small template (overhead baseline)",
135-
lambda: str(html(small_template))
131+
"Small template (overhead baseline)", lambda: str(html(small_template))
136132
)
137133

138134
# Heavy interpolation
139135
results["interpolation"] = benchmark_operation(
140-
"Heavy interpolation",
141-
lambda: str(html(interpolation_template))
136+
"Heavy interpolation", lambda: str(html(interpolation_template))
142137
)
143138

144139
# Component rendering (nested elements)
145140
nested_template = t"<div><div><div><p>Nested</p></div></div></div>"
146141
results["nested"] = benchmark_operation(
147-
"Nested elements",
148-
lambda: str(html(nested_template))
142+
"Nested elements", lambda: str(html(nested_template))
149143
)
150144

151145
# Attribute handling
152146
attr_template = t'<div id="test" class="foo bar" data-value="123">Content</div>'
153147
results["attributes"] = benchmark_operation(
154-
"Attribute handling",
155-
lambda: str(html(attr_template))
148+
"Attribute handling", lambda: str(html(attr_template))
156149
)
157150

158151
print("-" * 80)
@@ -173,9 +166,7 @@ def run_benchmark():
173166
else:
174167
print(" ✗ SLOW - Optimization recommended")
175168

176-
print(
177-
f"\n Current: {avg_time:.1f}μs/op | Target: <100μs/op | Best: <50μs/op"
178-
)
169+
print(f"\n Current: {avg_time:.1f}μs/op | Target: <100μs/op | Best: <50μs/op")
179170

180171
# Detailed breakdown
181172
print("\n" + "=" * 80)
@@ -187,9 +178,13 @@ def run_benchmark():
187178
full_time = results["full_pipeline"]
188179
overhead = full_time - (parse_time + serialize_time)
189180

190-
print(f"\nParsing: {parse_time:>8.3f}μs ({parse_time/full_time*100:.1f}%)")
191-
print(f"Serialization: {serialize_time:>8.3f}μs ({serialize_time/full_time*100:.1f}%)")
192-
print(f"Overhead: {overhead:>8.3f}μs ({overhead/full_time*100:.1f}%)")
181+
print(
182+
f"\nParsing: {parse_time:>8.3f}μs ({parse_time / full_time * 100:.1f}%)"
183+
)
184+
print(
185+
f"Serialization: {serialize_time:>8.3f}μs ({serialize_time / full_time * 100:.1f}%)"
186+
)
187+
print(f"Overhead: {overhead:>8.3f}μs ({overhead / full_time * 100:.1f}%)")
193188
print(f"Total: {full_time:>8.3f}μs (100.0%)")
194189

195190

tdom/profiling/benchmark_cache.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,26 @@ def benchmark_cache_scenario(name: str, templates, iterations: int = 1000):
103103
avg_without = without_cache_time / (iterations * len(templates))
104104
avg_with = with_cache_time / (iterations * len(templates))
105105
speedup = without_cache_time / with_cache_time if with_cache_time > 0 else 0
106-
savings_pct = ((without_cache_time - with_cache_time) / without_cache_time * 100) if without_cache_time > 0 else 0
106+
savings_pct = (
107+
((without_cache_time - with_cache_time) / without_cache_time * 100)
108+
if without_cache_time > 0
109+
else 0
110+
)
107111

108-
print(f" Without cache: {avg_without:>8.3f}μs/op (total: {without_cache_time/1000:.2f}ms)")
109-
print(f" With cache: {avg_with:>8.3f}μs/op (total: {with_cache_time/1000:.2f}ms)")
112+
print(
113+
f" Without cache: {avg_without:>8.3f}μs/op (total: {without_cache_time / 1000:.2f}ms)"
114+
)
115+
print(
116+
f" With cache: {avg_with:>8.3f}μs/op (total: {with_cache_time / 1000:.2f}ms)"
117+
)
110118
print(f" Speedup: {speedup:>8.2f}x")
111119
print(f" Time saved: {savings_pct:>8.1f}%")
112120

113121
# Cache stats
114122
info = parse_cached.cache_info()
115-
print(f" Cache stats: hits={info.hits}, misses={info.misses}, size={info.currsize}")
123+
print(
124+
f" Cache stats: hits={info.hits}, misses={info.misses}, size={info.currsize}"
125+
)
116126

117127
return {
118128
"without_cache": avg_without,
@@ -157,7 +167,9 @@ def benchmark_full_pipeline_cache():
157167

158168
print(f"\nRotating through {len(templates)} templates ({iterations} iterations):")
159169
print(f" Average time: {mixed_time:>8.3f}μs/op")
160-
print(f" Mix of {len(templates)} unique templates (25% cache hit rate per template)")
170+
print(
171+
f" Mix of {len(templates)} unique templates (25% cache hit rate per template)"
172+
)
161173

162174

163175
def run_benchmark():
@@ -173,28 +185,23 @@ def run_benchmark():
173185

174186
# Scenario 1: Best case - repeated parsing of same templates
175187
results_best = benchmark_cache_scenario(
176-
"Scenario 1: Best Case (100% cache hit rate)",
177-
templates,
178-
iterations=1000
188+
"Scenario 1: Best Case (100% cache hit rate)", templates, iterations=1000
179189
)
180190

181191
# Scenario 2: Single template repeated (extreme best case)
182192
results_single = benchmark_cache_scenario(
183193
"Scenario 2: Single Template Repeated (extreme best case)",
184194
[templates[0]],
185-
iterations=1000
195+
iterations=1000,
186196
)
187197

188198
# Scenario 3: More templates than cache (cache evictions)
189199
# Create 600 unique templates (more than cache maxsize=512)
190-
many_templates = [
191-
t"""<div id="{i}"><p>Content {i}</p></div>"""
192-
for i in range(600)
193-
]
200+
many_templates = [t"""<div id="{i}"><p>Content {i}</p></div>""" for i in range(600)]
194201
results_eviction = benchmark_cache_scenario(
195202
"Scenario 3: Cache Evictions (600 templates, cache size 512)",
196203
many_templates,
197-
iterations=10 # Fewer iterations due to many templates
204+
iterations=10, # Fewer iterations due to many templates
198205
)
199206

200207
# Full pipeline benchmark

0 commit comments

Comments
 (0)