@thestraylight/so-hot-right-now 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +163 -2
- package/bin/so-hot-right-now.mjs +9 -0
- package/docs/plugins.md +77 -0
- package/package.json +62 -4
- package/plugins/so-hot-right-now/.claude-plugin/plugin.json +16 -0
- package/plugins/so-hot-right-now/skills/hot-paths/SKILL.md +91 -0
- package/plugins/so-hot-right-now/skills/hot-paths/rules.md +56 -0
- package/plugins/so-hot-right-now/skills/hot-paths-setup/SKILL.md +102 -0
- package/src/baseline.mjs +41 -0
- package/src/checker.mjs +388 -0
- package/src/cli.mjs +171 -0
- package/src/config.mjs +68 -0
- package/src/helpers.d.ts +32 -0
- package/src/helpers.mjs +23 -0
- package/src/index.d.ts +151 -0
- package/src/index.mjs +11 -0
- package/src/options.mjs +85 -0
- package/src/rules/browser.mjs +108 -0
- package/src/rules/core.mjs +247 -0
- package/src/rules/frameworks.mjs +18 -0
- package/src/rules/webgpu.mjs +47 -0
- package/src/syntax.mjs +237 -0
- package/src/types.mjs +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Glenn Conner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
#
|
|
1
|
+
# so-hot-right-now
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript checker for code that runs on each frame or tick.
|
|
4
|
+
|
|
5
|
+
The checker finds allocations, closures, string compares, and hash lookups in hot code. These cause garbage collection pauses and frame spikes in games, simulations, renderers, and audio code.
|
|
6
|
+
It reads the TypeScript types and follows each resolved call from your engine's entry points. You do not need to mark every function by hand.
|
|
7
|
+
|
|
8
|
+
```text
|
|
9
|
+
src/physics/step.ts: 2 "object" problem(s), 0 allowed. Do not create an object literal in a hot path. Reuse a preallocated object or write to typed arrays.
|
|
10
|
+
src/physics/step.ts:41:12 (hot via PhysicsSystem.update > integrate > contact)
|
|
11
|
+
src/physics/step.ts:58:20 (hot via PhysicsSystem.update > integrate)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The package also gives agent skills, so that a coding agent can write hot code, fix problems, and set up the checker.
|
|
15
|
+
|
|
16
|
+
## How it works
|
|
17
|
+
|
|
18
|
+
1. You give the **roots**: the methods and callbacks that your engine calls on each frame or tick.
|
|
19
|
+
2. The checker follows each resolved call, getter, setter, and interface implementation from the roots.
|
|
20
|
+
3. Each function that it reaches must have a tag:
|
|
21
|
+
- `/** @hot */`: the function obeys the rules. The checker checks it and follows its calls.
|
|
22
|
+
- `/** @hotIgnore <reason> */`: the function may break the rules. The walk stops there. The reason says when it runs or what it costs.
|
|
23
|
+
4. A **baseline** file counts the existing problems for each file. New problems fail the check, so you can add the checker to a large project.
|
|
24
|
+
|
|
25
|
+
The tags show which code is hot. A reviewer can see that a function runs on each frame without a trace in a profiler.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
The npm name is `@thestraylight/so-hot-right-now`, and the command is `so-hot-right-now`.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
pnpm add -D @thestraylight/so-hot-right-now
|
|
33
|
+
npx so-hot-right-now init
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`init` writes `hot-path.config.mjs`. Edit its roots, then record a baseline:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
npx so-hot-right-now report # Show each problem with its call chain.
|
|
40
|
+
npx so-hot-right-now baseline # Write the current counts to hot-path-baseline.json.
|
|
41
|
+
npx so-hot-right-now # Check against the baseline. Put this in your lint script.
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Node 22.18 or newer is necessary.
|
|
45
|
+
The package includes TypeScript 6, because TypeScript 7 has no compiler API. Your project can still compile with TypeScript 7 or `tsgo`.
|
|
46
|
+
|
|
47
|
+
## Config
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
// hot-path.config.mjs
|
|
51
|
+
import { defineConfig, presets } from "@thestraylight/so-hot-right-now";
|
|
52
|
+
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
tsconfig: "tsconfig.json",
|
|
55
|
+
baseline: "hot-path-baseline.json",
|
|
56
|
+
include: ["src/**"],
|
|
57
|
+
// Every function in these files is a root, and the baseline accepts no problems in them.
|
|
58
|
+
strict: ["src/engine/core/**"],
|
|
59
|
+
plugins: [presets.browser, presets.webgpu, presets.react],
|
|
60
|
+
roots: [
|
|
61
|
+
{ kind: "method", owner: { implements: "System" }, methods: ["update"], label: "simulation" },
|
|
62
|
+
{ kind: "method", owner: { name: /Renderer$/ }, methods: ["draw"], label: "frame" },
|
|
63
|
+
{ kind: "argument", callee: "useFrame", index: 0, label: "frame" },
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The paths are relative to the config file's folder. The default `exclude` skips `node_modules` and test files.
|
|
69
|
+
|
|
70
|
+
### Roots
|
|
71
|
+
|
|
72
|
+
| Kind | Finds |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| `method` | A method or a function property on a class. The `owner` is `{ implements }`, `{ extends }`, or `{ name }` (a string or a RegExp). |
|
|
75
|
+
| `argument` | The function that the code gives to a call, such as `useFrame(callback)`. |
|
|
76
|
+
| `scheduler` | A callback that gives itself to a call from its own body, such as a `requestAnimationFrame` loop. |
|
|
77
|
+
|
|
78
|
+
For a root that a matcher cannot find, such as a worker message handler, tag the function `/** @hot <caller> */`.
|
|
79
|
+
|
|
80
|
+
### Presets
|
|
81
|
+
|
|
82
|
+
| Preset | Adds |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| `browser` | The `layout` rule for DOM layout reads. A root for each self-scheduling `requestAnimationFrame` callback. |
|
|
85
|
+
| `webgpu` | The `gpu` rule for WebGPU create calls, such as `createBindGroup` and `createView`. |
|
|
86
|
+
| `react` | Lets a tag on `const f = useCallback(...)` apply to the function. |
|
|
87
|
+
| `audioWorklet` | A root for `process` on each `AudioWorkletProcessor` class. |
|
|
88
|
+
|
|
89
|
+
The core rules always apply. [`plugins/so-hot-right-now/skills/hot-paths/rules.md`](plugins/so-hot-right-now/skills/hot-paths/rules.md) lists each rule and its fix.
|
|
90
|
+
|
|
91
|
+
### Allowed lines
|
|
92
|
+
|
|
93
|
+
A `// hot-path: allow <reason>` comment on the line above a problem accepts that one problem. Use it only when a platform API must allocate. The reason is necessary.
|
|
94
|
+
|
|
95
|
+
## Plugins
|
|
96
|
+
|
|
97
|
+
A plugin adds rules that only your project needs. For example, `examples/ecs-plugin.mjs` reports a world write from render code. Read [docs/plugins.md](docs/plugins.md).
|
|
98
|
+
|
|
99
|
+
## Agent skills
|
|
100
|
+
|
|
101
|
+
The package has two skills in the [Agent Skills](https://agentskills.io) format. Claude Code and Codex both read this format.
|
|
102
|
+
|
|
103
|
+
- `hot-paths`: write and fix hot code. It explains the tags, the fix for each problem, and the baseline.
|
|
104
|
+
- `hot-paths-setup`: add the checker to a project. It finds the roots, writes the config, and records the baseline.
|
|
105
|
+
|
|
106
|
+
### Copy the skills into the project
|
|
107
|
+
|
|
108
|
+
Use this way for a team. The skills go into version control, so each contributor and each agent gets the same version.
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
npx so-hot-right-now skills
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The command copies the skills to these folders:
|
|
115
|
+
|
|
116
|
+
| Agent | Folder |
|
|
117
|
+
| --- | --- |
|
|
118
|
+
| Claude Code | `.claude/skills/` |
|
|
119
|
+
| Codex, and other Agent Skills tools | `.agents/skills/` |
|
|
120
|
+
|
|
121
|
+
Add `--target claude` or `--target codex` for one agent only, or give a folder. Add `--global` to copy into `~/.claude/skills/` and `~/.agents/skills/`.
|
|
122
|
+
Run the command again after you update the package. It replaces the old copies.
|
|
123
|
+
|
|
124
|
+
### Install the plugin
|
|
125
|
+
|
|
126
|
+
Use this way for one person. The plugin gives the same skills with the `so-hot-right-now:` prefix.
|
|
127
|
+
|
|
128
|
+
Claude Code:
|
|
129
|
+
|
|
130
|
+
```text
|
|
131
|
+
/plugin marketplace add mrsharpoblunto/so-hot-right-now
|
|
132
|
+
/plugin install so-hot-right-now@so-hot-right-now
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Codex:
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
codex plugin marketplace add mrsharpoblunto/so-hot-right-now
|
|
139
|
+
codex plugin add so-hot-right-now@so-hot-right-now
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Codex reads the Claude Code marketplace file in `.claude-plugin/`, so one manifest serves both agents.
|
|
143
|
+
|
|
144
|
+
## API
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { checkHotPaths, compareWithBaseline, createProgram, presets } from "@thestraylight/so-hot-right-now";
|
|
148
|
+
|
|
149
|
+
const program = createProgram("tsconfig.json");
|
|
150
|
+
const problems = checkHotPaths(program, { root: process.cwd(), plugins: [presets.browser] });
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`checkHotPaths` takes a TypeScript `Program`, so you can run it from your own lint script and share one program with other type-aware checks.
|
|
154
|
+
Make the program with `createProgram` from this package, or with TypeScript 6. A program from a different TypeScript version can have different syntax kind numbers.
|
|
155
|
+
|
|
156
|
+
## Limits
|
|
157
|
+
|
|
158
|
+
- The checker reads only TypeScript and JavaScript that the `tsconfig.json` includes.
|
|
159
|
+
- It follows calls that the type checker can resolve. A call through an `any` value or a computed key stops the walk.
|
|
160
|
+
- It reads the project with TypeScript 6. A project that needs a TypeScript 7 language feature can get different types.
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
MIT
|
package/docs/plugins.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Plugins
|
|
2
|
+
|
|
3
|
+
A plugin adds rules, roots, or function hooks to the checker. The presets are plugins.
|
|
4
|
+
Write a project plugin for a rule that only your project needs.
|
|
5
|
+
|
|
6
|
+
## Shape
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
export const myPlugin = {
|
|
10
|
+
name: "my-plugin",
|
|
11
|
+
// The message for each problem id. The baseline uses the ids, so keep them stable.
|
|
12
|
+
messages: { slowCall: "Do not call `slowCall` in a hot path." },
|
|
13
|
+
// More roots. They have the same format as the `roots` in the config.
|
|
14
|
+
roots: [],
|
|
15
|
+
// Calls that return the function that they receive, such as `useCallback`.
|
|
16
|
+
functionHooks: [],
|
|
17
|
+
// Makes the hooks for one check.
|
|
18
|
+
create(program) {
|
|
19
|
+
const checker = program.getTypeChecker();
|
|
20
|
+
return {
|
|
21
|
+
checkFunction(fn, context) {},
|
|
22
|
+
checkNode(node, context) {},
|
|
23
|
+
checkSourceFile(sourceFile, context) {},
|
|
24
|
+
finish(graph, context) {},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Each field except `name` is optional. Two plugins cannot use the same message id.
|
|
31
|
+
Give your ids a prefix if you publish the plugin, for example `myPluginSlowCall`.
|
|
32
|
+
|
|
33
|
+
## Hooks
|
|
34
|
+
|
|
35
|
+
The checker calls the hooks in this order:
|
|
36
|
+
|
|
37
|
+
1. `checkSourceFile(sourceFile, context)` runs once for each project file, hot or not. Use it for a rule about module structure.
|
|
38
|
+
2. `checkFunction(fn, context)` runs once for each hot function. Use it for a rule about the signature.
|
|
39
|
+
3. `checkNode(node, context)` runs for each node in the body of a hot function. It does not run in a nested function, because the core reports the closure.
|
|
40
|
+
4. `finish(graph, context)` runs once after the walk. Use it for a rule that needs the call graph.
|
|
41
|
+
|
|
42
|
+
The checker does not enter type nodes, `throw` statements, or `catch` blocks.
|
|
43
|
+
|
|
44
|
+
## Context
|
|
45
|
+
|
|
46
|
+
- `context.checker` is the TypeScript type checker.
|
|
47
|
+
- `context.report(node, messageId)` records a problem. A `// hot-path: allow <reason>` line above the node accepts it.
|
|
48
|
+
- `context.isStrict` is `true` in a strict file.
|
|
49
|
+
- In `checkFunction` and `checkNode`, `context.owner` is the hot function, and `context.chain` is its call chain from a root.
|
|
50
|
+
- In `finish`, `context.report(node, messageId, chain)` takes the chain, and `context.isStrict(sourceFile)` is a function.
|
|
51
|
+
|
|
52
|
+
## The call graph
|
|
53
|
+
|
|
54
|
+
`finish` gets a graph with these fields:
|
|
55
|
+
|
|
56
|
+
- `roots`: a map from each label to its root functions. The checker adds the label `tagged` for `@hot <caller>` roots and `strict` for the roots in strict files.
|
|
57
|
+
- `callees`: a map from each hot function to the functions that it calls. It includes calls to `@hotIgnore` functions.
|
|
58
|
+
- `chain(fn)`: the call chain from a root to a hot function.
|
|
59
|
+
|
|
60
|
+
## State
|
|
61
|
+
|
|
62
|
+
`create` runs once for each check. Keep the state of a check in its closure, so that two checks do not share it.
|
|
63
|
+
|
|
64
|
+
## Helpers
|
|
65
|
+
|
|
66
|
+
`@thestraylight/so-hot-right-now/helpers` exports the type and syntax tests that the presets use. Some examples:
|
|
67
|
+
|
|
68
|
+
- `calledMethod(callee)` gives the method name of `a.b()` or `a["b"]()`.
|
|
69
|
+
- `isNamedType(type, name)` finds the name in a union or a type parameter constraint.
|
|
70
|
+
- `isLibraryDeclaration(declaration)` is `true` for a built-in from a `.d.ts` file.
|
|
71
|
+
|
|
72
|
+
## Example
|
|
73
|
+
|
|
74
|
+
`examples/ecs-plugin.mjs` in the repository has two project rules:
|
|
75
|
+
|
|
76
|
+
- `query`: a `defineQuery` call inside a function, in any file.
|
|
77
|
+
- `worldWrite`: a `World` write that a `frame` or `tagged` root reaches without a `simulation` root between them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thestraylight/so-hot-right-now",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A TypeScript checker that finds allocations, closures, string compares, and hash lookups in code that runs on each frame or tick.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"lint",
|
|
8
|
+
"performance",
|
|
9
|
+
"hot-path",
|
|
10
|
+
"allocation",
|
|
11
|
+
"garbage-collection",
|
|
12
|
+
"game",
|
|
13
|
+
"agent-skills"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Glenn Conner",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/mrsharpoblunto/so-hot-right-now.git"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"packageManager": "pnpm@11.5.2",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22.18"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./src/index.d.ts",
|
|
29
|
+
"default": "./src/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./helpers": {
|
|
32
|
+
"types": "./src/helpers.d.ts",
|
|
33
|
+
"default": "./src/helpers.mjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"bin": {
|
|
38
|
+
"so-hot-right-now": "bin/so-hot-right-now.mjs"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"bin",
|
|
42
|
+
"src",
|
|
43
|
+
"docs",
|
|
44
|
+
"plugins"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"lint": "oxlint --deny-warnings && pnpm format --check",
|
|
49
|
+
"format": "oxfmt '**/*.{mjs,ts,mts}'",
|
|
50
|
+
"prepublishOnly": "pnpm test && node scripts/check-version.mjs"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"picomatch": "^4.0.7",
|
|
54
|
+
"typescript": "^6.0.3"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"oxfmt": "^0.80.0",
|
|
58
|
+
"oxlint": "^1.86.0",
|
|
59
|
+
"vitest": "^5.1.2"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "so-hot-right-now",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Skills to write, fix, and set up hot path checks for TypeScript code that runs on each frame or tick.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Glenn Conner"
|
|
7
|
+
},
|
|
8
|
+
"repository": "https://github.com/mrsharpoblunto/so-hot-right-now",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"typescript",
|
|
12
|
+
"performance",
|
|
13
|
+
"hot-path",
|
|
14
|
+
"lint"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hot-paths
|
|
3
|
+
description: Write and fix code that runs on each frame or tick in a project that uses the so-hot-right-now checker. Use when you change a function tagged `@hot` or `@hotIgnore`, when the checker reports a hot path problem, when you add a call from hot code, or when you lower the hot path baseline.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Hot paths
|
|
7
|
+
|
|
8
|
+
A hot path is code that runs on each simulation tick or each displayed frame.
|
|
9
|
+
Hot code must not make garbage or do slow lookups. Garbage collection pauses cause frame spikes.
|
|
10
|
+
|
|
11
|
+
The `so-hot-right-now` checker finds the hot code. It starts at the roots in `hot-path.config.mjs` and follows each resolved call.
|
|
12
|
+
Each function that it reaches must have a tag, and each tagged function must obey the rules.
|
|
13
|
+
|
|
14
|
+
## Commands
|
|
15
|
+
|
|
16
|
+
Find the command in the `package.json` scripts first. These are the direct commands:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
# Check against the baseline. The lint script usually runs this.
|
|
20
|
+
npx so-hot-right-now
|
|
21
|
+
|
|
22
|
+
# Show each problem whose path contains the text, with the call chain that makes it hot.
|
|
23
|
+
npx so-hot-right-now report src/physics/
|
|
24
|
+
|
|
25
|
+
# Write the current counts after you fix problems.
|
|
26
|
+
npx so-hot-right-now baseline
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Add `--format json` to get the problems as data.
|
|
30
|
+
|
|
31
|
+
## Tags
|
|
32
|
+
|
|
33
|
+
- `/** @hot */`: the function is fast enough to run on each tick or frame. The checker applies the rules to it and follows its calls.
|
|
34
|
+
- `/** @hot <caller> */`: the function is a root that the call graph cannot find, such as a worker message handler. Name the caller.
|
|
35
|
+
- `/** @hotIgnore <reason> */`: hot code calls the function, but the function may break the rules. The checker does not examine it or its calls. The reason says when it runs or what it costs.
|
|
36
|
+
|
|
37
|
+
Read a function before you tag it:
|
|
38
|
+
|
|
39
|
+
- Tag it `@hot` only when its work for each call is small and bounded.
|
|
40
|
+
- Do not tag a slow function `@hot` to make the check pass.
|
|
41
|
+
- Make a slow function faster, or tag it `@hotIgnore` and give its cost.
|
|
42
|
+
|
|
43
|
+
Use `@hotIgnore` for these functions:
|
|
44
|
+
|
|
45
|
+
- A function that runs on a rare event behind a guard in the hot caller. Examples are spawn, load, save, and a version-gated rebuild.
|
|
46
|
+
- A function that does more work than the tick needs, such as a search that a cache could replace. `git grep "@hotIgnore"` lists these candidates for a speed-up.
|
|
47
|
+
- Code that runs only while a developer records a trace. Guard each call with a flag check.
|
|
48
|
+
|
|
49
|
+
Put the tag on the function, or on the declaration that holds the function. A class tag or an object tag does not apply to its members.
|
|
50
|
+
A closure that hot code creates needs no tag, because the checker already reports the closure.
|
|
51
|
+
|
|
52
|
+
## Fixes
|
|
53
|
+
|
|
54
|
+
Read `rules.md` in this skill folder for the fix for each message id.
|
|
55
|
+
These are the most frequent fixes:
|
|
56
|
+
|
|
57
|
+
- `new`, `object`, `array`: allocate once at module scope or in the constructor. Reuse the object on each call.
|
|
58
|
+
- `closure`: make a method on a long-lived object, or write a loop.
|
|
59
|
+
- `allocation`: write into a reused buffer. Copy each field by hand.
|
|
60
|
+
- `string`, `compare`: use integer constants. Compute the integer once, when the data loads.
|
|
61
|
+
- `hash`: use an array indexed by a dense integer, such as an entity index.
|
|
62
|
+
- `iterator`: iterate an array or a typed array with an index.
|
|
63
|
+
- `untagged`: read the function, then tag it `@hot` or `@hotIgnore <reason>`.
|
|
64
|
+
- `staleHot`, `staleIgnore`: remove the tag. No hot path calls the function now.
|
|
65
|
+
|
|
66
|
+
Code inside a `throw` statement or a `catch` block is not hot. It runs only on a failure.
|
|
67
|
+
|
|
68
|
+
## Allowed lines
|
|
69
|
+
|
|
70
|
+
A `// hot-path: allow <reason>` comment on the line above a problem accepts that one problem.
|
|
71
|
+
|
|
72
|
+
- Use it only when a platform API must allocate, for example a mapped GPU read.
|
|
73
|
+
- Give the reason. The checker reports an allow comment without a reason.
|
|
74
|
+
- Do not use it to skip a fix that you can make.
|
|
75
|
+
|
|
76
|
+
## The baseline
|
|
77
|
+
|
|
78
|
+
The baseline file records the problems that existed when the project started the check. It counts each message for each file.
|
|
79
|
+
|
|
80
|
+
- The check fails when a count increases. Fix the new problem. Do not raise the count.
|
|
81
|
+
- The check fails when a count decreases. Run the `baseline` command so that the count cannot increase again.
|
|
82
|
+
- The baseline never accepts a problem in a strict file. The config lists the strict files.
|
|
83
|
+
- Do not edit the baseline file by hand.
|
|
84
|
+
|
|
85
|
+
## Workflow
|
|
86
|
+
|
|
87
|
+
1. Run the check. Read each problem and its call chain.
|
|
88
|
+
2. Read the function at the end of the chain before you change it.
|
|
89
|
+
3. Fix the problem in the hot function, or move the slow work behind a guard into a `@hotIgnore` function.
|
|
90
|
+
4. Run the check again until it passes.
|
|
91
|
+
5. If counts went down, run the `baseline` command and commit the baseline with the fix.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Hot path rules
|
|
2
|
+
|
|
3
|
+
Each section is a message id. The baseline file uses these ids, so they do not change.
|
|
4
|
+
|
|
5
|
+
## Core
|
|
6
|
+
|
|
7
|
+
These rules apply in each project.
|
|
8
|
+
|
|
9
|
+
| Id | Problem | Fix |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `new` | A `new` expression. | Allocate once and reuse the object. |
|
|
12
|
+
| `object` | An object literal or an object spread. | Reuse a preallocated object, or write to typed arrays. |
|
|
13
|
+
| `array` | An array literal or an array spread. | Reuse a preallocated array. |
|
|
14
|
+
| `closure` | A function made inside a hot function. | Use a loop, or a method on a long-lived object. |
|
|
15
|
+
| `allocation` | A built-in that returns a new value, such as `map`, `filter`, `slice`, `Object.keys`, or `toFixed`. | Write into a reused buffer. Copy each field by hand. |
|
|
16
|
+
| `string` | A template literal or a `+` with a string. | Use numeric keys or indexes. |
|
|
17
|
+
| `compare` | A string comparison, a string `switch`, or a string method such as `startsWith` or `test`. | Use an integer enum. Compute it once when the data loads. |
|
|
18
|
+
| `hash` | A `Map` or `Set` call, a string-keyed lookup, or `in`. | Use an array indexed by a dense integer. |
|
|
19
|
+
| `iterator` | A `for...of` on a value that is not an array. | Iterate an array or a typed array with an index. |
|
|
20
|
+
| `spread` | An argument spread, such as `f(...values)`. | Pass fixed arguments. |
|
|
21
|
+
| `rest` | A rest parameter or a rest element in a destructuring pattern. | Pass fixed arguments, and read the fields that you need. |
|
|
22
|
+
| `generator` | A generator function. | Iterate arrays with an index. |
|
|
23
|
+
| `async` | An async function. Each call makes a promise. | Reuse a promise, or accept the line with a `hot-path: allow` reason. |
|
|
24
|
+
| `regex` | A regex literal. Each evaluation makes a new `RegExp`. | Move it to module scope. |
|
|
25
|
+
| `promise` | A `then`, `catch`, or `finally` call on a promise. | Do not chain promises in hot code. |
|
|
26
|
+
| `console` | A `console` call. | Remove it, or put it behind a debug flag in a `@hotIgnore` function. |
|
|
27
|
+
| `delete` | A `delete` expression. The object goes into slow dictionary mode. | Set the property to a sentinel value. |
|
|
28
|
+
| `arguments` | A read of `arguments`. | Use named parameters. |
|
|
29
|
+
| `numberKey` | A plain object used as an integer-keyed map. | Use an array. |
|
|
30
|
+
| `mixed` | An array of numbers that also holds `null` or `undefined`. The engine loses the packed number storage. | Use a typed array with a sentinel value. |
|
|
31
|
+
|
|
32
|
+
## Tag rules
|
|
33
|
+
|
|
34
|
+
| Id | Problem | Fix |
|
|
35
|
+
| --- | --- | --- |
|
|
36
|
+
| `untagged` | A hot path reaches a function that has no tag. | Read it. Tag it `@hot`, or `@hotIgnore <reason>`. |
|
|
37
|
+
| `reason` | A `@hotIgnore` tag without a reason. | Say when the function runs or what it costs. |
|
|
38
|
+
| `staleHot` | A `@hot` tag on a function that no hot path calls. | Remove the tag, or name the caller after `@hot` if it is a root. |
|
|
39
|
+
| `staleIgnore` | A `@hotIgnore` tag on a function that no hot code calls. | Remove the tag. Strict files do not get this problem. |
|
|
40
|
+
| `misplaced` | A tag that is not on a function or on a declaration that holds a function. | Move the tag to the function. |
|
|
41
|
+
| `allow` | A `hot-path: allow` comment without a reason. | Name the API that must allocate. |
|
|
42
|
+
|
|
43
|
+
## Presets
|
|
44
|
+
|
|
45
|
+
A project adds these rules with a preset in `hot-path.config.mjs`.
|
|
46
|
+
|
|
47
|
+
| Preset | Id | Problem | Fix |
|
|
48
|
+
| --- | --- | --- | --- |
|
|
49
|
+
| `browser` | `layout` | A DOM layout read, such as `getBoundingClientRect` or `offsetWidth`. The browser computes the layout in the middle of a frame. | Read it on a resize or an event, and keep the value. |
|
|
50
|
+
| `webgpu` | `gpu` | A WebGPU create call, such as `createBindGroup`, `createView`, or `createBuffer`. | Create the object once and reuse it. A command encoder and a pass are made each frame. |
|
|
51
|
+
|
|
52
|
+
The `browser` preset also makes a root of each `requestAnimationFrame` callback that schedules itself again.
|
|
53
|
+
The `react` preset lets a tag on `const f = useCallback(...)` apply to the function.
|
|
54
|
+
The `audioWorklet` preset makes a root of `process` on each `AudioWorkletProcessor` class.
|
|
55
|
+
|
|
56
|
+
A project plugin can add more ids. Read the project's `hot-path.config.mjs` to find them.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hot-paths-setup
|
|
3
|
+
description: Add the so-hot-right-now hot path checker to a TypeScript project. Find the code that runs on each frame or tick, write hot-path.config.mjs with its roots and presets, tag the reached functions, and record a baseline. Use when the user asks to set up, adopt, or configure hot path checks, or to add roots or a project plugin.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Set up the hot path checker
|
|
7
|
+
|
|
8
|
+
The checker finds allocations and slow lookups in code that runs on each frame or tick.
|
|
9
|
+
It needs roots: the functions that the engine calls on each frame or tick.
|
|
10
|
+
It follows each call from a root, so the roots decide which code it checks.
|
|
11
|
+
|
|
12
|
+
## 1. Install
|
|
13
|
+
|
|
14
|
+
Install the package as a dev dependency with the project's package manager. It includes its own TypeScript 6, so the project can compile with any TypeScript version.
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pnpm add -D @thestraylight/so-hot-right-now
|
|
18
|
+
npx so-hot-right-now init
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`init` writes a starter `hot-path.config.mjs` in the current folder. Put it in the folder that holds `tsconfig.json`.
|
|
22
|
+
|
|
23
|
+
## 2. Find the roots
|
|
24
|
+
|
|
25
|
+
Read the engine loop before you write roots. Search for these entry points:
|
|
26
|
+
|
|
27
|
+
- A `requestAnimationFrame` loop. The `browser` preset finds a callback that schedules itself.
|
|
28
|
+
- An update, tick, step, or render method that the loop calls on each object of a class or an interface.
|
|
29
|
+
- A hook that takes a per-frame callback, such as `useFrame` in react-three-fiber.
|
|
30
|
+
- A worker `onmessage` handler that runs for each frame or tick.
|
|
31
|
+
- An `AudioWorkletProcessor.process` method. The `audioWorklet` preset finds it.
|
|
32
|
+
|
|
33
|
+
Write one root for each entry point:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
roots: [
|
|
37
|
+
// Each class that implements `System` has its `update` called on each tick.
|
|
38
|
+
{ kind: "method", owner: { implements: "System" }, methods: ["update"], label: "simulation" },
|
|
39
|
+
// Each class that extends `Entity` has its `tick` called on each tick.
|
|
40
|
+
{ kind: "method", owner: { extends: "Entity" }, methods: ["tick"], label: "simulation" },
|
|
41
|
+
// A class name pattern.
|
|
42
|
+
{ kind: "method", owner: { name: /Renderer$/ }, methods: ["draw"], label: "frame" },
|
|
43
|
+
// The callback that the code gives as argument 0 of `useFrame`.
|
|
44
|
+
{ kind: "argument", callee: "useFrame", index: 0, label: "frame" },
|
|
45
|
+
],
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The `owner` names match the text in the `implements` or `extends` clause, or the class name.
|
|
49
|
+
A label groups roots. A project plugin can read the labels.
|
|
50
|
+
For a root that a matcher cannot find, tag the function `/** @hot <caller> */` and name the caller.
|
|
51
|
+
|
|
52
|
+
## 3. Pick the presets and the strict files
|
|
53
|
+
|
|
54
|
+
- Add `presets.browser` for DOM code. Add `presets.webgpu` for WebGPU code.
|
|
55
|
+
- Add `presets.react` when the code tags functions made with `useCallback`.
|
|
56
|
+
- Add `presets.audioWorklet` for audio worklet processors.
|
|
57
|
+
- Put a folder in `strict` when each of its functions must obey the rules, such as an engine core.
|
|
58
|
+
|
|
59
|
+
## 4. Run and tag
|
|
60
|
+
|
|
61
|
+
1. Run `npx so-hot-right-now report`. The first run usually reports many `untagged` problems.
|
|
62
|
+
2. Read each untagged function. Tag it `@hot` if its work for each call is small and bounded.
|
|
63
|
+
3. Tag a function `@hotIgnore <reason>` if it runs on a rare event or does slow work. Give the cost in the reason.
|
|
64
|
+
4. Do not tag a slow function `@hot` to make the report shorter.
|
|
65
|
+
5. Run the report again. Tags on new functions find more calls, so repeat until no `untagged` problem stays.
|
|
66
|
+
|
|
67
|
+
Use the `hot-paths` skill for the meaning of each tag and the fix for each problem.
|
|
68
|
+
|
|
69
|
+
## 5. Record the baseline
|
|
70
|
+
|
|
71
|
+
Run `npx so-hot-right-now baseline`. It writes the current problem counts to `hot-path-baseline.json`.
|
|
72
|
+
Commit the config and the baseline. After this, the check fails when a count goes up or down.
|
|
73
|
+
|
|
74
|
+
## 6. Add the skills to the project
|
|
75
|
+
|
|
76
|
+
Run `npx so-hot-right-now skills`. It copies the skills to `.claude/skills/` for Claude Code and to `.agents/skills/` for Codex.
|
|
77
|
+
Commit the copies, so that each agent that works in the project gets them.
|
|
78
|
+
|
|
79
|
+
## 7. Run it in the lint script
|
|
80
|
+
|
|
81
|
+
Add the check to the lint script, so that each pull request runs it:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
"scripts": {
|
|
85
|
+
"lint": "tsc --noEmit && so-hot-right-now"
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Then add these lines to the project's agent instructions file, such as `CLAUDE.md` or `AGENTS.md`:
|
|
90
|
+
|
|
91
|
+
```md
|
|
92
|
+
# Hot paths
|
|
93
|
+
|
|
94
|
+
Code that runs on each frame or tick must not allocate, create closures, compare strings, or use hash maps.
|
|
95
|
+
Use the `hot-paths` skill before you change a function tagged `@hot` or `@hotIgnore`.
|
|
96
|
+
Do not add problems to `hot-path-baseline.json`. Lower the counts when you fix problems.
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Project plugins
|
|
100
|
+
|
|
101
|
+
A plugin adds rules that only one project needs, such as "do not write to the world from render code".
|
|
102
|
+
Read `node_modules/@thestraylight/so-hot-right-now/docs/plugins.md` before you write a plugin.
|
package/src/baseline.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The baseline counts the existing problems for each file and message. A count may only fall.
|
|
2
|
+
// It never accepts a problem in a strict file.
|
|
3
|
+
|
|
4
|
+
function groupProblems(problems) {
|
|
5
|
+
const counts = {};
|
|
6
|
+
for (const problem of problems) {
|
|
7
|
+
counts[problem.file] ??= {};
|
|
8
|
+
counts[problem.file][problem.messageId] = (counts[problem.file][problem.messageId] ?? 0) + 1;
|
|
9
|
+
}
|
|
10
|
+
return counts;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function strictFiles(problems) {
|
|
14
|
+
return new Set(problems.filter((problem) => problem.strict).map((problem) => problem.file));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function countProblems(problems) {
|
|
18
|
+
const counts = groupProblems(problems);
|
|
19
|
+
const strict = strictFiles(problems);
|
|
20
|
+
const sorted = {};
|
|
21
|
+
for (const file of Object.keys(counts).sort()) if (!strict.has(file)) sorted[file] = counts[file];
|
|
22
|
+
return sorted;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function compareWithBaseline(problems, baseline) {
|
|
26
|
+
const counts = groupProblems(problems);
|
|
27
|
+
const strict = strictFiles(problems);
|
|
28
|
+
const grown = [];
|
|
29
|
+
const stale = [];
|
|
30
|
+
for (const [file, messages] of Object.entries(counts))
|
|
31
|
+
for (const [messageId, count] of Object.entries(messages)) {
|
|
32
|
+
const allowed = strict.has(file) ? 0 : (baseline[file]?.[messageId] ?? 0);
|
|
33
|
+
if (count > allowed) grown.push({ file, messageId, count, allowed });
|
|
34
|
+
}
|
|
35
|
+
for (const [file, messages] of Object.entries(baseline))
|
|
36
|
+
for (const [messageId, allowed] of Object.entries(messages)) {
|
|
37
|
+
const count = counts[file]?.[messageId] ?? 0;
|
|
38
|
+
if (count < allowed) stale.push({ file, messageId, count, allowed });
|
|
39
|
+
}
|
|
40
|
+
return { grown, stale };
|
|
41
|
+
}
|