Skip to content

Commit

Permalink
refactor(experimental): use DrainOuterGeneric helper on codec type ma…
Browse files Browse the repository at this point in the history
…ppings
  • Loading branch information
lorisleiva committed Mar 20, 2024
1 parent 7ed7506 commit d309da8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .changeset/heavy-students-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@solana/codecs-data-structures': patch
---

Use `DrainOuterGeneric` helper on codec type mappings

This significantly reduces the number of TypeScript instantiations on object mappings,
which increases TypeScript performance and prevents "Type instantiation is excessively deep and possibly infinite" errors.
10 changes: 5 additions & 5 deletions packages/codecs-data-structures/src/data-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SolanaError,
} from '@solana/errors';

import { getMaxSize, maxCodecSizes, sumCodecSizes } from './utils';
import { DrainOuterGeneric, getMaxSize, maxCodecSizes, sumCodecSizes } from './utils';

/**
* Defines a data enum using discriminated union types.
Expand Down Expand Up @@ -74,21 +74,21 @@ export type DataEnumCodecConfig<TDiscriminator = NumberCodec | NumberDecoder | N
type Variants<T> = readonly (readonly [string, T])[];
type ArrayIndices<T extends readonly unknown[]> = Exclude<Partial<T>['length'], T['length']> & number;

type GetEncoderTypeFromVariants<TVariants extends Variants<Encoder<any>>> = {
type GetEncoderTypeFromVariants<TVariants extends Variants<Encoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TVariants>]: (TVariants[I][1] extends Encoder<infer TFrom>
? TFrom extends object
? TFrom
: object
: never) & { __kind: TVariants[I][0] };
}[ArrayIndices<TVariants>];
}>[ArrayIndices<TVariants>];

type GetDecoderTypeFromVariants<TVariants extends Variants<Decoder<any>>> = {
type GetDecoderTypeFromVariants<TVariants extends Variants<Decoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TVariants>]: (TVariants[I][1] extends Decoder<infer TTo>
? TTo extends object
? TTo
: object
: never) & { __kind: TVariants[I][0] };
}[ArrayIndices<TVariants>];
}>[ArrayIndices<TVariants>];

/**
* Creates a data enum encoder.
Expand Down
10 changes: 5 additions & 5 deletions packages/codecs-data-structures/src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import {
VariableSizeEncoder,
} from '@solana/codecs-core';

import { getFixedSize, getMaxSize, sumCodecSizes } from './utils';
import { DrainOuterGeneric, getFixedSize, getMaxSize, sumCodecSizes } from './utils';

type Fields<T> = readonly (readonly [string, T])[];
type ArrayIndices<T extends readonly unknown[]> = Exclude<Partial<T>['length'], T['length']> & number;

type GetEncoderTypeFromFields<TFields extends Fields<Encoder<any>>> = {
type GetEncoderTypeFromFields<TFields extends Fields<Encoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TFields> as TFields[I][0]]: TFields[I][1] extends Encoder<infer TFrom> ? TFrom : never;
};
}>;

type GetDecoderTypeFromFields<TFields extends Fields<Decoder<any>>> = {
type GetDecoderTypeFromFields<TFields extends Fields<Decoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TFields> as TFields[I][0]]: TFields[I][1] extends Decoder<infer TTo> ? TTo : never;
};
}>;

/**
* Creates a encoder for a custom object.
Expand Down
12 changes: 12 additions & 0 deletions packages/codecs-data-structures/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { isFixedSize } from '@solana/codecs-core';

/**
* Functionally, this type helper is equivalent to the identity type — i.e. `type Identity<T> = T`.
* However, wrapping generic object mappings in this type significantly reduces the number
* of instantiation expressions processed, which increases TypeScript performance and
* prevents "Type instantiation is excessively deep and possibly infinite" errors.
*
* This works because TypeScript doesn't create a new level of nesting when encountering conditional generic types.
* @see https://github.com/microsoft/TypeScript/issues/34933
* @see https://github.com/kysely-org/kysely/pull/483
*/
export type DrainOuterGeneric<T> = [T] extends [unknown] ? T : never;

export function maxCodecSizes(sizes: (number | null)[]): number | null {
return sizes.reduce(
(all, size) => (all === null || size === null ? null : Math.max(all, size)),
Expand Down

0 comments on commit d309da8

Please sign in to comment.