From d374d2cc1508e8f2fc021a155119be33f9924f4c Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Fri, 5 Jan 2024 11:16:26 -0500 Subject: [PATCH] Move expensive type expressions to generic --- integration-tests/lts/bench.ts | 17 +++++------ packages/generate/src/syntax/select.ts | 42 ++++++++++++-------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/integration-tests/lts/bench.ts b/integration-tests/lts/bench.ts index 8c26b41f6..4c166f377 100644 --- a/integration-tests/lts/bench.ts +++ b/integration-tests/lts/bench.ts @@ -15,14 +15,14 @@ bench("select: free object", () => { bench("select: id only", () => { const query = e.select(e.User, () => ({ id: true })); return {} as typeof query; -}).types([3642, "instantiations"]); +}).types([3895, "instantiations"]); bench("select: filtered", () => { const query = e.select(e.User, () => ({ filter_single: { id: e.uuid("123") }, })); return {} as typeof query; -}).types([5384, "instantiations"]); +}).types([5386, "instantiations"]); bench("select: nested", () => { const user = e.select(e.User, () => ({ @@ -31,7 +31,7 @@ bench("select: nested", () => { const query = e.select(user, () => ({ id: true })); return {} as typeof query; -}).types([7412, "instantiations"]); +}).types([7593, "instantiations"]); bench("select: complex", () => { const query = e.select(e.Movie, () => ({ @@ -43,7 +43,7 @@ bench("select: complex", () => { }), })); return {} as typeof query; -}).types([6339, "instantiations"]); +}).types([6556, "instantiations"]); bench("select: with filter", () => { const query = e.select(e.Hero, (hero) => ({ @@ -55,7 +55,7 @@ bench("select: with filter", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([98669, "instantiations"]); +}).types([6690, "instantiations"]); bench("select: with order", () => { const query = e.select(e.Hero, (hero) => ({ @@ -68,7 +68,7 @@ bench("select: with order", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([98963, "instantiations"]); +}).types([6985, "instantiations"]); bench("select: with limit", () => { const query = e.select(e.Hero, (hero) => ({ @@ -81,7 +81,7 @@ bench("select: with limit", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([98694, "instantiations"]); +}).types([6713, "instantiations"]); bench("select: with offset", () => { const query = e.select(e.Hero, (hero) => ({ @@ -94,5 +94,4 @@ bench("select: with offset", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([98730, "instantiations"]); - +}).types([6752, "instantiations"]); diff --git a/packages/generate/src/syntax/select.ts b/packages/generate/src/syntax/select.ts index 60371e7fc..2b2c5aea8 100644 --- a/packages/generate/src/syntax/select.ts +++ b/packages/generate/src/syntax/select.ts @@ -848,18 +848,17 @@ export const $existingScopes = new Set< function $shape< Expr extends ObjectTypeExpression, - Shape extends objectTypeToSelectShape & - SelectModifiers // + Element extends Expr["__element__"], + Shape extends objectTypeToSelectShape & SelectModifiers, + Scope extends $scopify & + $linkPropify<{ + [k in keyof Expr]: k extends "__cardinality__" + ? Cardinality.One + : Expr[k]; + }> >( expr: Expr, - _shape: ( - scope: $scopify & - $linkPropify<{ - [k in keyof Expr]: k extends "__cardinality__" - ? Cardinality.One - : Expr[k]; - }> - ) => Readonly + _shape: (scope: Scope) => Readonly ): (scope: unknown) => Readonly; function $shape(_a: unknown, b: (...args: any) => any) { return b; @@ -881,23 +880,22 @@ export function select( ): $expr_Select>; export function select< Expr extends ObjectTypeExpression, - Shape extends objectTypeToSelectShape & - SelectModifiers, + Element extends Expr["__element__"], + Scope extends $scopify & + $linkPropify<{ + [k in keyof Expr]: k extends "__cardinality__" + ? Cardinality.One + : Expr[k]; + }>, + Shape extends objectTypeToSelectShape & SelectModifiers, Modifiers extends UnknownSelectModifiers = Pick >( expr: Expr, - shape: ( - scope: $scopify & - $linkPropify<{ - [k in keyof Expr]: k extends "__cardinality__" - ? Cardinality.One - : Expr[k]; - }> - ) => Readonly + shape: (scope: Scope) => Readonly ): $expr_Select<{ __element__: ObjectType< - `${Expr["__element__"]["__name__"]}`, // _shape - Expr["__element__"]["__pointers__"], + `${Element["__name__"]}`, // _shape + Element["__pointers__"], Omit, SelectModifierNames> >; __cardinality__: ComputeSelectCardinality;