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

refactor!: remove unnecessary async from crypto methods #1963

Merged
merged 4 commits into from
Nov 27, 2023

Conversation

tabcat
Copy link
Contributor

@tabcat tabcat commented Aug 12, 2023

Removed async keyword from function which don't:

  1. do any async work
  2. return a promise
  3. have an async browser equivalent

Closes: #1949

@tabcat tabcat force-pushed the async-to-sync branch 4 times, most recently from 7b702b9 to 84d2082 Compare August 14, 2023 15:20
@tabcat tabcat changed the title refactor: Remove unnecessary async methods in crypto package refactor!: Remove unnecessary async methods in crypto package Aug 14, 2023
@tabcat tabcat marked this pull request as ready for review August 14, 2023 17:01
@p-shahi p-shahi linked an issue Aug 15, 2023 that may be closed by this pull request
@maschad maschad changed the title refactor!: Remove unnecessary async methods in crypto package refactor(@libp2p/crypto)!: Remove unnecessary async methods in crypto package Aug 15, 2023
Copy link
Member

@maschad maschad left a comment

Choose a reason for hiding this comment

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

Left a comment. I think there are some other functions such as encrypt and create which could also be made synchronous.

Majority of the functions addressed in 78b008c would have been async without an await so I think many could be made synchronous

packages/crypto/src/ciphers/aes-gcm.ts Outdated Show resolved Hide resolved
@tabcat tabcat force-pushed the async-to-sync branch 3 times, most recently from dd77b85 to 2fd3ebb Compare August 16, 2023 10:19
@tabcat tabcat changed the title refactor(@libp2p/crypto)!: Remove unnecessary async methods in crypto package refactor(@libp2p/crypto)!: Remove unnecessary async methods Aug 16, 2023
@tabcat tabcat marked this pull request as draft August 16, 2023 10:23
@tabcat
Copy link
Contributor Author

tabcat commented Aug 16, 2023

The create function is already made sync. I chose to keep the encrypt function returning a promise because that is what the browser equivalent does. Can change if it's fine to do this.

The other places where the require-await comments were removed still return promises. With current linter rules any function returning a promise must be an async function.

@maschad
Copy link
Member

maschad commented Aug 16, 2023

The create function is already made sync.

My apologies, I had actually intended to link to create function in HMAC

I chose to keep the encrypt function returning a promise because that is what the browser equivalent does. Can change if it's fine to do this.

My understanding is that given they run in different environments that should be inconsequential, but @achingbrain can comment further.

The other places where the require-await comments were removed still return promises. With current linter rules any function returning a promise must be an async function.

The premise was that given the functions are not doing asynchronous work, they should not return a promise. So they need to be refactored to not create promises.

@tabcat
Copy link
Contributor Author

tabcat commented Aug 16, 2023

The premise was that given the functions are not doing asynchronous work, they should not return a promise. So they need to be refactored to not create promises.

There may be some situations where the function being returned could be refactored to not return a promise. I will check for this.

@tabcat
Copy link
Contributor Author

tabcat commented Aug 17, 2023

I've made the hmac create function synchronous. I looked at whether some of the keys methods can be made synchronous and they can, although mainly just for nodejs. Ed25519 can be made synchronous in both the browser and nodejs.

@tabcat
Copy link
Contributor Author

tabcat commented Aug 20, 2023

Ah, hmac's create function for the browser cannot be made sync. Did you talk with @achingbrain yet about if they have to match? Seems like they should, especially if part of public API.

@tabcat
Copy link
Contributor Author

tabcat commented Aug 22, 2023

I think would be best for the node/browser mirror methods to keep parity wrt their interfaces. One returning a value while the other returns a Promise looks like trouble.

There are cases (Ed25519 keys) where both node and browser methods could be synchronous. Since this is not a public API/not exported from package and gets turned into a promise via the public API methods, changing these seems ok.
Same thing with Secp256k1 keys except that there is no node/browser separation here.

@tabcat tabcat marked this pull request as ready for review August 23, 2023 21:22
@tabcat
Copy link
Contributor Author

tabcat commented Aug 24, 2023

i did some benchmarking with synchronous ed25519 in node and the async api is much faster.
also checked over all the async function and i think i covered them all.

@maschad maschad requested a review from a team as a code owner August 30, 2023 19:34
@maschad
Copy link
Member

maschad commented Aug 30, 2023

i did some benchmarking with synchronous ed25519 in node and the async api is much faster.
also checked over all the async function and i think i covered them all.

Could you share the benchmark results here, I find it strange that's the case.

@tabcat
Copy link
Contributor Author

tabcat commented Aug 31, 2023

https://gist.github.com/tabcat/871bca3b48fb23c30ab3ba4dc243b8fa i fixed the benchmarks and the results are:

synchronous x 31,708 ops/sec ±0.81% (96 runs sampled)
asynchronous x 24,510 ops/sec ±1.44% (81 runs sampled)
Fastest is synchronous

I made a mistake with how concurrency is handled in the benchmark

@tabcat tabcat marked this pull request as draft September 2, 2023 15:10
@tabcat
Copy link
Contributor Author

tabcat commented Sep 2, 2023

I've made the ed25519 methods all synchronous, this doesn't change public api since they are later wrapped in a promise inside of keys/index.ts

use the refactor semantic commit tag but maybe chore would be better?

@achingbrain achingbrain marked this pull request as ready for review November 27, 2023 15:59
@achingbrain achingbrain merged commit e2267d4 into libp2p:main Nov 27, 2023
21 checks passed
@achingbrain achingbrain changed the title refactor(@libp2p/crypto)!: Remove unnecessary async methods refactor!: remove unnecessary async from crypto methods Nov 27, 2023
@achingbrain
Copy link
Member

Thanks!

@github-actions github-actions bot mentioned this pull request Nov 27, 2023
maschad added a commit to maschad/js-libp2p that referenced this pull request Nov 27, 2023
Removed async keyword from function which don't:

1) do any async work
2) return a promise
3) have an async browser equivalent

Closes: libp2p#1949

---------

Co-authored-by: Chad Nehemiah <[email protected]>
Co-authored-by: achingbrain <[email protected]>
@github-actions github-actions bot mentioned this pull request Nov 29, 2023
This was referenced Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

refactor: Remove unnecessary async methods in crypto package
3 participants