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

crypto: Remove useless if statement and convert if-else-if chain #14911

Closed
wants to merge 1 commit into from

Conversation

starkwang
Copy link
Contributor

@starkwang starkwang commented Aug 18, 2017

One if statement in ECDH.getPublicKey is useless. This change
is to remove it and convert the following if-else-if chain to a switch
statement, which is more efficient.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

crypto

The if statement in `ECDH.getPublicKey` is useless. This change
is to remove it and convert the `if-else-if` chain to a `switch`
statement, which is more efficient.
@nodejs-github-bot nodejs-github-bot added the crypto Issues and PRs related to the crypto subsystem. label Aug 18, 2017
Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

I don't think this change is an improvement in legibility over the existing code.

f = constants.POINT_CONVERSION_UNCOMPRESSED;
break;
default:
throw new TypeError('Bad format: ' + format);
Copy link
Member

Choose a reason for hiding this comment

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

This looks like it's breaking the typeof format === 'number case.

Copy link
Contributor Author

@starkwang starkwang Aug 18, 2017

Choose a reason for hiding this comment

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

The typeof format === 'number is no use right now. It only assign a value to the f and then throw an error without using f.

Copy link
Member

Choose a reason for hiding this comment

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

This is true but the error should also be thrown in case the format.length will match one of the entries without matching the correct word. That is not the case anymore.

throw new TypeError('Bad format: ' + format);
switch (format.length) {
case 6:
if (format === 'hybrid')
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this is faster? There are 2 checks now, the original only had one.

Copy link
Contributor Author

@starkwang starkwang Aug 18, 2017

Choose a reason for hiding this comment

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

It will go through all the cases in the if-else-if chain. The switch statement reduces it into 2 checks. Same code in https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L513

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

Sorry but I think that this hurts readability. I'd also like to see a benchmark back up any performance claims. Thanks for your contribution.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

Either each if should get a else path with the error or another if statement should be added to check for f !== undefined. The latter would remove the benefit of having two checks in average instead of 1-3. As the performance is negligible for one - three checks I think it is nicer to read with the if else as it was before. So only the obsolete if should be removed out of my perspective.

f = constants.POINT_CONVERSION_UNCOMPRESSED;
break;
default:
throw new TypeError('Bad format: ' + format);
Copy link
Member

Choose a reason for hiding this comment

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

This is true but the error should also be thrown in case the format.length will match one of the entries without matching the correct word. That is not the case anymore.

@starkwang starkwang closed this Aug 24, 2017
@BridgeAR
Copy link
Member

@starkwang I think if you reopen this and only remove the useless if statement, everyone would approve.

@starkwang
Copy link
Contributor Author

starkwang commented Aug 26, 2017

@BridgeAR I've just created a new PR for this: #15041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants