Skip to content

Commit

Permalink
ci: add type benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Jan 17, 2024
1 parent 921efbd commit 0f5d67d
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 117 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ jobs:
- name: Check types (--exactOptionalPropertyTypes false)
run: pnpm typecheck:propertyTypes

- name: Bench types
run: pnpm typebench

# Redundant with `pnpm typecheck`
# If Vitest adds special features in the future, e.g. type coverage, can add this back!
# - name: Test types
Expand Down
25 changes: 8 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
*.local
.DS_Store
.next
.attest
.env
.env.*local
.envrc
.pnpm-debug.log*
bench
cache
coverage
dist
node_modules
tsconfig*.tsbuildinfo
bench
playgrounds/performance/out
# temporary type-testing cache
.attest

# local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
.envrc

# proxy packages
packages/abitype/abis
packages/abitype/zod
packages/abitype/zod
playgrounds/performance/out
tsconfig*.tsbuildinfo
4 changes: 2 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"files": {
"ignore": [
".vocs",
"CHANGELOG.md",
"pnpm-lock.yaml",
"trace/**",
"tsconfig.*.json",
".vocs"
"tsconfig.*.json"
]
},
"formatter": {
Expand Down
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"changeset:version": "changeset version && pnpm version:update",
"clean": "pnpm run --r --parallel clean",
"deps": "pnpx taze -r",
"docs:dev": "rimraf node_modules/.cache/twoslash && pnpm -r --filter docs dev",
"docs:dev": "pnpm -r --filter docs dev",
"format": "biome format . --write",
"lint": "biome check .",
"lint:fix": "pnpm lint --apply",
Expand All @@ -25,8 +25,10 @@
"test:typecheck": "vitest typecheck -c ./packages/abitype/test/vitest.config.ts",
"test:update": "vitest --update",
"trace": "tsc --noEmit --generateTrace ./playgrounds/performance/out --incremental false --project playgrounds/performance/tsconfig.json && echo \"Open playgrounds/performance/out/trace.json in https://ui.perfetto.dev\"",
"typebench": "pnpm run --r --parallel typebench",
"typecheck": "pnpm run --r --parallel typecheck && tsc --noEmit",
"typecheck:propertyTypes": "pnpm run --r --parallel typecheck --exactOptionalPropertyTypes false && tsc --noEmit --exactOptionalPropertyTypes false",
"typeperf": "pnpm run --r --parallel typeperf",
"version:update": "bun .scripts/updateVersion.ts"
},
"devDependencies": {
Expand All @@ -40,21 +42,15 @@
"glob": "^10.3.10",
"knip": "^2.29.0",
"publint": "^0.2.2",
"rimraf": "^5.0.1",
"simple-git-hooks": "^2.8.1",
"tsx": "^4.7.0",
"typescript": "5.0.4",
"typescript@5.1.3": "npm:typescript@5.1.3",
"typescript@5.2.2": "npm:typescript@5.2.2",
"vitest": "0.34.5"
},
"packageManager": "pnpm@8.8.0",
"pnpm": {
"overrides": {
"remark-shiki-twoslash>shiki": "^0.14.1",
"shiki-twoslash>shiki": "^0.14.1"
},
"peerDependencyRules": {
"ignoreMissing": ["@algolia/client-search", "search-insights"]
"ignoreMissing": ["rollup"]
}
},
"simple-git-hooks": {
Expand All @@ -76,6 +72,7 @@
"entry": [
"src/exports/{abis,index,zod}.ts!",
"**/*.bench.ts",
"**/*.bench-d.ts",
"**/*.test.ts",
"**/*.test-d.ts"
],
Expand Down
11 changes: 5 additions & 6 deletions packages/abitype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:esm+types": "tsc --project tsconfig.build.json --module es2020 --outDir ./dist/esm --declaration --declarationMap --declarationDir ./dist/types && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}'",
"clean": "rm -rf dist tsconfig.tsbuildinfo abis zod",
"test:build": "publint --strict",
"typebench": "tsx src/**.bench-d.ts --benchPercentThreshold 20 --benchErrorOnThresholdExceeded",
"typecheck": "tsc --noEmit",
"typeperf": "tsc --noEmit --extendedDiagnostics --composite false --incremental false"
},
Expand All @@ -20,6 +21,7 @@
"!src/**/*.test.ts",
"!src/**/*.test-d.ts",
"!src/**/*.bench.ts",
"!src/**/*.bench-d.ts",
"/abis",
"/zod"
],
Expand Down Expand Up @@ -70,20 +72,17 @@
"ethers": "^6.5.1",
"zod": "^3.22.4"
},
"contributors": [
"awkweb.eth <t@wagmi.sh>",
"jxom.eth <j@wagmi.sh>"
],
"contributors": ["awkweb.eth <t@wevm.dev>", "jxom.eth <j@wevm.dev>"],
"funding": "https://github.com/sponsors/wevm",
"keywords": [
"abi",
"eth",
"ethereum",
"typescript",
"types",
"web3",
"typescript",
"viem",
"wagmi",
"web3",
"wevm"
]
}
12 changes: 0 additions & 12 deletions packages/abitype/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,6 @@ type KeyofUnion<T> = T extends T ? keyof T : never
*/
export type Pretty<T> = { [K in keyof T]: T[K] } & unknown

/**
* Check that a type is a subtype of another.
*
* Useful for ensuring more complex types conform to a base pattern, e.g. by
* defining a set of keys.
*
* @param Base - The type that T must extend.
* @param T - The type to check.
* @returns T
*/
export type Satisfy<Base, T extends Base> = T

