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

Cache functions on create* for perfomance #1190

Closed
miyaji255 opened this issue Aug 1, 2024 · 2 comments
Closed

Cache functions on create* for perfomance #1190

miyaji255 opened this issue Aug 1, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@miyaji255
Copy link
Contributor

Feature Request

Improves performance by caching functions when creating reusable functions (such as createValidate).

Simple Example

Current Generated Code:

(input: any): typia.IValidation<number> => {
  const errors = [] as any[];
  const __is = (input: any): input is number => {
    return "number" === typeof input;
  };
  if (false === __is(input)) {
    ...
  }
  const success = 0 === errors.length;
  return {
    success,
    errors,
    data: success ? input : undefined,
  } as any;
};

Suggesting Code:

(() => {
  const __is = (input: any): input is number => {
    return "number" === typeof input;
  };
  return (input: any): typia.IValidation<number> => {
    const errors = [] as any[];
    if (false === __is(input)) {
      ...
    }
    const success = 0 === errors.length;
    return {
      success,
      errors,
      data: success ? input : undefined,
    } as any;
  }
})()

Benchmark

I benchmarked createValidate with this interface.

interface IMember {
  id: string & tags.Format<"uuid">;
  name: string;
  age: number &
    tags.Type<"uint32"> &
    tags.Minimum<20> &
    tags.ExclusiveMaximum<100>;
}
(index) Task Name ops/sec Average Time (ns) Margin Samples
0 'cached - OK' '9,340,070' 107.06557722464086 '±0.65%' 4670036
1 'no cached - OK' '2,336,021' 428.07816022288165 '±0.67%' 1168011
2 'cached - Error' '4,019,449' 248.79030713192148 '±0.33%' 2009725
3 'no cached - Error' '1,069,736' 934.8094580170683 '±0.61%' 534869

Benchmark Code:
https://github.com/miyaji255/Typia-Cached-Fucntion-Benchmark

@samchon samchon self-assigned this Aug 1, 2024
@samchon samchon added good first issue Good for newcomers enhancement New feature or request labels Aug 1, 2024
@samchon
Copy link
Owner

samchon commented Aug 1, 2024

Good idea, I'll try this way on weekend.

samchon added a commit to samchon/nestia that referenced this issue Aug 3, 2024
@samchon
Copy link
Owner

samchon commented Aug 3, 2024

compare

Performance be tuned significantly.

@samchon samchon closed this as completed in 60e90c2 Aug 3, 2024
samchon added a commit that referenced this issue Aug 3, 2024
Close #1190: optimization by caching internal functions.
samchon added a commit to samchon/nestia that referenced this issue Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants