Skip to content

Performance optimization analysis: reduce unnecessary I/O, context overhead, and evaluation runtime #153

Description

@benjamin920102

Problem Description

After analyzing the repository architecture, core algorithms, and data flow, this issue proposes optimization opportunities without changing functional behavior.

The current implementation is already simple and mostly linear-time. There are limited opportunities to reduce asymptotic complexity because the workload size is dominated by:

reading configuration files
loading the markdown ruleset
scanning session history
injecting context messages
running evaluation cases

The main performance gains are expected from:

Reducing repeated file-system access
Avoiding unnecessary context traversal
Improving memory reuse
Reducing evaluation harness process overhead
Improving parallel execution

This issue separates:

A. Reducible Big-O complexity
B. Same Big-O but significant constant/cache/allocation/parallelism improvements
Repository Structure Analysis
High-level architecture
i-have-adhd-main/

├── extensions/
│ ├── i-have-adhd.ts
│ └── context-compat.ts

├── hooks/
│ └── always-on.mjs

├── skills/
│ └── i-have-adhd/
│ └── SKILL.md

├── scripts/
│ ├── run_evals.py
│ ├── judge.py
│ └── validation tools

├── tests/
│ └── automated tests

└── configuration files
Core Data Flow
Runtime extension flow
User starts session
|
v
session_start event
|
v
restoreState()
|
+---- load config
|
+---- check always-on flag
|
+---- scan session branch
|
v
syncContext()
|
+---- check if rules already injected
|
+---- inject SKILL.md if missing
|
v
Model receives context
Enable/Disable flow
/i-have-adhd on
|
v
setEnabled(true)
|
+---- append state entry
|
+---- update UI status
|
+---- sync context
Complexity Analysis
A. Reducible Big-O Complexity Issues

  1. Session history scanning in getSavedState()

File:

extensions/i-have-adhd.ts

Current:

for (const entry of ctx.sessionManager.getBranch()) {
...
}
Current complexity

Let:

N = number of session entries

Current:

Time: O(N)
Space: O(1)

Every session restore scans the complete branch.

For long conversations:

N = thousands of messages

This becomes increasingly expensive.

Proposed improvement

Maintain the latest state pointer.

Instead of:

session_start
|
scan entire history

Use:

session_start
|
read stored state metadata

Example:

ctx.sessionManager.getLatestCustomEntry(
STATE_ENTRY_TYPE
)

or maintain:

let currentStateEntryId
Expected improvement

Before:

Startup cost:
O(N)

After:

Startup cost:
O(1)

Priority:

★★★★★ High

  1. Context marker search optimization

File:

extensions/context-compat.ts

Current behavior:

rulesAreInContext()
|
scan context messages

Complexity:

O(M)

where:

M = number of context messages
Improvement

Maintain:

lastInjectedMarker

Instead of searching:

message1
message2
message3
...
messageN

Store:

{
rulesInjected:true,
timestamp
}
Expected improvement

Before:

O(M)

After:

O(1)

Priority:

★★★★☆

B. Big-O unchanged but large constant-factor improvements

  1. Cache SKILL.md loading

Files:

extensions/i-have-adhd.ts
hooks/always-on.mjs

Current:

readFileSync(SKILL_PATH)

Every startup:

disk IO
+
UTF-8 decoding
+
frontmatter parsing
Problem

The rules file is static.

Repeated parsing gives no benefit.

Improvement

Use module-level cache:

let cachedRules:string|null=null;

function loadRules(){
if(cachedRules)
return cachedRules;

cachedRules = readFileSync(...)
return cachedRules;

}
Complexity

Same:

O(S)

where S = size of SKILL.md

But:

Disk access:

many times
|
v
one time

Expected improvement:

faster startup
lower filesystem overhead

Priority:

★★★★★

  1. Avoid duplicate regex parsing

Current:

Both:

extensions/i-have-adhd.ts

hooks/always-on.mjs

contain:

/^--- ... ---/

for frontmatter removal.

Improvement

Create shared utility:

utils/frontmatter.ts

Benefits:

single implementation
less duplicated work
easier optimization

Priority:

★★★☆☆

  1. Reduce context injection size

Current:

When enabled:

ADHD MODE ACTIVE

entire SKILL.md

is injected.

The file is approximately:

~6800 characters
Problem

Every model request carries additional context.

Although Big-O does not change:

O(tokens)

constant cost increases.

Improvement

Split rules:

core rules
|
+-- always required

extended explanation
|
+-- optional

Example:

Current:

6000 tokens every session

Possible:

1500 token operational rules
+
optional documentation

Expected improvement:

lower context window usage
lower inference cost
faster generation

Priority:

★★★★★

  1. Evaluation framework parallel execution

Files:

scripts/run_evals.py
scripts/judge.py

Current:

Evaluation flow:

case1
|
runner
|
judge

case2
|
runner
|
judge

Sequential execution.

Complexity

Let:

C = number of evaluation cases
T = average evaluation time

Current:

O(C*T)

Technically Big-O remains:

O(C*T)

but wall-clock time improves.

Improvement

Use:

concurrent.futures.ProcessPoolExecutor

Example:

case1 ----
case2 ----- parallel workers
case3 ----/

Expected:

8 CPU cores:

8x throughput improvement

depending on external model latency.

Priority:

★★★★★

  1. Reduce subprocess creation overhead

File:

scripts/run_evals.py

Current:

Multiple:

subprocess.run()

calls.

Problem

Process creation overhead:

fork()
+
environment setup
+
CLI initialization
Improvement

Use:

persistent worker processes
multiprocessing pool
async runners

Expected improvement:

Large evaluation suites:

20-50% faster

Priority:

★★★★☆

Optimization Ranking
Rank Optimization Type Expected Benefit
1 Cache SKILL.md loading Constant reduction Very High
2 Parallel evaluation execution Parallelism Very High
3 Reduce injected context size Memory/token reduction Very High
4 Replace session scan with indexed state lookup Big-O reduction High
5 Cache context marker state Big-O reduction Medium
6 Persistent evaluation workers Allocation reduction Medium
7 Shared frontmatter parser Code maintenance Low-Medium
Expected Overall Impact
Runtime startup

Before:

Startup:

read config
+
read skill file
+
scan session history
+
scan context

After:

read cached config
+
read cached skill
+
O(1) state lookup
+
O(1) marker lookup

Expected:

30-70% faster startup
Long sessions

Current:

Performance decreases with conversation length

After:

Mostly constant-time state restoration
Evaluation pipeline

Current:

Sequential benchmark execution

After:

parallel workers
+
reused processes

Expected:

3-8x faster evaluation throughput
Implementation Priority
Phase 1 (high impact, low risk)
Cache SKILL.md contents
Reduce duplicated parsing
Reduce context injection size
Phase 2
Add indexed session state retrieval
Cache context marker state
Phase 3
Parallelize evaluation runner
Add persistent workers
Environment

Repository:

i-have-adhd-main

Runtime:

TypeScript extension
Node.js hooks
Python evaluation framework
Expected Result

The plugin should preserve identical behavior while achieving:

lower startup latency
lower memory/context usage
faster evaluation cycles
improved scalability for long sessions

No algorithmic behavior or output quality should change.

Additional Notes

This analysis intentionally separates:

True algorithmic improvements:
O(N) → O(1)

from:

Engineering optimizations:
same Big-O
but better cache locality,
less allocation,
less IO,
better parallel execution

because this repository is primarily an agent integration/plugin system rather than a computational algorithm library.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions