codeprobe-performance

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Standalone Mode

独立模式

If invoked directly (not via the orchestrator), you must first:
  1. Read
    ../codeprobe/shared-preamble.md
    for the output contract, execution modes, and constraints.
  2. Load applicable reference files from
    ../codeprobe/references/
    based on the project's tech stack.
  3. Default to
    full
    mode unless the user specifies otherwise.
如果直接调用(不通过编排器),您必须首先:
  1. 阅读
    ../codeprobe/shared-preamble.md
    以了解输出协议、执行模式和约束条件。
  2. 根据项目技术栈加载
    ../codeprobe/references/
    中的适用参考文件。
  3. 除非用户另行指定,否则默认使用
    full
    模式。

Performance & Scalability Auditor

性能与可扩展性审计器

Domain Scope

领域范围

This sub-skill detects performance and scalability issues across these categories:
  1. N+1 Queries — Lazy-loading relationships inside loops
  2. Missing Indexes — WHERE/ORDER BY on non-indexed columns
  3. Unbounded Queries — Model::all() without pagination/limit
  4. Memory — Loading entire files into memory, array accumulation in loops
  5. Caching — Repeated identical queries, missing TTL, stale cache after writes
  6. Algorithmic Efficiency — O(n^2) in hot paths, nested loops, sorting in loops
  7. Concurrency — Race conditions, non-idempotent queue jobs, shared mutable state
  8. Frontend Performance — Unnecessary re-renders, bundle size, missing lazy loading

该子技能检测以下类别的性能与可扩展性问题:
  1. N+1 查询 — 循环内延迟加载关联关系
  2. 缺失索引 — 在未索引列上使用WHERE/ORDER BY
  3. 无界查询 — Model::all()未使用分页/限制
  4. 内存问题 — 将整个文件加载到内存中、循环内数组累积
  5. 缓存问题 — 重复执行相同查询、缺失TTL、写入后缓存过期
  6. 算法效率 — 热点路径中的O(n^2)复杂度、嵌套循环、循环内排序
  7. 并发问题 — 竞态条件、非幂等队列任务、共享可变状态
  8. 前端性能 — 不必要的重渲染、包体积过大、缺失懒加载

What It Does NOT Flag

不标记的内容

  • Premature optimization in non-hot-path code — proportional design matters. A utility function called once at startup doesn't need the same optimization as a request handler.
  • Development-only debug queries — queries in seeders, dev-only commands, or debug endpoints.
  • Batch processing scripts — scripts intentionally designed to process everything (migrations, data backfills) where unbounded queries may be appropriate.
  • O(n^2) on small bounded collections (<100 items) — nested loops on small known-size arrays are fine.
  • Frontend SSR/build-time code — server components and build scripts have different performance profiles than client-side code.

  • 非热点路径中的过早优化 — 设计需与场景匹配。启动时仅调用一次的工具函数无需与请求处理函数采用相同的优化标准。
  • 仅开发环境使用的调试查询 — 种子文件、仅开发环境命令或调试端点中的查询。
  • 批处理脚本 — 专门设计用于处理全部数据的脚本(如迁移、数据回填),此类场景中无界查询可能是合理的。
  • 小型有限集合(<100条数据)上的O(n^2)操作 — 在已知大小的小型数组上使用嵌套循环是可行的。
  • 前端SSR/构建时代码 — 服务端组件和构建脚本的性能特征与客户端代码不同。

Detection Instructions

检测说明

N+1 Queries

N+1 查询

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Eloquent relationship access inside loop without eager loadingSearch for
foreach
/
for
loops iterating over a collection, then accessing a relationship property (e.g.,
$order->items
,
$user->profile
) inside the loop body. Check whether the query that produced the collection includes
with()
or
load()
for that relationship.
Critical
PERF
Any ORM lazy-loading inside iterationLook for patterns where a database query is implicitly triggered inside a loop: Django querysets accessed per-iteration, SQLAlchemy lazy loads, Prisma relation access in
.map()
.
Critical
PERF
Template/view triggering queriesBlade templates, Jinja2 templates, or React components calling relationship properties that trigger queries during rendering.Major
ID 前缀检测内容检测方式严重程度
PERF
循环内访问Eloquent关联关系但未使用预加载搜索遍历集合的
foreach
/
for
循环,检查循环体中是否访问关联属性(例如
$order->items
$user->profile
)。确认生成该集合的查询是否包含对应关联的
with()
load()
方法。
严重
PERF
迭代过程中任何ORM延迟加载查找循环内隐式触发数据库查询的模式:Django查询集逐次访问、SQLAlchemy延迟加载、Prisma在
.map()
中访问关联。
严重
PERF
模板/视图触发查询Blade模板、Jinja2模板或React组件在渲染时调用关联属性从而触发查询。主要

Missing Indexes

缺失索引

