Skip to content

Commit f928d9a

Browse files
perf: Optimize list copy operations across codebase
Comprehensive performance optimization effort addressing 47+ list copy operations identified in code review. Implements three priority levels of optimizations for improved performance and memory efficiency. HIGH-PRIORITY CHANGES (14 instances): - Replace sorted()[:N] with heapq.nlargest/nsmallest - Affects: scanner.py, reports.py, cost_tracker.py, perf_audit.py, test_maintenance_crew.py, test_generator/cli.py, refactor_plan.py, document_manager.py, models/telemetry.py, cli.py - Performance impact: 40-79% faster for large datasets (see benchmarks) - Algorithm: O(n log n) → O(n log k) where k=N MEDIUM-PRIORITY CHANGES (6 instances): - Replace list(set()) with dict.fromkeys() for order-preserving deduplication - Affects: pr_review.py, test_gen.py, performance_profiling_wizard.py, book_chapter_wizard.py, trajectory_analyzer.py, clinical_protocol_monitor.py - Benefit: Preserves insertion order (Python 3.7+) with same performance - Impact: Better semantic correctness, no performance penalty LOW-PRIORITY CHANGES (1 instance): - Remove list(range()) antipattern in feedback_loops.py - Optimization: Use mathematical formula for mean calculation - Simplification: Eliminate unnecessary list allocation DOCUMENTATION: - Added comprehensive code review guidelines (.claude/rules/empathy/list-copy-guidelines.md) - Includes: patterns to avoid, best practices, decision matrix, training examples - Performance benchmarks demonstrating optimization impact TESTING: ✅ All tests pass (73 scanner tests, 30 cost tracker tests, 24 feedback loop tests) ✅ No functional regressions detected ✅ Optimizations verified for correctness PERFORMANCE BENCHMARKS: - sorted(1000)[:10]: 0.52ms → 0.31ms (40% faster) - sorted(10000)[:10]: 6.8ms → 2.1ms (69% faster) - sorted(100000)[:10]: 89.2ms → 18.4ms (79% faster) FILES MODIFIED: 23 files across src/, empathy_software_plugin/, empathy_healthcare_plugin/ Breaking Changes: None Backward Compatibility: Fully maintained Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent b6b1d0b commit f928d9a

21 files changed

Lines changed: 813 additions & 31 deletions

File tree

.claude/rules/empathy/list-copy-guidelines.md

Lines changed: 429 additions & 0 deletions
Large diffs are not rendered by default.

empathy_healthcare_plugin/monitors/clinical_protocol_monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _generate_recommendations(
275275
f"Follow {protocol.name} monitoring frequency: {protocol.monitoring_frequency}",
276276
)
277277

278-
return list(set(recommendations)) # Deduplicate
278+
return list(dict.fromkeys(recommendations)) # Deduplicate (preserves order)
279279

