-
Notifications
You must be signed in to change notification settings - Fork 391
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
Use a single "message" event listener to dispatch received messages #653
Use a single "message" event listener to dispatch received messages #653
Conversation
Co-authored-by: Roman Shtylman <[email protected]>
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
src/comlink.ts
Outdated
return; | ||
} | ||
const resolver = pendingListeners.get(data.id); | ||
if (resolver) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: flip this around to early return so that early return is the "no more work to do case"
if (!resolver) {
return;
}
// actual work
...
} | ||
}); | ||
|
||
return createProxy<T>(ep, pendingListeners, [], target) as any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this case as any
? I know this was also true of the earlier code but in our code (foxglove) we always avoid any
- are we not able to get the correct type here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. But seems like it's not that easy, quoting the README:
Comlink does provide TypeScript types. When you
expose()
something of typeT
, the correspondingwrap()
call will return something of typeComlink.Remote<T>
. While this type has been battle-tested over some time now, it is implemented on a best-effort basis. There are some nuances that are incredibly hard if not impossible to encode correctly in TypeScript’s type system. It may sometimes be necessary to force a certain type usingas unknown as <type>
.
ep.removeEventListener("message", l as any); | ||
resolve(ev.data); | ||
} as any); | ||
pendingListeners.set(id, resolve); | ||
if (ep.start) { | ||
ep.start(); | ||
} | ||
ep.postMessage({ id, ...msg }, transfers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Random thought for the future - but it would be nice to avoid this spread operator - I don't see any reason for it when we could instead have id, payload
or similar. While not a massive perf issue, removing it is fewer work cycles for the runtime with no downside to the interface or logic since this is an internal structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be something for a separate PR?
This change had a huge impact on my work with a physics library. I am calling a function on main thread to add physics bodies in the worker thread. Originally it was a huge hot path, because I was stress testing creating 5000 bodies in one 16ms tick. I thought I would have to instead batch up the commands and parameters and send over in one message (which still might be a good idea), instead I can now continue to use the comlink proxy for the function. Nice work. I am now using the foxglove fork for my project to pick up the other nice to haves like faster id generation. |
@achim-k Thank you for this change! I think the performance improvements are great. Do you still need to follow up on something or at this point are just waiting for the final PR approval? |
This is just waiting for the final approval. We are already using this in production for a couple of months, without issues. |
@surma is this PR still interesting from the maintainer's perspective? I understand that you might have different opinions on how this should be implemented. I think this is a useful change for high-throughput applications. |
Bump |
is there any ETA for this? with this comlink could be adapted for use with generic message channels, such as websocket, text, etc and no longer be bound to just web workers |
Comlink is abandonware. I use forks for now but will probably migrate to
transporter or some other library.
…On Mon, 16 Sept 2024, 17:02 Cas_, ***@***.***> wrote:
is there any ETA for this? with this comlink could be adapted for use with
generic message channels, such as websocket, text, etc and no longer be
bound to just web workers
—
Reply to this email directly, view it on GitHub
<#653 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVSGJ63U63QRSDERGZ46C3ZW3XIJAVCNFSM6AAAAABCBAGELOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJTGE3TSMJRGM>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Thanks so much.
published in v4.4.2 |
Thank you! |
From #649:
This PR is similar to #649 and #651 but avoids that references to the resolve functions or the endpoint are kept in memory.
The performance impact of this PR gets more noticeable the more parallel requests are made:
See also #651 (comment)