Skip to content

Commit

Permalink
Some continued performance improvements (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh authored Jan 10, 2024
1 parent e0b9e5e commit bf19955
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 85 deletions.
28 changes: 14 additions & 14 deletions integration-tests/lts/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, () => ({
Expand All @@ -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, () => ({
Expand All @@ -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) => ({
Expand All @@ -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) => ({
Expand All @@ -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) => ({
Expand All @@ -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) => ({
Expand All @@ -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) =>
Expand All @@ -135,4 +135,4 @@ bench("params select", () => {
}))
);
return {} as typeof query;
}).types([14680, "instantiations"]);
}).types([11865, "instantiations"]);
2 changes: 1 addition & 1 deletion packages/driver/src/reflection/typeutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export namespace typeutil {

export type depromisify<T> = T extends Promise<infer U> ? depromisify<U> : T;
export type identity<T> = T;
export type flatten<T> = identity<{ [k in keyof T]: T[k] }>;
export type flatten<T> = { [k in keyof T]: T[k] } & unknown;
export type tupleOf<T> = [T, ...T[]] | [];
export type writeable<T> = { -readonly [P in keyof T]: T[P] };

Expand Down
28 changes: 9 additions & 19 deletions packages/generate/src/syntax/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { runnableExpressionKinds } from "./query";
import { select } from "./select";
import { complexParamKinds } from "./__spec__";

type Param = ParamType | $expr_OptionalParam;

type ParamsRecord = Record<string, Param>;

export type $expr_OptionalParam<Type extends ParamType = ParamType> = {
__kind__: ExpressionKind.OptionalParam;
__type__: Type;
Expand All @@ -32,9 +36,7 @@ export function optional<Type extends ParamType>(

export type QueryableWithParamsExpression<
Set extends TypeSet = TypeSet,
Params extends {
[key: string]: ParamType | $expr_OptionalParam;
} = {}
Params extends ParamsRecord = Record<string, never>
> = Expression<Set, false> & {
run(
cxn: Executor,
Expand All @@ -44,9 +46,7 @@ export type QueryableWithParamsExpression<
};

export type $expr_WithParams<
Params extends {
[key: string]: ParamType | $expr_OptionalParam;
} = {},
Params extends ParamsRecord = Record<string, never>,
Expr extends TypeSet = TypeSet
> = QueryableWithParamsExpression<
{
Expand All @@ -59,11 +59,7 @@ export type $expr_WithParams<
Params
>;

type paramsToParamArgs<
Params extends {
[key: string]: ParamType | $expr_OptionalParam;
}
> = {
type paramsToParamArgs<Params extends ParamsRecord> = {
[key in keyof Params as Params[key] extends ParamType
? key
: never]: Params[key] extends ParamType
Expand Down Expand Up @@ -91,11 +87,7 @@ export type $expr_Param<
__isComplex__: boolean;
}>;

type paramsToParamExprs<
Params extends {
[key: string]: ParamType | $expr_OptionalParam;
}
> = {
type paramsToParamExprs<Params extends ParamsRecord> = {
[key in keyof Params]: Params[key] extends $expr_OptionalParam
? $expr_Param<key, Params[key]["__type__"], true>
: Params[key] extends ParamType
Expand All @@ -104,9 +96,7 @@ type paramsToParamExprs<
};

export function params<
Params extends {
[key: string]: ParamType | $expr_OptionalParam;
} = {},
Params extends ParamsRecord = Record<string, never>,
Expr extends Expression = Expression
>(
paramsDef: Params,
Expand Down
85 changes: 42 additions & 43 deletions packages/generate/src/syntax/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,18 @@ export type SelectModifierNames =
| "offset"
| "limit";

type filterSingle<T extends TypeSet> = T extends ObjectTypeSet
? TypeSet<anonymizeObject<T["__element__"]>, T["__cardinality__"]>
: orLiteralValue<T>;

export type exclusivesToFilterSingle<E extends ExclusiveTuple> =
ExclusiveTuple extends E
? never
: E extends []
? never
: {
[j in keyof E]: {
[k in keyof E[j]]: E[j][k] extends ObjectTypeSet
? TypeSet<
anonymizeObject<E[j][k]["__element__"]>,
E[j][k]["__cardinality__"]
>
: orLiteralValue<E[j][k]>;
[k in keyof E[j]]: filterSingle<E[j][k]>;
};
}[number];
export type SelectModifiers<T extends ObjectType = ObjectType> = {
Expand Down Expand Up @@ -353,10 +352,10 @@ export interface SelectModifierMethods<Root extends TypeSet> {

export type InferOffsetLimitCardinality<
Card extends Cardinality,
Modifers extends UnknownSelectModifiers
> = Modifers["limit"] extends number | LimitExpression
Modifiers extends UnknownSelectModifiers
> = Modifiers["limit"] extends number | LimitExpression
? cardutil.overrideLowerBound<Card, "Zero">
: Modifers["offset"] extends number | OffsetExpression
: Modifiers["offset"] extends number | OffsetExpression
? cardutil.overrideLowerBound<Card, "Zero">
: Card;

Expand Down Expand Up @@ -741,44 +740,42 @@ export type linkDescToLinkProps<Desc extends LinkDesc> = {
export type pointersToObjectType<P extends ObjectTypePointers> = ObjectType<
string,
P,
{}
object
>;

type linkDescToShape<L extends LinkDesc> = objectTypeToSelectShape<
L["target"]
> &
objectTypeToSelectShape<pointersToObjectType<L["properties"]>> &
SelectModifiers;
export type linkDescToSelectElement<L extends LinkDesc> =

type linkDescToSelectElement<L extends LinkDesc> =
| boolean
// | pointerToCastableExpression<Shape[k]>
| TypeSet<anonymizeObject<L["target"]>, cardutil.assignable<L["cardinality"]>>
| linkDescToShape<L>
| ((
scope: $scopify<L["target"]> & linkDescToLinkProps<L>
) => linkDescToShape<L>);

type propDescToSelectElement<P extends PropertyDesc> =
| boolean
| TypeSet<P["target"], cardutil.assignable<P["cardinality"]>>
| $expr_PolyShapeElement;

// object types -> pointers
// pointers -> links
// links -> target object type
// links -> link properties
export type objectTypeToSelectShape<T extends ObjectType = ObjectType> =
// 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<T["__pointers__"][k]["cardinality"]>
>
| $expr_PolyShapeElement
: T["__pointers__"][k] extends LinkDesc
? linkDescToSelectElement<T["__pointers__"][k]>
: 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]>
: Pointers[k] extends LinkDesc
? linkDescToSelectElement<Pointers[k]>
: any;
}> & { [k: string]: unknown };

// incorporate __shape__ (computeds) on selection shapes
// this works but a major rewrite of setToTsType is required
Expand Down Expand Up @@ -865,40 +862,42 @@ function $shape(_a: unknown, b: (...args: any) => any) {
}
export { $shape as shape };

export function select<Expr extends ObjectTypeExpression>(
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<ElementName, ElementPointers, ElementShape>;
__cardinality__: Card;
}>;
export function select<Expr extends TypeSet>(
expr: Expr
): $expr_Select<stripSet<Expr>>;
export function select<
Expr extends ObjectTypeExpression,
Element extends Expr["__element__"],
Shape extends objectTypeToSelectShape<Element> & SelectModifiers<Element>,
SelectCard extends ComputeSelectCardinality<Expr, Modifiers>,
SelectShape extends normaliseShape<Shape, SelectModifierNames>,
Scope extends $scopify<Element> &
$linkPropify<{
[k in keyof Expr]: k extends "__cardinality__"
? Cardinality.One
: Expr[k];
}>,
Shape extends objectTypeToSelectShape<Element> & SelectModifiers<Element>,
ElementName extends `${Element["__name__"]}`,
Modifiers extends UnknownSelectModifiers = Pick<Shape, SelectModifierNames>
>(
expr: Expr,
shape: (scope: Scope) => Readonly<Shape>
): $expr_Select<{
__element__: ObjectType<
`${Element["__name__"]}`, // _shape
Element["__pointers__"],
Omit<normaliseShape<Shape>, SelectModifierNames>
>;
__cardinality__: ComputeSelectCardinality<Expr, Modifiers>;
__element__: ObjectType<ElementName, Element["__pointers__"], SelectShape>;
__cardinality__: SelectCard;
}>;
/*
Expand Down
13 changes: 5 additions & 8 deletions packages/generate/src/syntax/typesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export type BaseTypeTuple = typeutil.tupleOf<BaseType>;

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;
Expand All @@ -48,12 +48,9 @@ export type scalarTypeWithConstructor<
> = S & {
// tslint:disable-next-line
<T extends S["__tstype__"] | ExtraTsTypes>(val: T): $expr_Literal<
ScalarType<
S["__name__"],
S["__tstype__"],
S["__tsargtype__"],
T extends S["__tstype__"] ? T : S["__tstype__"]
>
Omit<S, "__tsconsttype__"> & {
__tsconsttype__: T extends S["__tstype__"] ? T : S["__tstype__"];
}
>;
};

Expand Down

0 comments on commit bf19955

Please sign in to comment.