Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize assertType() and validate() function #281

Closed
samchon opened this issue Oct 28, 2022 · 3 comments
Closed

Optimize assertType() and validate() function #281

samchon opened this issue Oct 28, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@samchon
Copy link
Owner

samchon commented Oct 28, 2022

Current assertType() function has programmed to throws an exception directly when found an invalid type through if condition. Therefore, too much throw exception statements are written in generated code and it makes iteration speed slower. However, if I change the program to throw only at once in the last line, the iteration speed would be faster.

Current code

// EXCEPTION THROWING FUNCTION
export function $predicate(
    matched: boolean,
    exceptionable: boolean,
    closure: () => Omit<TypeGuardError.IProps, "method">,
): boolean {
    if (matched === false && exceptionable === true)
        throw new TypeGuardError({
            method: "TSON.assertType",
            ...closure(),
        });
    return matched;
}

// GENERATED CODE
(input, path, exceptionable) =>
    $predicate(
        "number" === typeof input.x,
        exceptionable,
        () => ({
            path: path + ".x",
            expected: "number",
            value: input.x,
        }),
    ) &&
    $predicate(
        "number" === typeof input.y,
        exceptionable,
        () => ({
            path: path + ".y",
            expected: "number",
            value: input.y,
        }),
    ) &&
    $predicate(
        "number" === typeof input.z,
        exceptionable,
        () => ({
            path: path + ".z",
            expected: "number",
            value: input.z,
        }),
    )

Future

// RETURNING FUNCTION
export function $predicate(
    matched: boolean,
    closure: () => Omit<TypeGuardError.IProps, "method">,
): TypeGuardError | null {
    return matched 
        ? null 
        : new TypeGuardError({
            method: "TSON.assertType",
            ...closure(),
        });
}

// ERROR OR NULL STATEMENT
const error = ((input, path, exceptionable) =>
    $predicate("number" === typeof input.x, () => ({
        path: path + ".x",
        expected: "number",
        value: input.x,
    })) ||
    $predicate("number" === typeof input.y, () => ({
        path: path + ".y",
        expected: "number",
        value: input.y,
    })) ||
    $predicate("number" === typeof input.z, () => ({
        path: path + ".z",
        expected: "number",
        value: input.z,
    })))(input);
if (error !== null)
    throw error;
return input;
@samchon samchon added the enhancement New feature or request label Oct 28, 2022
@samchon samchon self-assigned this Oct 28, 2022
@samchon
Copy link
Owner Author

samchon commented Oct 28, 2022

Timepoint iterate throw
Before 27027.907814187973 152.88035450516986
After 156.1754450357864 117.852546423975

Became much slower...

samchon added a commit that referenced this issue Oct 29, 2022
@samchon samchon changed the title Make assertType() code to throw only at once Optimize assertType() and validate() function Oct 30, 2022
samchon added a commit that referenced this issue Oct 30, 2022
Close #281 - optimize `assertType()` and `validate()`
samchon added a commit that referenced this issue Oct 30, 2022
@samchon
Copy link
Owner Author

samchon commented Oct 30, 2022

Succeeded to tuning the performence.

Below is the reuslt and it would be published as 3.3.17 version.

image

samchon added a commit that referenced this issue Oct 30, 2022
samchon added a commit that referenced this issue Oct 30, 2022
Complement #281 - make `validate()` shorter
samchon added a commit that referenced this issue Oct 30, 2022
Complement #281 - `assertType@v3` became faster
@samchon
Copy link
Owner Author

samchon commented Oct 30, 2022

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by AMD 5800H

image

Measured by AMD 5800H

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by AMD 5900HX

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by Intel i5-1135g7, Surface Pro 8

image

Measured by Intel i5-1135g7, Surface Pro 8

samchon added a commit that referenced this issue Oct 30, 2022
samchon added a commit that referenced this issue Oct 30, 2022
samchon added a commit that referenced this issue Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant