Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: .extend performance #2328

Merged
merged 6 commits into from
May 29, 2024
Merged

fix: .extend performance #2328

merged 6 commits into from
May 29, 2024

Conversation

tmm
Copy link
Member

@tmm tmm commented May 28, 2024

GetValue was causing ContractFunctionParameters to blow up when used with .extend + publicActions.

Added some type benchmarks using attest to protect against this in the future.

Resolves #2322


PR-Codex overview

This PR focuses on improving performance and adding type checking.

Detailed summary

  • Improved performance with .extend in createClient
  • Added type checking in various files
  • Updated dependencies and excluded unnecessary files
  • Refactored writeContracts function for better readability

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

vercel bot commented May 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
viem ✅ Ready (Inspect) Visit Preview May 29, 2024 0:49am

Copy link

changeset-bot bot commented May 28, 2024

🦋 Changeset detected

Latest commit: fbcb2fc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

vercel.json Outdated Show resolved Hide resolved
Copy link

socket-security bot commented May 28, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@arktype/attest@0.7.5 environment, filesystem 0 131 kB ssalbdivad
npm/tsx@4.11.0 Transitive: environment, filesystem, network, shell, unsafe +26 224 MB hirokiosame

🚮 Removed packages: npm/@actions/core@1.10.1, npm/@actions/github@5.1.1

View full report↗︎

@@ -209,9 +209,7 @@ export type ContractFunctionParameters<
| allFunctionNames // show all options
| (functionName extends allFunctionNames ? functionName : never) // infer value
args?: (abi extends Abi ? UnionWiden<args> : never) | allArgs | undefined
} & (readonly [] extends allArgs ? {} : { args: Widen<args> }) &
// TODO: Remove `GetValue` from here (should be applied to top-level type as separate utility)
GetValue<abi, functionName>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetValue was causing ContractFunctionParameters to blow up when used with .extend + publicActions

Comment on lines -1 to -2
### Description

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this lol

@@ -54,7 +54,7 @@ jobs:
timeout-minutes: 5
strategy:
matrix:
typescript-version: ['5.3.2', 'latest']
version: ['5.0.4', '5.1.6', '5.2.2', '5.3.3', '5.4.5']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test against more ts versions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if it's worth the work to fix the types that are incompatible with older versions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. on it!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these type benchmarking tests make sure the types don't blow up too much

Copy link
Contributor

github-actions bot commented May 28, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
viem (esm) 56.94 KB (0%) 1.2 s (0%) 1.3 s (-20.07% 🔽) 2.4 s
viem (cjs) 68.37 KB (0%) 1.4 s (0%) 4.9 s (+52.58% 🔺) 6.3 s
viem (minimal surface - tree-shaking) 3.31 KB (0%) 67 ms (0%) 43 ms (+158.54% 🔺) 109 ms
viem/accounts 73.61 KB (0%) 1.5 s (0%) 1.1 s (+47.62% 🔺) 2.5 s
viem/accounts (tree-shaking) 18.89 KB (0%) 378 ms (0%) 363 ms (-39.84% 🔽) 741 ms
viem/actions 41.77 KB (0%) 836 ms (0%) 1.1 s (+48.97% 🔺) 1.9 s
viem/actions (tree-shaking) 318 B (0%) 10 ms (0%) 10 ms (+33.34% 🔺) 20 ms
viem/chains 27.9 KB (0%) 559 ms (0%) 493 ms (-5.62% 🔽) 1.1 s
viem/chains (tree-shaking) 324 B (0%) 10 ms (0%) 26 ms (+345.46% 🔺) 36 ms
viem/chains/utils 1.02 KB (0%) 21 ms (0%) 31 ms (+167.5% 🔺) 51 ms
viem/ens 41.77 KB (-0.04% 🔽) 836 ms (-0.04% 🔽) 970 ms (+170.66% 🔺) 1.9 s
viem/ens (tree-shaking) 18.41 KB (0%) 369 ms (0%) 834 ms (+3.4% 🔺) 1.3 s
viem/siwe 26.53 KB (+100% 🔺) 531 ms (+100% 🔺) 1.3 s (+100% 🔺) 1.8 s
viem/siwe (tree-shaking) 25.4 KB (+100% 🔺) 508 ms (+100% 🔺) 889 ms (+100% 🔺) 1.4 s

Comment on lines +124 to +149
export type WriteContractFunctionParameters<
abi extends Abi | readonly unknown[] = Abi,
mutability extends AbiStateMutability = AbiStateMutability,
functionName extends ContractFunctionName<
abi,
mutability
> = ContractFunctionName<abi, mutability>,
args extends ContractFunctionArgs<
abi,
mutability,
functionName
> = ContractFunctionArgs<abi, mutability, functionName>,
///
allFunctionNames = ContractFunctionName<abi, mutability>,
allArgs = ContractFunctionArgs<abi, mutability, functionName>,
// when `args` is inferred to `readonly []` ("inputs": []) or `never` (`abi` declared as `Abi` or not inferrable), allow `args` to be optional.
// important that both branches return same structural type
> = {
address: Address
abi: abi
functionName:
| allFunctionNames // show all options
| (functionName extends allFunctionNames ? functionName : never) // infer value
args?: (abi extends Abi ? UnionWiden<args> : never) | allArgs | undefined
} & (readonly [] extends allArgs ? {} : { args: Widen<args> }) &
GetValue<abi, functionName>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated this type here for now. will address during some more extensive cleanup.

Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (0f10468) to head (fbcb2fc).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2328   +/-   ##
=======================================
  Coverage   99.67%   99.67%           
=======================================
  Files         673      673           
  Lines       57331    57367   +36     
  Branches     2770     2772    +2     
=======================================
+ Hits        57145    57181   +36     
+ Misses        169      168    -1     
- Partials       17       18    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tmm tmm merged commit d946d55 into main May 29, 2024
25 of 26 checks passed
@tmm tmm deleted the tmm/type-perf branch May 29, 2024 01:29
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.

client extend slow type inference
2 participants