From bf19955c8bbb4d227bf884f35eb4b7d9c6fe1ffb Mon Sep 17 00:00:00 2001 From: Scott Trinh Date: Wed, 10 Jan 2024 13:14:21 -0500 Subject: [PATCH] Some continued performance improvements (#832) --- integration-tests/lts/bench.ts | 28 +++---- packages/driver/src/reflection/typeutil.ts | 2 +- packages/generate/src/syntax/params.ts | 28 +++---- packages/generate/src/syntax/select.ts | 85 +++++++++++----------- packages/generate/src/syntax/typesystem.ts | 13 ++-- 5 files changed, 71 insertions(+), 85 deletions(-) diff --git a/integration-tests/lts/bench.ts b/integration-tests/lts/bench.ts index c6e670db5..256c11f2b 100644 --- a/integration-tests/lts/bench.ts +++ b/integration-tests/lts/bench.ts @@ -5,19 +5,19 @@ import e from "./dbschema/edgeql-js"; bench("scalar literal", () => { const lit = e.int32(42); return {} as typeof lit; -}).types([656, "instantiations"]); +}).types([555, "instantiations"]); bench("array literal", () => { const lit = e.literal(e.array(e.str), ["abcd"]); return {} as typeof lit; -}).types([2454, "instantiations"]); +}).types([2407, "instantiations"]); bench("named tuple literal", () => { const lit = e.literal(e.tuple({ str: e.str }), { str: "asdf", }); return {} as typeof lit; -}).types([11654, "instantiations"]); +}).types([11597, "instantiations"]); bench("base type: array", () => { const baseType = e.array(e.str); @@ -32,24 +32,24 @@ bench("base type: named tuple", () => { bench("select: scalar", () => { const query = e.select(e.int32(42)); return {} as typeof query; -}).types([1263, "instantiations"]); +}).types([1155, "instantiations"]); bench("select: free object", () => { const query = e.select({ meaning: e.int32(42) }); return {} as typeof query; -}).types([2120, "instantiations"]); +}).types([2012, "instantiations"]); bench("select: id only", () => { const query = e.select(e.User, () => ({ id: true })); return {} as typeof query; -}).types([4105, "instantiations"]); +}).types([3687, "instantiations"]); bench("select: filtered", () => { const query = e.select(e.User, () => ({ filter_single: { id: e.uuid("123") }, })); return {} as typeof query; -}).types([5596, "instantiations"]); +}).types([5019, "instantiations"]); bench("select: nested", () => { const user = e.select(e.User, () => ({ @@ -58,7 +58,7 @@ bench("select: nested", () => { const query = e.select(user, () => ({ id: true })); return {} as typeof query; -}).types([7911, "instantiations"]); +}).types([6037, "instantiations"]); bench("select: complex", () => { const query = e.select(e.Movie, () => ({ @@ -70,7 +70,7 @@ bench("select: complex", () => { }), })); return {} as typeof query; -}).types([6887, "instantiations"]); +}).types([6342, "instantiations"]); bench("select: with filter", () => { const query = e.select(e.Hero, (hero) => ({ @@ -82,7 +82,7 @@ bench("select: with filter", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([7085, "instantiations"]); +}).types([6289, "instantiations"]); bench("select: with order", () => { const query = e.select(e.Hero, (hero) => ({ @@ -95,7 +95,7 @@ bench("select: with order", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([7380, "instantiations"]); +}).types([6624, "instantiations"]); bench("select: with limit", () => { const query = e.select(e.Hero, (hero) => ({ @@ -108,7 +108,7 @@ bench("select: with limit", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([7108, "instantiations"]); +}).types([6352, "instantiations"]); bench("select: with offset", () => { const query = e.select(e.Hero, (hero) => ({ @@ -121,7 +121,7 @@ bench("select: with offset", () => { filter_single: e.op(hero.name, "=", "Peter Parker"), })); return {} as typeof query; -}).types([7147, "instantiations"]); +}).types([6391, "instantiations"]); bench("params select", () => { const query = e.params({ name: e.str }, (params) => @@ -135,4 +135,4 @@ bench("params select", () => { })) ); return {} as typeof query; -}).types([14680, "instantiations"]); +}).types([11865, "instantiations"]); diff --git a/packages/driver/src/reflection/typeutil.ts b/packages/driver/src/reflection/typeutil.ts index 00df0a817..bd53e7b28 100644 --- a/packages/driver/src/reflection/typeutil.ts +++ b/packages/driver/src/reflection/typeutil.ts @@ -7,7 +7,7 @@ export namespace typeutil { export type depromisify = T extends Promise ? depromisify : T; export type identity = T; - export type flatten = identity<{ [k in keyof T]: T[k] }>; + export type flatten = { [k in keyof T]: T[k] } & unknown; export type tupleOf = [T, ...T[]] | []; export type writeable = { -readonly [P in keyof T]: T[P] }; diff --git a/packages/generate/src/syntax/params.ts b/packages/generate/src/syntax/params.ts index 5e981ccc1..03b24c76b 100644 --- a/packages/generate/src/syntax/params.ts +++ b/packages/generate/src/syntax/params.ts @@ -16,6 +16,10 @@ import { runnableExpressionKinds } from "./query"; import { select } from "./select"; import { complexParamKinds } from "./__spec__"; +type Param = ParamType | $expr_OptionalParam; + +type ParamsRecord = Record; + export type $expr_OptionalParam = { __kind__: ExpressionKind.OptionalParam; __type__: Type; @@ -32,9 +36,7 @@ export function optional( export type QueryableWithParamsExpression< Set extends TypeSet = TypeSet, - Params extends { - [key: string]: ParamType | $expr_OptionalParam; - } = {} + Params extends ParamsRecord = Record > = Expression & { run( cxn: Executor, @@ -44,9 +46,7 @@ export type QueryableWithParamsExpression< }; export type $expr_WithParams< - Params extends { - [key: string]: ParamType | $expr_OptionalParam; - } = {}, + Params extends ParamsRecord = Record, Expr extends TypeSet = TypeSet > = QueryableWithParamsExpression< { @@ -59,11 +59,7 @@ export type $expr_WithParams< Params >; -type paramsToParamArgs< - Params extends { - [key: string]: ParamType | $expr_OptionalParam; - } -> = { +type paramsToParamArgs = { [key in keyof Params as Params[key] extends ParamType ? key : never]: Params[key] extends ParamType @@ -91,11 +87,7 @@ export type $expr_Param< __isComplex__: boolean; }>; -type paramsToParamExprs< - Params extends { - [key: string]: ParamType | $expr_OptionalParam; - } -> = { +type paramsToParamExprs = { [key in keyof Params]: Params[key] extends $expr_OptionalParam ? $expr_Param : Params[key] extends ParamType @@ -104,9 +96,7 @@ type paramsToParamExprs< }; export function params< - Params extends { - [key: string]: ParamType | $expr_OptionalParam; - } = {}, + Params extends ParamsRecord = Record, Expr extends Expression = Expression >( paramsDef: Params, diff --git a/packages/generate/src/syntax/select.ts b/packages/generate/src/syntax/select.ts index 2b2c5aea8..5f6054dbf 100644 --- a/packages/generate/src/syntax/select.ts +++ b/packages/generate/src/syntax/select.ts @@ -96,6 +96,10 @@ export type SelectModifierNames = | "offset" | "limit"; +type filterSingle = T extends ObjectTypeSet + ? TypeSet, T["__cardinality__"]> + : orLiteralValue; + export type exclusivesToFilterSingle = ExclusiveTuple extends E ? never @@ -103,12 +107,7 @@ export type exclusivesToFilterSingle = ? never : { [j in keyof E]: { - [k in keyof E[j]]: E[j][k] extends ObjectTypeSet - ? TypeSet< - anonymizeObject, - E[j][k]["__cardinality__"] - > - : orLiteralValue; + [k in keyof E[j]]: filterSingle; }; }[number]; export type SelectModifiers = { @@ -353,10 +352,10 @@ export interface SelectModifierMethods { export type InferOffsetLimitCardinality< Card extends Cardinality, - Modifers extends UnknownSelectModifiers -> = Modifers["limit"] extends number | LimitExpression + Modifiers extends UnknownSelectModifiers +> = Modifiers["limit"] extends number | LimitExpression ? cardutil.overrideLowerBound - : Modifers["offset"] extends number | OffsetExpression + : Modifiers["offset"] extends number | OffsetExpression ? cardutil.overrideLowerBound : Card; @@ -741,7 +740,7 @@ export type linkDescToLinkProps = { export type pointersToObjectType

= ObjectType< string, P, - {} + object >; type linkDescToShape = objectTypeToSelectShape< @@ -749,36 +748,34 @@ type linkDescToShape = objectTypeToSelectShape< > & objectTypeToSelectShape> & SelectModifiers; -export type linkDescToSelectElement = + +type linkDescToSelectElement = | boolean - // | pointerToCastableExpression | TypeSet, cardutil.assignable> | linkDescToShape | (( scope: $scopify & linkDescToLinkProps ) => linkDescToShape); +type propDescToSelectElement

= + | boolean + | TypeSet> + | $expr_PolyShapeElement; + // object types -> pointers // pointers -> links // links -> target object type // links -> link properties -export type objectTypeToSelectShape = - // ObjectType extends T - // ? {[k: string]: unknown} - // : - Partial<{ - [k in keyof T["__pointers__"]]: T["__pointers__"][k] extends PropertyDesc - ? - | boolean - | TypeSet< - T["__pointers__"][k]["target"], - cardutil.assignable - > - | $expr_PolyShapeElement - : T["__pointers__"][k] extends LinkDesc - ? linkDescToSelectElement - : any; - }> & { [k: string]: unknown }; +export type objectTypeToSelectShape< + T extends ObjectType = ObjectType, + Pointers extends ObjectTypePointers = T["__pointers__"] +> = Partial<{ + [k in keyof Pointers]: Pointers[k] extends PropertyDesc + ? propDescToSelectElement + : Pointers[k] extends LinkDesc + ? linkDescToSelectElement + : any; +}> & { [k: string]: unknown }; // incorporate __shape__ (computeds) on selection shapes // this works but a major rewrite of setToTsType is required @@ -865,15 +862,18 @@ function $shape(_a: unknown, b: (...args: any) => any) { } export { $shape as shape }; -export function select( +export function select< + Expr extends ObjectTypeExpression, + Element extends Expr["__element__"], + ElementName extends `${Element["__name__"]}`, + ElementPointers extends Element["__pointers__"], + ElementShape extends Element["__shape__"], + Card extends Expr["__cardinality__"] +>( expr: Expr ): $expr_Select<{ - __element__: ObjectType< - `${Expr["__element__"]["__name__"]}`, // _shape - Expr["__element__"]["__pointers__"], - Expr["__element__"]["__shape__"] // {id: true} - >; - __cardinality__: Expr["__cardinality__"]; + __element__: ObjectType; + __cardinality__: Card; }>; export function select( expr: Expr @@ -881,24 +881,23 @@ export function select( export function select< Expr extends ObjectTypeExpression, Element extends Expr["__element__"], + Shape extends objectTypeToSelectShape & SelectModifiers, + SelectCard extends ComputeSelectCardinality, + SelectShape extends normaliseShape, Scope extends $scopify & $linkPropify<{ [k in keyof Expr]: k extends "__cardinality__" ? Cardinality.One : Expr[k]; }>, - Shape extends objectTypeToSelectShape & SelectModifiers, + ElementName extends `${Element["__name__"]}`, Modifiers extends UnknownSelectModifiers = Pick >( expr: Expr, shape: (scope: Scope) => Readonly ): $expr_Select<{ - __element__: ObjectType< - `${Element["__name__"]}`, // _shape - Element["__pointers__"], - Omit, SelectModifierNames> - >; - __cardinality__: ComputeSelectCardinality; + __element__: ObjectType; + __cardinality__: SelectCard; }>; /* diff --git a/packages/generate/src/syntax/typesystem.ts b/packages/generate/src/syntax/typesystem.ts index a559a6470..5ebcf9c37 100644 --- a/packages/generate/src/syntax/typesystem.ts +++ b/packages/generate/src/syntax/typesystem.ts @@ -31,8 +31,8 @@ export type BaseTypeTuple = typeutil.tupleOf; export interface ScalarType< Name extends string = string, - TsType extends any = any, - TsArgType extends any = TsType, + TsType = any, + TsArgType = TsType, TsConstType extends TsType = TsType > extends BaseType { __kind__: TypeKind.scalar; @@ -48,12 +48,9 @@ export type scalarTypeWithConstructor< > = S & { // tslint:disable-next-line (val: T): $expr_Literal< - ScalarType< - S["__name__"], - S["__tstype__"], - S["__tsargtype__"], - T extends S["__tstype__"] ? T : S["__tstype__"] - > + Omit & { + __tsconsttype__: T extends S["__tstype__"] ? T : S["__tstype__"]; + } >; };