ID PrefixWhat to DetectHow to DetectSeverity
PERF
WHERE/ORDER BY on non-indexed columnsCross-reference query conditions (
where()
,
orderBy()
,
WHERE
,
ORDER BY
) with migration files or schema definitions to check for matching indexes.
Major
PERF
Foreign keys without indexesCheck migration files for
foreignId()
,
foreign()
,
references()
without corresponding index definitions. Most ORMs add these automatically, but raw migrations may miss them.
Minor
PERF
Composite queries needing compound indexesMultiple
where()
conditions on different columns in the same query, or
where() + orderBy()
combinations that would benefit from a compound index.
Minor
ID 前缀检测内容检测方式严重程度
PERF
在未索引列上使用WHERE/ORDER BY将查询条件(
where()
orderBy()
WHERE
ORDER BY
)与迁移文件或模式定义交叉比对,检查是否存在匹配的索引。
主要
PERF
外键未设置索引检查迁移文件中的
foreignId()
foreign()
references()
是否未对应定义索引。大多数ORM会自动添加这些索引,但原生迁移可能会遗漏。
次要
PERF
需要复合索引的复合查询同一查询中对不同列使用多个
where()
条件,或
where() + orderBy()
组合,此类场景可受益于复合索引。
次要

Unbounded Queries

无界查询

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Model::all()
or
SELECT *
without limit
Search for
.all()
,
::all()
,
findAll()
,
SELECT * FROM
without
LIMIT
,
paginate()
,
take()
, or
limit()
.
Major
PERF
Missing cursor/chunk for large dataset processingOperations that
get()
or load entire collections when processing large datasets. Should use
cursor()
,
chunk()
,
lazy()
, or equivalent streaming approach.
Major
ID 前缀检测内容检测方式严重程度
PERF
Model::all()
SELECT *
未设置限制
搜索
.all()
::all()
findAll()
SELECT * FROM
,检查是否未搭配
LIMIT
paginate()
take()
limit()
主要
PERF
处理大型数据集时缺失游标/分块使用
get()
或加载整个集合来处理大型数据集的操作。应使用
cursor()
chunk()
lazy()
或等效的流式处理方式。
主要

Memory

内存问题

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Loading entire files into memory
file_get_contents()
on user uploads or large files,
fs.readFileSync()
on variable-size files, Python
open().read()
without size limits.
Major
PERF
Array accumulation in loopsArrays that grow inside loops without bounds or cleanup — collecting results in memory that could be streamed or yielded.Minor
PERF
Large collections instead of generatorsReturning full arrays/lists where generators (
yield
), lazy collections, or iterators would be more memory-efficient for large datasets.
Minor
ID 前缀检测内容检测方式严重程度
PERF
将整个文件加载到内存中对用户上传文件或大文件使用
file_get_contents()
,对可变大小文件使用
fs.readFileSync()
,Python中使用
open().read()
且未设置大小限制。
主要
PERF
循环内数组累积循环内无限制增长或未清理的数组——在内存中收集结果,而这些结果本可以流式传输或生成。次要
PERF
使用大型集合而非生成器返回完整数组/列表,而对于大型数据集,使用生成器(
yield
)、延迟集合或迭代器会更节省内存。
次要

Caching

缓存问题

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Repeated identical queries in same requestSame query pattern executed multiple times within a single request/function call without caching the result.Minor
PERF
Cache without TTL
Cache::put()
,
cache.set()
, Redis SET without expiration. Data cached forever risks staleness.
Minor
PERF
Cache not invalidated after writesWrite operations (create/update/delete) that don't clear or update related cache entries. Stale cache served after mutation.Major
ID 前缀检测内容检测方式严重程度
PERF
同一请求中重复执行相同查询同一请求/函数调用中多次执行相同查询模式但未缓存结果。次要
PERF
缓存未设置TTL
Cache::put()
cache.set()
、Redis SET未设置过期时间。永久缓存的数据存在过期风险。
次要
PERF
写入后未失效缓存写入操作(创建/更新/删除)未清除或更新相关缓存条目。数据变更后仍提供过期缓存。主要

Algorithmic Efficiency

算法效率

ID PrefixWhat to DetectHow to DetectSeverity
PERF
O(n^2) or worse in hot pathsNested loops iterating over the same or related collections.
array_search
/
in_array
/
includes()
inside loops (linear search in a loop = O(n^2)).
Major
PERF
Sorting inside loops
sort()
,
usort()
,
array_sort()
,
.sort()
called inside a loop body.
Major
PERF
Hashmap-replaceable linear search
in_array()
,
.includes()
,
.indexOf()
,
list.index()
used repeatedly on the same array where building a Set/dict/hashmap first would be O(1) per lookup.
Minor
ID 前缀检测内容检测方式严重程度
PERF
热点路径中的O(n^2)或更差复杂度遍历相同或相关集合的嵌套循环。循环内使用
array_search
/
in_array
/
includes()
(循环内线性搜索=O(n^2))。
主要
PERF
循环内排序循环体中调用
sort()
usort()
array_sort()
.sort()
主要
PERF
可替换为哈希表的线性搜索对同一数组重复使用
in_array()
.includes()
.indexOf()
list.index()
,此时预先构建Set/字典/哈希表可将每次查找的复杂度降至O(1)。
次要

Concurrency

并发问题

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Race conditions in read-modify-writePatterns that read a value, modify it, and write back without locking: incrementing counters, updating balances, toggling flags in concurrent contexts.Critical
PERF
Queue jobs without idempotencyQueue/job handlers that don't check for duplicate execution. Jobs that create resources without checking if already created. Missing unique constraints on job-created data.Major
PERF
Shared mutable state in async contextsGlobal/module-level mutable variables accessed in async handlers, request-scoped data stored in module scope.Major
ID 前缀检测内容检测方式严重程度
PERF
读取-修改-写入中的竞态条件读取值、修改值并写回但未加锁的模式:并发场景下的计数器递增、余额更新、标志位切换。严重
PERF
非幂等队列任务未检查重复执行的队列/任务处理器。创建资源时未检查是否已存在的任务。任务创建的数据缺失唯一约束。主要
PERF
异步上下文内的共享可变状态异步处理器中访问的全局/模块级可变变量,存储在模块作用域中的请求级数据。主要

Frontend Performance

前端性能

ID PrefixWhat to DetectHow to DetectSeverity
PERF
Unnecessary re-rendersMissing
React.memo
,
useMemo
,
useCallback
on expensive computations or components receiving new object/array references on every render. Objects/arrays created inline in JSX props.
Minor
PERF
Large bundle imports
import _ from 'lodash'
(imports entire library),
import moment from 'moment'
(large library where
date-fns
or
dayjs
suffice),
import * as icons from 'icon-library'
.
Minor
PERF
Missing lazy loadingNo
React.lazy()
/ dynamic
import()
for route-level code splitting. Heavy components loaded eagerly on initial page load.
Minor
ID 前缀检测内容检测方式严重程度
PERF
不必要的重渲染昂贵计算或接收新对象/数组引用的组件未使用
React.memo
useMemo
useCallback
。JSX props中内联创建对象/数组。
次要
PERF
大体积包导入
import _ from 'lodash'
(导入整个库)、
import moment from 'moment'
(大型库,使用
date-fns
dayjs
即可满足需求)、
import * as icons from 'icon-library'
次要
PERF
缺失懒加载路由级代码拆分未使用
React.lazy()
/ 动态
import()
。初始页面加载时预加载重型组件。
次要

Optional Script Integration

可选脚本集成

When
scripts/complexity_scorer.py
output is available (run by the orchestrator during
/codeprobe health
or
/codeprobe audit
), use it to identify high-complexity functions as performance hot-spot candidates. Functions rated "high" or "very_high" that also appear in hot paths (request handlers, loop bodies, frequently-called utilities) are strong signals for algorithmic efficiency findings.

scripts/complexity_scorer.py
的输出可用时(由编排器在
/codeprobe health
/codeprobe audit
期间运行),使用它识别高复杂度函数作为性能热点候选。被评为“high”或“very_high”且出现在热点路径(请求处理器、循环体、频繁调用的工具函数)中的函数是算法效率问题的强烈信号。

ID Prefix & Fix Prompt Examples

ID前缀与修复提示示例

All findings use the
PERF-
prefix, numbered sequentially:
PERF-001
,
PERF-002
, etc.
所有检测结果使用
PERF-
前缀,并按顺序编号:
PERF-001
PERF-002
等。

Fix Prompt Examples

修复提示示例

  • "In
    OrderController@index
    (line 22), add
    ->with('items', 'customer')
    to the
    Order::query()
    call to fix the N+1 problem — currently loading 2 relations lazily inside the Blade loop at
    orders/index.blade.php:15
    ."
  • "Replace
    Product::all()
    at line 30 of
    CatalogService.php
    with
    Product::query()->paginate(25)
    or
    Product::cursor()
    if processing all records. The current query loads all products into memory."
  • "In
    ReportGenerator@aggregate
    (lines 45-60), the nested loop iterating
    $orders
    inside
    $customers
    is O(n*m). Build a lookup hashmap before the outer loop:
    $ordersByCustomerId = collect($orders)->groupBy('customer_id')
    ."
  • "Replace
    import _ from 'lodash'
    at line 3 of
    src/utils/helpers.ts
    with specific imports:
    import debounce from 'lodash/debounce'
    to reduce bundle size."
  • "在
    OrderController@index
    (第22行),向
    Order::query()
    调用添加
    ->with('items', 'customer')
    以修复N+1问题——当前在
    orders/index.blade.php:15
    的Blade循环内延迟加载2个关联关系。"
  • "将
    CatalogService.php
    第30行的
    Product::all()
    替换为
    Product::query()->paginate(25)
    ;如果需要处理所有记录,可使用
    Product::cursor()
    。当前查询会将所有产品加载到内存中。"
  • "在
    ReportGenerator@aggregate
    (第45-60行),嵌套循环在
    $customers
    内遍历
    $orders
    的复杂度为O(n*m)。在外层循环前构建查找哈希表:
    $ordersByCustomerId = collect($orders)->groupBy('customer_id')
    。"
  • "将
    src/utils/helpers.ts
    第3行的
    import _ from 'lodash'
    替换为按需导入:
    import debounce from 'lodash/debounce'
    以减小包体积。"