form icon indicating copy to clipboard operation
form copied to clipboard

perf(form-core): reduce instantiations when type checking

Open chorobin opened this issue 10 months ago • 3 comments

I have used the large example in the repo (this isn't very large so difference is not so large) to check reduction in instantiations and type checking time. I am using extendedDiagnostics but also traced the example

Before:

> tsc "--extendedDiagnostics"

Files:                         215
Lines of Library:            43159
Lines of Definitions:        80538
Lines of TypeScript:           209
Lines of JavaScript:             0
Lines of JSON:                   0
Lines of Other:                  0
Identifiers:                105350
Symbols:                    118110
Types:                       18129
Instantiations:             205206
Memory used:               157045K
Assignability cache size:     9324
Identity cache size:           223
Subtype cache size:              5
Strict subtype cache size:       0
I/O Read time:               0.05s
Parse time:                  0.43s
ResolveModule time:          0.09s
ResolveTypeReference time:   0.00s
ResolveLibrary time:         0.02s
Program time:                0.66s
Bind time:                   0.21s
Check time:                  0.71s
printTime time:              0.00s
Emit time:                   0.00s
Total time:                  1.58s

After

> tsc "--extendedDiagnostics"

Files:                         215
Lines of Library:            43159
Lines of Definitions:        80528
Lines of TypeScript:           209
Lines of JavaScript:             0
Lines of JSON:                   0
Lines of Other:                  0
Identifiers:                105504
Symbols:                     85611
Types:                        6188
Instantiations:              27643
Memory used:               133987K
Assignability cache size:     1640
Identity cache size:           166
Subtype cache size:              5
Strict subtype cache size:      12
I/O Read time:               0.05s
Parse time:                  0.44s
ResolveModule time:          0.10s
ResolveTypeReference time:   0.00s
ResolveLibrary time:         0.02s
Program time:                0.68s
Bind time:                   0.21s
Check time:                  0.37s
printTime time:              0.00s
Emit time:                   0.00s
Total time:                  1.26s

There are three main things contributing to lower instantiations:

  1. Variance annotations have been added, this means when comparing two types with different type parameters, the variance worker takes less time as we are hinting to the compiler how our type parameters are going to be used
  2. TName is not constrained and uses a ValidateName utility instead. I prefer not to use contraints at inference sites against complex types that could be unions. This is because TName could default to the union and this can slow things down in a few situations.
  3. I re-worked DeepKeys and DeepValue. Instead of creating a union and then parsing TName again, a map is constructed so TValue can be grabbed by TName. TName -> TValue

chorobin avatar Mar 09 '25 23:03 chorobin

View your CI Pipeline Execution ↗ for commit ea07bce270503ae78be707b3014a64cf0f167fc9.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 21s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 18s View ↗

☁️ Nx Cloud last updated this comment at 2025-04-02 16:29:47 UTC

nx-cloud[bot] avatar Mar 09 '25 23:03 nx-cloud[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 88.86%. Comparing base (ce177f0) to head (ea07bce). Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1262      +/-   ##
==========================================
- Coverage   88.88%   88.86%   -0.02%     
==========================================
  Files          28       28              
  Lines        1277     1275       -2     
  Branches      335      332       -3     
==========================================
- Hits         1135     1133       -2     
  Misses        126      126              
  Partials       16       16              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Mar 10 '25 00:03 codecov[bot]

I'm merging it to avoid conflicts on long lived branches, thanks again!

Balastrong avatar Apr 02 '25 16:04 Balastrong