/**
* Creates range between two positive numbers using [tail recursion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types).
*
Expand Down
101 changes: 101 additions & 0 deletions packages/abitype/src/utils.bench-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { bench } from "@arktype/attest";
import {
type AbiParameterToPrimitiveType,
type AbiParametersToPrimitiveTypes,
type TypedDataToPrimitiveTypes,
} from "./utils.js";

bench("AbiParameterToPrimitiveType > nested", () => {
const abiParameter = {
name: "s",
type: "tuple",
components: [
{ name: "a", type: "uint8" },
{ name: "b", type: "uint8[2]" },
{
name: "c",
type: "tuple[]",
components: [
{ name: "x", type: "uint256" },
{
name: "y",
type: "tuple",
components: [{ name: "a", type: "string" }],
},
],
},
],
} as const;
return {} as AbiParameterToPrimitiveType<typeof abiParameter>;
}).types([28, "instantiations"]);

bench("AbiParametersToPrimitiveTypes > nested", () => {
const abiParameters = [
{
name: "s",
type: "tuple",
components: [
{ name: "a", type: "uint8" },
{ name: "b", type: "uint8[]" },
{
name: "c",
type: "tuple[]",
components: [
{ name: "x", type: "uint8" },
{ name: "y", type: "uint8" },
],
},
],
},
{
name: "t",
type: "tuple",
components: [
{ name: "x", type: "uint8" },
{ name: "y", type: "uint8" },
],
},
{ name: "a", type: "uint8" },
{
name: "t",
type: "tuple[2]",
components: [
{ name: "x", type: "uint256" },
{ name: "y", type: "uint256" },
],
},
] as const;
return {} as AbiParametersToPrimitiveTypes<typeof abiParameters>;
}).types([56, "instantiations"]);

bench("TypedDataToPrimitiveTypes > recursive", () => {
const types = {
Foo: [{ name: "bar", type: "Bar[]" }],
Bar: [{ name: "foo", type: "Foo[]" }],
} as const;
return {} as TypedDataToPrimitiveTypes<typeof types>;
}).types([12, "instantiations"]);

bench("TypedDataToPrimitiveTypes > deep", () => {
const types = {
Contributor: [
{ name: "name", type: "string" },
{ name: "address", type: "address" },
],
Website: [
{ name: "domain", type: "string" },
{ name: "webmaster", type: "Contributor" },
],
Project: [
{ name: "name", type: "string" },
{ name: "contributors", type: "Contributor[2]" },
{ name: "website", type: "Website" },
],
Organization: [
{ name: "name", type: "string" },
{ name: "projects", type: "Project[]" },
{ name: "website", type: "Website" },
],
} as const;
return {} as TypedDataToPrimitiveTypes<typeof types>;
}).types([44, "instantiations"]);
34 changes: 0 additions & 34 deletions packages/abitype/src/utils.bench.ts

This file was deleted.

55 changes: 54 additions & 1 deletion packages/abitype/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
import { attest } from '@arktype/attest'
import { test } from 'vitest'
import type { TypedDataToPrimitiveTypes } from './utils.js'
import type {
AbiParametersToPrimitiveTypes,
TypedDataToPrimitiveTypes,
} from './utils.js'

test('deeply nested parameters', () => {
type Result = AbiParametersToPrimitiveTypes<
[
{
name: 's'
type: 'tuple'
components: [
{ name: 'a'; type: 'uint8' },
{ name: 'b'; type: 'uint8[]' },
{
name: 'c'
type: 'tuple[]'
components: [
{ name: 'x'; type: 'uint8' },
{ name: 'y'; type: 'uint8' },
]
},
]
},
{
name: 't'
type: 'tuple'
components: [{ name: 'x'; type: 'uint8' }, { name: 'y'; type: 'uint8' }]
},
{ name: 'a'; type: 'uint8' },
{
name: 't'
type: 'tuple[2]'
components: [
{ name: 'x'; type: 'uint256' },
{ name: 'y'; type: 'uint256' },
]
},
]
>
attest<
[
{
a: number
b: readonly number[]
c: readonly { x: number; y: number }[]
},
{ x: number; y: number },
number,
readonly [{ x: bigint; y: bigint }, { x: bigint; y: bigint }],
],
Result
>()
})

test('self-referencing', () => {
type Result = TypedDataToPrimitiveTypes<{
Expand Down
2 changes: 1 addition & 1 deletion packages/abitype/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type GreaterThan48Bits = Exclude<MBits, 8 | 16 | 24 | 32 | 40 | 48 | NoBits>
type LessThanOrEqualTo48Bits = Exclude<MBits, GreaterThan48Bits | NoBits>
type NoBits = ''

export type BitsTypeLookup = {
type BitsTypeLookup = {
[K in MBits]: ResolvedRegister[K extends LessThanOrEqualTo48Bits
? 'IntType'
: 'BigIntType']
Expand Down
2 changes: 1 addition & 1 deletion packages/abitype/test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, setup } from '@arktype/attest'

export default () => {
export default function () {
setup()
return cleanup
}
7 changes: 6 additions & 1 deletion packages/abitype/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.bench.ts", "src/**/*.test.ts", "src/**/*.test-d.ts"],
"exclude": [
"src/**/*.bench.ts",
"src/**/*.bench-d.ts",
"src/**/*.test.ts",
"src/**/*.test-d.ts"
],
"compilerOptions": {
"sourceMap": true
}
Expand Down
Loading

1 comment on commit 0f5d67d

@vercel
Copy link

@vercel vercel bot commented on 0f5d67d Jan 17, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

abitype – ./

abitype-wagmi-dev.vercel.app
abitype.vercel.app
abitype-git-main-wagmi-dev.vercel.app
abitype.dev

Please # to comment.