Performance rules
Finds code whose cost grows faster than its input, blocks concurrency, or leaks memory and resources.
- Agent id:
performance - Rules: 14 (source:
performance.yaml) - Deterministic checks:
performance_patterns
PERF-001: Query inside a loop (N+1)
- Severity: medium
- Languages: all
- References: CWE-1060
Look for database queries, ORM lazy relation access, HTTP calls or remote cache lookups (Redis, Memcached) made once per item inside a loop or a list comprehension over query results. Suggest eager loading (select_related, prefetch_related, include, JOIN), a batched IN query or a DataLoader. Raise it to high on request paths where the loop size grows with user data. A loop over a small fixed set (a handful of config keys) is not a violation.
PERF-002: Quadratic nested loop
- Severity: medium
- Languages: all
Look for nested loops over two collections that both grow with input, used to match, join or deduplicate items, where a set or dict built once would make each lookup O(1). Nested loops where the inner collection has a fixed small size, or over a matrix the algorithm must visit fully, are not violations.
PERF-003: Linear lookup in a loop
- Severity: medium
- Languages: all
Look for in on a list, indexOf, includes, find, contains on an array or list, or list.remove called inside a loop over another large collection. Suggest converting the searched collection to a set or map before the loop. Lookups on collections of a few elements are not violations.
PERF-004: String built by concatenation in a loop
- Severity: low
- Languages: python, java, csharp, go, javascript, typescript
- References: CWE-1046
Look for strings grown with += inside a loop over input of unbounded size, in languages where strings are immutable (Python, Java, C#, Go, JavaScript in hot paths). Suggest join, StringBuilder, strings.Builder or a list buffer. Short loops with a few iterations, and languages or runtimes that optimize the pattern, are not violations.
PERF-005: Blocking call in async code
- Severity: high
- Languages: python, javascript, typescript, rust, csharp, kotlin, dart
Look for synchronous I/O or sleeps inside async functions or event-loop callbacks, such as time.sleep, requests, blocking database drivers, open().read() on large files, fs.readFileSync, execSync, or CPU-heavy work without an executor or worker. Synchronous calls at startup before the loop serves traffic are not violations.
PERF-006: Sequential awaits that could run concurrently
- Severity: low
- Languages: python, javascript, typescript, csharp, rust, kotlin, dart
Look for await calls in a loop or in sequence where each call does not depend on the previous result, and suggest asyncio.gather, Promise.all or a bounded task group. Sequential calls kept on purpose to respect a rate limit or ordering are not violations.
PERF-007: Unbounded cache or collection
- Severity: high
- Languages: all
- References: CWE-401, CWE-770
Look for module-level dicts, lists, maps or lru_cache(maxsize=None) that grow with each request, user or message and are never evicted, and event listeners or subscriptions added repeatedly without removal. A cache with a size limit, a TTL, or a key space that is small and fixed is not a violation.
PERF-008: Whole dataset loaded into memory
- Severity: medium
- Languages: all
- References: CWE-400
Look for .all(), fetchall(), readlines(), read() on large files, SELECT * without LIMIT, or a full API page set collected into a list, where the data size grows with production use. Suggest streaming, pagination, iterators or chunked reads. Small lookup tables and files known to be small are not violations.
PERF-009: Missing timeout on network or subprocess calls
- Severity: high
- Languages: all
- References: CWE-1088
Look for HTTP clients, database connections, socket reads and subprocess calls created without a timeout (requests.get without timeout=, fetch without an abort signal, http.Client{} with no Timeout), so one slow dependency can hang a worker. Clients configured once with a default timeout are not violations.
PERF-010: Regular expression with catastrophic backtracking
- Severity: high
- Languages: all
- References: CWE-1333
Look for regexes applied to user input that contain nested quantifiers or overlapping alternatives, such as (a+)+, (.*)*, (\w|\d)+$. Regexes on trusted, bounded input, and engines without backtracking (RE2, Rust regex, Go regexp), are not violations.
PERF-011: Repeated work that could be cached or hoisted
- Severity: low
- Languages: all
- References: CWE-1050
Look for the same pure computation, regex compilation, file read, config parse, object construction or sorting of the same collection repeated inside a loop or on every request when its inputs do not change. Suggest computing it once outside the loop or at startup. Work that depends on per-iteration data is not a violation.
PERF-012: Missing index for a frequent query
- Severity: medium
- Languages: all
- References: CWE-1067
Look for queries filtering, joining or sorting on columns that the schema or migrations in the repository do not index, when the table grows with use (users, orders, events). Only report what the migrations show; do not assume an index is missing when the schema is not in the repository.
PERF-013: Expensive rendering or recomputation in UI code
- Severity: low
- Languages: javascript, typescript, dart
Look for React, Vue or Flutter components that create new objects, arrays or callbacks passed to memoized children on every render, derive large lists without memoization, or render long lists without virtualization. Small components and lists of a few dozen items are not violations.
PERF-014: Excessive object allocation in a hot loop
- Severity: low
- Languages: all
Look for objects, closures, buffers or boxed values created on every iteration of a tight loop that runs many times per request, where one instance could be reused or the value kept as a primitive. Allocation in code that runs once per request is not a violation.