Skip to content

Conversation

@goldenice
Copy link
Contributor

@goldenice goldenice commented Jan 26, 2026

The public facing API / types should remain identical with this change, so I reckon we can do this as 1.1.3. On our internal benchmark project, this drops type instantations from about 1.59 million to 1.28-ish. I expect at least 555k to be non-farstorm related, as replacing InputType and OutputType with any drops to that number. This is with a bunch of typing errors on account of narrowing to never not working properly, so definitely could be higher in practice.

That's all measured with tsgo 7.x, not the 5.x tsc tool.

@goldenice
Copy link
Contributor Author

@greptileai review please?

@greptile-apps
Copy link

greptile-apps bot commented Jan 27, 2026

Greptile Summary

Caching strategy reduces TypeScript type instantiations by ~300k (from 1.59M to 1.28M) by introducing cached mapping types InputTypeByName and OutputTypeByName. The change replaces recursive type computations like OutputType<ED, EntityByName<ED, N>> with direct lookups via OutputTypeByName<ED>[N], allowing TypeScript to cache the result once per entity instead of recomputing it each time.

  • Created OutputTypeByName<ED> and InputTypeByName<ED> mapping types that compute all entity types upfront
  • Replaced all recursive OutputType<ED, EntityByName<...>> patterns with cached OutputTypeByName<ED>[...] lookups
  • Added OutputTypeRefByName helper to support cached Promise/non-Promise union types
  • Updated all public API signatures in DbFunctions<ED> to use cached types
  • The public-facing exports (InputType and OutputType) remain unchanged, maintaining full API compatibility

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is a pure TypeScript optimization that introduces caching of computed types. The runtime behavior is completely unchanged, and the public API remains identical (exports still include InputType and OutputType). The transformation is mechanical and systematic across all files, replacing recursive type computations with cached lookups. Performance improvement is substantial (~300k fewer type instantiations) with no breaking changes.
  • No files require special attention

Important Files Changed

Filename Overview
src/types/OutputType.ts Added OutputTypeByName mapping type and replaced recursive OutputType<ED, EntityByName<...>> calls with direct indexing via OutputTypeByName<ED>[...] to cache type computations
src/types/InputType.ts Added InputTypeByName mapping type and replaced OutputTypeRef<ED, EntityByName<...>> with OutputTypeRefByName<ED, ...> to leverage cached output types
src/main.ts Updated all function signatures and implementations to use cached InputTypeByName<ED>[N] and OutputTypeByName<ED>[N] instead of computing InputType<ED, EntityByName<ED, N>> and OutputType<ED, EntityByName<ED, N>>

Sequence Diagram

sequenceDiagram
    participant User
    participant TypeScript as TypeScript Compiler
    participant OutputTypeByName as OutputTypeByName Cache
    participant InputTypeByName as InputTypeByName Cache
    
    Note over User,InputTypeByName: Before: Recursive Type Computation
    User->>TypeScript: Request OutputType<ED, EntityByName<ED, "User">>
    TypeScript->>TypeScript: Compute EntityByName<ED, "User">
    TypeScript->>TypeScript: Compute OutputType<ED, ...>
    TypeScript->>TypeScript: For each relation, compute OutputType<ED, EntityByName<...>>
    Note over TypeScript: ~1.59M type instantiations
    
    Note over User,InputTypeByName: After: Cached Type Lookup
    User->>TypeScript: Request OutputTypeByName<ED>["User"]
    TypeScript->>OutputTypeByName: Check cache for ED mapping
    alt Cache Miss
        OutputTypeByName->>OutputTypeByName: Compute all types once: {[N in keyof ED]: OutputType<ED, ED[N]>}
        Note over OutputTypeByName: Types reference OutputTypeByName[...] internally
    end
    OutputTypeByName-->>TypeScript: Return cached type
    TypeScript-->>User: Return type instantly
    Note over TypeScript: ~1.28M type instantiations (300k saved)
Loading

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.

3 participants