-
Notifications
You must be signed in to change notification settings - Fork 395
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 |
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! |
@@ -402,7 +425,7 @@ function throwIfProxyReleased(isReleased: boolean) { | |||
} | |||
|
|||
function releaseEndpoint(ep: Endpoint) { | |||
return requestResponseMessage(ep, { | |||
return requestResponseMessage(ep, new Map(), { |
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 did noone ever address this? this now leaks INDEFINETLY, causing ep[finalizer]()
to never be called!!! this means in browsers you will leak threads if you had worker.terminate linked to the exposed [finalizer] property, this promise will never resolve!!!
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.
Which part are you saying is leaking?
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.
doing
class SelfTerminate extends Worker {
[finalize](){
this.terminate() // never fires
}
}
const x = wrap(new SelfTerminate('./w'))
x[releaseProxy]()
means the worker never terminates.... it used to before this change, thats because the releaseEndpoint promise never settles
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.
Hm, I don't understand how this worked before. When releasing the proxy, the finalizer of the exposed object (the object created in the worker) is called, not the finalizer of the SelfTerminate
instance. I have tested this with v4.4.1 which does not include this change.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comlink Worker Example</title>
</head>
<body>
<script type="module">
import * as Comlink from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/comlink.mjs';
// Inline worker definition
const workerCode = `
importScripts('https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/comlink.js');
const workerAPI = {
add(a, b) {
return a + b;
},
// Uncomment function below to have worker close itself when proxy is released.
// [Comlink.finalizer]() {
// console.warn("Closing worker...");
// self.close();
// }
};
Comlink.expose(workerAPI);
`;
class SelfTerminate extends Worker {
[Comlink.finalizer]() {
console.warn("Terminating worker...");
this.terminate();
}
}
const blob = new Blob([workerCode], { type: 'application/javascript' });
const worker = new SelfTerminate(URL.createObjectURL(blob));
(async function() {
const workerAPI = Comlink.wrap(worker);
const result = await workerAPI.add(5, 7);
console.log(`The result of the addition is: ${result}`);
workerAPI[Comlink.releaseProxy]();
})();
</script>
</body>
</html>
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.
ah, sorry, a minor missunderstanding on my part, I wrote this when tired, it needs to be a message port, which then is never closed https://github.com/GoogleChromeLabs/comlink/blob/main/src/comlink.ts#L395
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.
OK got it. I'll submit a PR that fixes this.
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.
await releaseEndpoint(ep, pendingListeners)
function releaseEndpoint (ep: Endpoint, listeners: PendingListenersMap = new Map()) {
return requestResponseMessage(ep, listeners, {
type: MessageType.RELEASE
}).then(() => {
if (finalizer in ep && typeof ep[finalizer] === 'function') {
ep[finalizer]()
}
})
}
is how i fixed it on my end
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 you verify if the change in foxglove#6 also works for you? If yes, then I'll submit a PR to this repo here.
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)