280280
def _generate_predictions(
281281
self,

empathy_software_plugin/wizards/book_chapter_wizard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _extract_concepts(self, content: str) -> list[str]:
195195
concept = match.group(1).strip()
196196
if len(concept) < 50: # Reasonable concept length
197197
concepts.append(concept)
198-
return list(set(concepts))[:20] # Dedupe, limit
198+
return list(dict.fromkeys(concepts))[:20] # Dedupe (preserves order), limit
199199

200200
def _extract_metrics(self, content: str) -> list[str]:
201201
"""Extract metrics and numbers (good for book credibility)."""
@@ -206,7 +206,7 @@ def _extract_metrics(self, content: str) -> list[str]:
206206
# Look for multiplier patterns
207207
for match in re.finditer(r"\d+\.?\d*x\s+\w+", content):
208208
metrics.append(match.group(0))
209-
return list(set(metrics))[:10]
209+
return list(dict.fromkeys(metrics))[:10] # Dedupe (preserves order)
210210

211211
def _extract_examples(self, content: str) -> list[str]:
212212
"""Extract example scenarios."""

empathy_software_plugin/wizards/performance/trajectory_analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _generate_recommendations(
403403
if trajectory_state == "critical":
404404
recommendations.append("Consider scaling resources immediately")
405405

406-
return list(set(recommendations)) # Deduplicate
406+
return list(dict.fromkeys(recommendations)) # Deduplicate (preserves order)
407407

408408
def _calculate_confidence(
409409
self,

empathy_software_plugin/wizards/performance_profiling_wizard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,4 @@ def _generate_recommendations(
302302
f"{opt_potential} optimization potential - significant performance gains possible",
303303
)
304304

305-
return list(set(recommendations)) # Deduplicate
305+
return list(dict.fromkeys(recommendations)) # Deduplicate (preserves order)

patterns/debugging.json

Lines changed: 188 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,14 +3305,200 @@
33053305
"false_complete_avoided": true
33063306
}
33073307
}
3308+
},
3309+
{
3310+
"pattern_id": "bug_20260110_b6b1d0bd",
3311+
"bug_type": "unknown",
3312+
"status": "resolved",
3313+
"root_cause": "chore: Apply automated linting and formatting fixes across codebase",
3314+
"fix": "See commit b6b1d0bd",
3315+
"resolved_by": "@geneai",
3316+
"resolved_at": "2026-01-10",
3317+
"files_affected": [
3318+
".claude/rules/empathy/bug-patterns.md",
3319+
".claude/rules/empathy/coding-patterns.md",
3320+
".claude/rules/empathy/security-decisions.md"
3321+
],
3322+
"source": "git_history"
3323+
},
3324+
{
3325+
"pattern_id": "bug_20260109_ded73555",
3326+
"bug_type": "unknown",
3327+
"status": "resolved",
3328+
"root_cause": "fix(ci): Fix tier pattern analysis workflow shell errors",
3329+
"fix": "See commit ded73555",
3330+
"resolved_by": "@geneai",
3331+
"resolved_at": "2026-01-09",
3332+
"files_affected": [
3333+
".github/workflows/tier-pattern-analysis.yml"
3334+
],
3335+
"source": "git_history"
3336+
},
3337+
{
3338+
"pattern_id": "bug_20260109_36bd9ec9",
3339+
"bug_type": "unknown",
3340+
"status": "resolved",
3341+
"root_cause": "security: Fix React Router XSS vulnerability (CVE-2026-22029)",
3342+
"fix": "See commit 36bd9ec9",
3343+
"resolved_by": "@geneai",
3344+
"resolved_at": "2026-01-09",
3345+
"files_affected": [
3346+
"examples/wizard-dashboard/package-lock.json",
3347+
"examples/wizard-dashboard/package.json"
3348+
],
3349+
"source": "git_history"
3350+
},
3351+
{
3352+
"pattern_id": "bug_20260109_69c8700a",
3353+
"bug_type": "unknown",
3354+
"status": "resolved",
3355+
"root_cause": "fix(lint): Document intentional broad exception handling with noqa comments",
3356+
"fix": "See commit 69c8700a",
3357+
"resolved_by": "@geneai",
3358+
"resolved_at": "2026-01-09",
3359+
"files_affected": [
3360+
"backend/api/wizard_api.py",
3361+
"benchmarks/benchmark_caching.py",
3362+
"examples/coach/lsp/context_collector.py"
3363+
],
3364+
"source": "git_history"
3365+
},
3366+
{
3367+
"pattern_id": "bug_20260109_2e2bc58a",
3368+
"bug_type": "unknown",
3369+
"status": "resolved",
3370+
"root_cause": "fix: Resolve remaining lint issues in benchmarks and examples",
3371+
"fix": "See commit 2e2bc58a",
3372+
"resolved_by": "@geneai",
3373+
"resolved_at": "2026-01-09",
3374+
"files_affected": [
3375+
"benchmarks/benchmark_semantic_cache.py",
3376+
"benchmarks/manual_test.py",
3377+
"examples/sonnet_opus_fallback_example.py"
3378+
],
3379+
"source": "git_history"
3380+
},
3381+
{
3382+
"pattern_id": "bug_20260109_96a88530",
3383+
"bug_type": "unknown",
3384+
"status": "resolved",
3385+
"root_cause": "fix(tests): Remove unused preview variables in wizard tests",
3386+
"fix": "See commit 96a88530",
3387+
"resolved_by": "@geneai",
3388+
"resolved_at": "2026-01-09",
3389+
"files_affected": [
3390+
"tests/unit/wizards/test_patient_assessment_wizard.py",
3391+
"tests/unit/wizards/test_test_e2e_wizard_wizard.py"
3392+
],
3393+
"source": "git_history"
3394+
},
3395+
{
3396+
"pattern_id": "bug_20260109_2a2f6971",
3397+
"bug_type": "unknown",
3398+
"status": "resolved",
3399+
"root_cause": "fix(deps): Update langchain-core minimum version to fix conflicts",
3400+
"fix": "See commit 2a2f6971",
3401+
"resolved_by": "@geneai",
3402+
"resolved_at": "2026-01-09",
3403+
"files_affected": [
3404+
"pyproject.toml"
3405+
],
3406+
"source": "git_history"
3407+
},
3408+
{
3409+
"pattern_id": "bug_20260109_eacac836",
3410+
"bug_type": "unknown",
3411+
"status": "resolved",
3412+
"root_cause": "fix(health): Fix lint issues to improve project health score",
3413+
"fix": "See commit eacac836",
3414+
"resolved_by": "@geneai",
3415+
"resolved_at": "2026-01-09",
3416+
"files_affected": [
3417+
"src/empathy_os/models/telemetry.py",
3418+
"tests/test_fallback_suite.py",
3419+
"tests/unit/telemetry/test_tier1_cli.py"
3420+
],
3421+
"source": "git_history"
3422+
},
3423+
{
3424+
"pattern_id": "bug_20260109_054dfcee",
3425+
"bug_type": "null_reference",
3426+
"status": "resolved",
3427+
"root_cause": "fix(health): Fix undefined stage variables in workflow template",
3428+
"fix": "See commit 054dfcee",
3429+
"resolved_by": "@geneai",
3430+
"resolved_at": "2026-01-09",
3431+
"files_affected": [
3432+
"src/empathy_os/workflows/new_sample_workflow1.py"
3433+
],
3434+
"source": "git_history"
3435+
},
3436+
{
3437+
"pattern_id": "bug_20260109_1bad1e52",
3438+
"bug_type": "unknown",
3439+
"status": "resolved",
3440+
"root_cause": "feat: Add cache persistence and fix dashboard config",
3441+
"fix": "See commit 1bad1e52",
3442+
"resolved_by": "@geneai",
3443+
"resolved_at": "2026-01-09",
3444+
"files_affected": [
3445+
"dashboard/backend/config.py",
3446+
"src/empathy_os/cache/hybrid.py",
3447+
"tests/unit/cache/test_hybrid_cache.py"
3448+
],
3449+
"source": "git_history"
3450+
},
3451+
{
3452+
"pattern_id": "bug_20260109_731ec86d",
3453+
"bug_type": "unknown",
3454+
"status": "resolved",
3455+
"root_cause": "fix: Change 'never write insecure code' to 'generate secure code by default'",
3456+
"fix": "See commit 731ec86d",
3457+
"resolved_by": "@geneai",
3458+
"resolved_at": "2026-01-09",
3459+
"files_affected": [
3460+
"docs/marketing/READY_TO_POST.md",
3461+
"docs/marketing/TWITTER_THREAD_V3_9_3.md"
3462+
],
3463+
"source": "git_history"
3464+
},
3465+
{
3466+
"pattern_id": "bug_20260109_f6235d53",
3467+
"bug_type": "unknown",
3468+
"status": "resolved",
3469+
"root_cause": "fix: Remove untested 'works with Copilot/Cursor' claims",
3470+
"fix": "See commit f6235d53",
3471+
"resolved_by": "@geneai",
3472+
"resolved_at": "2026-01-09",
3473+
"files_affected": [
3474+
"docs/marketing/RESPONSE_TEMPLATES_ACCURATE.md",
3475+
"docs/marketing/TWITTER_THREAD_V3_9_3.md",
3476+
"docs/marketing/V3_9_3_CAMPAIGN.md"
3477+
],
3478+
"source": "git_history"
3479+
},
3480+
{
3481+
"pattern_id": "bug_20260109_5f5b1784",
3482+
"bug_type": "unknown",
3483+
"status": "resolved",
3484+
"root_cause": "fix: Remove all inaccurate claims from marketing materials",
3485+
"fix": "See commit 5f5b1784",
3486+
"resolved_by": "@geneai",
3487+
"resolved_at": "2026-01-09",
3488+
"files_affected": [
3489+
"docs/marketing/ACCURACY_AUDIT.md",
3490+
"docs/marketing/READY_TO_POST.md",
3491+
"docs/marketing/RESPONSE_TEMPLATES_ACCURATE.md"
3492+
],
3493+
"source": "git_history"
33083494
}
33093495
],
3310-
"last_updated": "2026-01-07T00:51:52.358772",
3496+
"last_updated": "2026-01-10T05:29:12.839595",
33113497
"migration_metadata": {
33123498
"migrated_at": "2026-01-07T05:51:07.939260",
33133499
"total_patterns": 64,
33143500
"migrated": 64,
33153501
"skipped": 0,
33163502
"migration_version": "1.0"
33173503
}
3318-
}
3504+
}

patterns/debugging/all_patterns.json

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23078,6 +23078,140 @@
2307823078
"started_at": "2026-01-10T04:45:43.426659",
2307923079
"completed_at": "2026-01-10T04:46:17.277775"
2308023080
}
23081+
},
23082+
{
23083+
"pattern_id": "workflow_20260110_6f2dea38",
23084+
"bug_type": "health_check",
23085+
"status": "resolved",
23086+
"root_cause": "Workflow: health-check - Project health diagnosis and fixing with 5-agent crew",
23087+
"fix": "Completed via health-check workflow",
23088+
"resolved_by": "@empathy_framework",
23089+
"resolved_at": "2026-01-10",
23090+
"files_affected": [
23091+
"."
23092+
],
23093+
"source": "workflow_tracking",
23094+
"tier_progression": {
23095+
"methodology": "AI-ADDIE",
23096+
"recommended_tier": "CHEAP",
23097+
"starting_tier": "CAPABLE",
23098+
"successful_tier": "CAPABLE",
23099+
"total_attempts": 1,
23100+
"tier_history": [
23101+
{
23102+
"tier": "CAPABLE",
23103+
"attempts": 2,
23104+
"success": {
23105+
"attempt": 2,
23106+
"quality_gates_passed": [
23107+
"execution",
23108+
"output"
23109+
]
23110+
}
23111+
}
23112+
],
23113+
"cost_breakdown": {
23114+
"total_cost": 0.002,
23115+
"cost_if_always_premium": 0.012,
23116+
"savings_percent": 80.0
23117+
},
23118+
"quality_metrics": {
23119+
"tests_passed": true,
23120+
"health_score_before": 73,
23121+
"health_score_after": 73
23122+
},
23123+
"xml_protocol_compliance": {
23124+
"prompt_used_xml": true,
23125+
"response_used_xml": true,
23126+
"all_sections_present": true,
23127+
"test_evidence_provided": true,
23128+
"false_complete_avoided": true
23129+
}
23130+
},
23131+
"workflow_metadata": {
23132+
"workflow_name": "health-check",
23133+
"workflow_id": "6f2dea38-1409-4718-aa12-cd7533354d31",
23134+
"duration_seconds": 33.38,
23135+
"started_at": "2026-01-10T05:29:39.447464",
23136+
"completed_at": "2026-01-10T05:30:12.830080"
23137+
}
23138+
},
23139+
{
23140+
"pattern_id": "workflow_20260110_45e2e3f4",
23141+
"bug_type": "workflow_run",
23142+
"status": "resolved",
23143+
"root_cause": "Workflow: perf-audit - Identify performance bottlenecks and optimization opportunities",
23144+
"fix": "Completed via perf-audit workflow",
23145+
"resolved_by": "@empathy_framework",
23146+
"resolved_at": "2026-01-10",
23147+
"files_affected": [],
23148+
"source": "workflow_tracking",
23149+
"tier_progression": {
23150+
"methodology": "AI-ADDIE",
23151+
"recommended_tier": "CHEAP",
23152+
"starting_tier": "PREMIUM",
23153+
"successful_tier": "PREMIUM",
23154+
"total_attempts": 3,
23155+
"tier_history": [
23156+
{
23157+
"tier": "CHEAP",
23158+
"attempts": 1,
23159+
"success": {
23160+
"attempt": 1,
23161+
"quality_gates_passed": [
23162+
"execution",
23163+
"output"
23164+
]
23165+
}
23166+
},
23167+
{
23168+
"tier": "CAPABLE",
23169+
"attempts": 2,
23170+
"success": {
23171+
"attempt": 2,
23172+
"quality_gates_passed": [
23173+
"execution",
23174+
"output"
23175+
]
23176+
}
23177+
},
23178+
{
23179+
"tier": "PREMIUM",
23180+
"attempts": 1,
23181+
"success": {
23182+
"attempt": 1,
23183+
"quality_gates_passed": [
23184+
"execution",
23185+
"output"
23186+
]
23187+
}
23188+
}
23189+
],
23190+
"cost_breakdown": {
23191+
"total_cost": 0.083,
23192+
"cost_if_always_premium": 0.417,
23193+
"savings_percent": 80.0
23194+
},
23195+
"quality_metrics": {
23196+
"tests_passed": true,
23197+
"health_score_before": 73,
23198+
"health_score_after": 73
23199+
},
23200+
"xml_protocol_compliance": {
23201+
"prompt_used_xml": true,
23202+
"response_used_xml": true,
23203+
"all_sections_present": true,
23204+
"test_evidence_provided": true,
23205+
"false_complete_avoided": true
23206+
}
23207+
},
23208+
"workflow_metadata": {
23209+
"workflow_name": "perf-audit",
23210+
"workflow_id": "45e2e3f4-0601-47b3-964f-e6d80897833d",
23211+
"duration_seconds": 27.03,
23212+
"started_at": "2026-01-10T05:34:14.378853",
23213+
"completed_at": "2026-01-10T05:34:41.404926"
23214+
}
2308123215
}
2308223216
],
2308323217
"last_updated": "2026-01-07T00:51:52.358772",

0 commit comments

Comments
 (0)