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

perf(core): optimize reflector #10112

Merged
merged 1 commit into from Sep 19, 2022

Conversation

zanminkian
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@@ -86,6 +86,6 @@ export class Reflector {
metadataKey: TKey,
targets: (Type<any> | Function)[],
): TResult {
return this.getAll(metadataKey, targets).find(item => item !== undefined);
return targets.find(target => this.get(metadataKey, target) !== undefined);
Copy link
Member

@jmcdo29 jmcdo29 Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this instead of the this.getAll()? Just for an early stop if a value is found?

Comment on lines +89 to +95
for (const target of targets) {
const result = this.get(metadataKey, target);
if (result !== undefined) {
return result;
}
}
return undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to refactor this to a raw for loop, we should do the same to the getAll for squeezing as much performance out as we can.

Side note: do you have any benchmarks for how much of an improvement this is? Just curious if the improvement is going to be worth the readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think raw for loop is not the mainly reason. Just break the loop earlier to increase the performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems not increase too much. This is a choice between a little performance and readability. :P

function get(num) {
    for(let i = 0; i < 100; i++) {
        num++
    }
    return num
}

function test1() {
    const start = process.hrtime.bigint();
    for(let i = 0; i< 10000; i++) {
        const targets = [100, 1000, 10000]
        targets.map(target=>get(target)).find(item => item !== undefined)
    }
    console.log(process.hrtime.bigint()-start);
}

function test2() {
    const start = process.hrtime.bigint();
    for(let i = 0; i< 10000; i++) {
        const targets = [100, 1000, 10000]
        for(const target of targets) {
            const res = get(target)
            if(res !== undefined) {
                break;
            }
        }
    }
    console.log(process.hrtime.bigint()-start);
}

function test3() {
    const start = process.hrtime.bigint();
    for(let i = 0; i< 10000; i++) {
        const targets = [100, 1000, 10000]
        for(const target of targets) {
            const res = get(target)
            if(res !== undefined) {
                // break;
            }
        }
    }
    console.log(process.hrtime.bigint()-start);
}

test1() // 119022900n
test2() // 4902500n
test3() // 7564600n

@coveralls
Copy link

Pull Request Test Coverage Report for Build f3608146-d290-4a7e-a1ce-5fbbf02b5f37

  • 6 of 7 (85.71%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 93.813%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/services/reflector.service.ts 6 7 85.71%
Files with Coverage Reduction New Missed Lines %
packages/core/repl/repl-context.ts 1 85.54%
Totals Coverage Status
Change from base Build 7429007a-3efb-492d-a2a1-84f706a41b5f: -0.01%
Covered Lines: 6096
Relevant Lines: 6498

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants