Skip to content

perf(pi-extension): exact .ts specifiers cut Pi load from 43.5ms to 15ms#1106

Merged
backnotprop merged 2 commits into
backnotprop:mainfrom
dca123:perf/pi-extension-ts-specifiers
Jul 22, 2026
Merged

perf(pi-extension): exact .ts specifiers cut Pi load from 43.5ms to 15ms#1106
backnotprop merged 2 commits into
backnotprop:mainfrom
dca123:perf/pi-extension-ts-specifiers

Conversation

@dca123

@dca123 dca123 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Problem

The Pi extension is distributed and executed as raw TypeScript (pi.extensionsindex.ts, noEmit) — but its relative import specifiers mix phantom .js paths with extensionless paths. The former name files that never exist in this package; both require jiti to probe instead of resolving the exact file.

Pi's loader (jiti) supports the .js.ts remap, but only as its last-resort fallback — after two full ESM resolution cascades and three constructed ERR_MODULE_NOT_FOUNDs per import:

jiti 2.7 esmResolve, same file per resolution
"./config.js" (phantom) ~1,809 µs
"./config.ts" (real file) ~20 µs

Multiplied across the eager import graph it cost me around that's ~26 ms of every Pi session start.

This never shows up in development because Bun does the same remap natively at no cost. startup.test.ts guards the eager-graph shape, but under the one runtime that doesn't exhibit the cost.

Measured result

Extension module import (PI_TIMING=1, n=12 interleaved runs, same machine):

median
main (.js + extensionless) 43.5 ms
this branch (exact .ts) 15 ms

What changed

  1. Hand-written sources: mechanical rewrite of relative .js and extensionless specifiers to exact .ts paths. The tsconfig already fully permits this (moduleResolution: "bundler", noEmit, allowImportingTsExtensions) — zero config changes.
  2. vendor.sh: rewrite rules now emit .ts, plus a final normalization pass over generated/ for both .js and extensionless source styles — the monorepo conventions are untouched. It covers import/re-export, dynamic import, and side-effect import forms while leaving bare and explicit non-TypeScript specifiers alone.
  3. import-specifiers.test.ts: guards the invariant so future contributions in either style stay correct.

Validation

  • bun test — 73/73 (apps/pi-extension), 456/456 (packages/server, which imports these files)
  • all seven TypeScript project checks pass
  • packed npm artifact audit: 87 generated TypeScript files; all 327 relative specifier occurrences exact; all .ts targets exist
  • Scope check: generated/ and the pi-extension sources are consumed only by this package's runtimes (jiti/Bun); the hook, OpenCode, VS Code, and web builds import packages/* directly and see no changed bytes

@dca123
dca123 force-pushed the perf/pi-extension-ts-specifiers branch from cf8fdf9 to 347ac0d Compare July 22, 2026 02:40
@dca123 dca123 changed the title perf(pi-extension): .ts relative specifiers — 42ms → 16ms extension load under Pi's jiti loader perf(pi-extension): .ts relative specifiers improve extenstion load under Pi's jiti loader Jul 22, 2026
@dca123
dca123 marked this pull request as ready for review July 22, 2026 04:19
The Pi extension is distributed and executed as raw TypeScript through
Pi's jiti loader. Phantom .js paths force jiti through its last-resort
fallback, while extensionless paths still require probing. Exact .ts
paths avoid both and match the files that actually ship.

- rewrite relative .js and extensionless specifiers in hand-written
  sources (the existing noEmit bundler tsconfig permits .ts imports)
- make vendor.sh emit .ts in explicit rewrite rules and normalize both
  source styles across verbatim-copied generated modules
- guard hand-written and generated sources against either inexact form

Extension module import: 43.5ms -> 15ms median (PI_TIMING, n=12
interleaved). bun test: 73/73 (pi-extension), 456/456 (server); all
seven TypeScript project checks and the packed-artifact audit pass.
@dca123
dca123 force-pushed the perf/pi-extension-ts-specifiers branch from 347ac0d to e20eef8 Compare July 22, 2026 04:23
@dca123
dca123 marked this pull request as draft July 22, 2026 04:24
@dca123 dca123 changed the title perf(pi-extension): .ts relative specifiers improve extenstion load under Pi's jiti loader perf(pi-extension): exact .ts specifiers cut Pi load from 43.5ms to 15ms Jul 22, 2026
@dca123
dca123 marked this pull request as ready for review July 22, 2026 05:07
@backnotprop
backnotprop merged commit 929e8f9 into backnotprop:main Jul 22, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants