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

feat: optimistically create streams for light protocols #1514

Closed
wants to merge 12 commits into from

Conversation

danisharora099
Copy link
Collaborator

@danisharora099 danisharora099 commented Aug 30, 2023

!! not to be merged -- optimisations and testing to be done !!

Results:

encoder creation: 0.299ms
getPeer: 1.613ms
getStream: 0.761ms
pipe: 321.266ms
lightpush send: 343.507ms
...
encoder creation: 0.049ms
getPeer: 2.344ms
getStream: 0.722ms
pipe: 345.292ms
lightpush send: 349.836ms
...
encoder creation: 0.283ms
getPeer: 3.866ms
getStream: 0.435ms
pipe: 305.904ms
lightpush send: 310.952ms

Thoughts/TODOs:

  • create a StreamManager to increase readability
  • if we send requests over the protocol rapidly (leading to streams being used up after than they are being replenished), then the delay is longer (since the stream is being created adhoc) -- let's add the ability to have multiple streams optimistically created
    • next PR perhaps

@github-actions
Copy link

github-actions bot commented Aug 30, 2023

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 28.67 KB (0%) 574 ms (0%) 2.4 s (+74.52% 🔺) 3 s
Waku Simple Light Node 298.58 KB (+0.19% 🔺) 6 s (+0.19% 🔺) 5.3 s (+10.3% 🔺) 11.3 s
ECIES encryption 28.68 KB (0%) 574 ms (0%) 2 s (-4.31% 🔽) 2.6 s
Symmetric encryption 28.69 KB (0%) 574 ms (0%) 2.3 s (+118.74% 🔺) 2.9 s
DNS discovery 118.6 KB (0%) 2.4 s (0%) 3 s (-1.58% 🔽) 5.3 s
Privacy preserving protocols 122.15 KB (0%) 2.5 s (0%) 2.6 s (-15.29% 🔽) 5.1 s
Light protocols 28.95 KB (+1.93% 🔺) 579 ms (+1.93% 🔺) 1.7 s (+48.92% 🔺) 2.2 s
History retrieval protocols 28.18 KB (+1.96% 🔺) 564 ms (+1.96% 🔺) 1.5 s (+39.8% 🔺) 2 s
Deterministic Message Hashing 5.64 KB (0%) 113 ms (0%) 440 ms (-18.09% 🔽) 553 ms

packages/core/src/lib/base_protocol.ts Show resolved Hide resolved
packages/core/src/lib/base_protocol.ts Show resolved Hide resolved
packages/core/src/lib/base_protocol.ts Show resolved Hide resolved

export class StreamManager {
private streamsPool: Map<string, Stream[]> = new Map();
private ongoingStreamCreations: Map<string, Promise<Stream>> = new Map();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would make it simpler by not having a "pool" of stream, but just one stream.

Also, once you created a stream, I'd just store the Promise<Stream> and that's it. and just return this promise

packages/core/src/lib/stream_manager.ts Show resolved Hide resolved
}

this.log(`No stream available for peer ${peerIdStr}. Opening a new one.`);
return this.createAndSaveStream(peer);
Copy link
Collaborator

Choose a reason for hiding this comment

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

you dont need to "create and save", you just need to return a fresh new stream

Comment on lines +69 to +70
const peerStreams = this.streamsPool.get(peerIdStr) || [];
peerStreams.push(stream);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am pretty sure this could be your race condition. Not 100% sure how it work with the JavaScript event loop.
Maybe @weboko can advise.

In any case, I would not have a array of stream, just have 1

@danisharora099
Copy link
Collaborator Author

closing in favour of #1516

@danisharora099 danisharora099 deleted the feat/optimistic-streams branch December 18, 2023 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants