Skip to content

fix: use global() to get performance API on wasm32-unknown-unknown#107

Merged
tobz merged 5 commits intometrics-rs:mainfrom
Officeyutong:main
Dec 12, 2024
Merged

fix: use global() to get performance API on wasm32-unknown-unknown#107
tobz merged 5 commits intometrics-rs:mainfrom
Officeyutong:main

Conversation

@Officeyutong
Copy link
Copy Markdown
Contributor

Currently quanta uses window.performance to access Performance API in browser, which will not work in WebWorkers. In WebWorkers, there is no window object, but performance is in the global space.

This PR uses web_sys::js_sys::global() to access the global scope, and look up for performance in that, which will both work on webpage and WebWorkers.

See https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers for more details

@Officeyutong Officeyutong marked this pull request as ready for review November 23, 2024 14:02
Copy link
Copy Markdown
Member

@tobz tobz left a comment

Choose a reason for hiding this comment

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

Thanks for opening this PR!

Taking a look at the code, it seems like this is going to be somewhat inefficient, reflecting through the global scope on every single call.

Is my intuition wrong here? Could we not instead simply query for the Performance class when constructing Monotonic so that there's always a live reference to it to directly use?

@Officeyutong
Copy link
Copy Markdown
Contributor Author

Thanks for opening this PR!

Taking a look at the code, it seems like this is going to be somewhat inefficient, reflecting through the global scope on every single call.

Is my intuition wrong here? Could we not instead simply query for the Performance class when constructing Monotonic so that there's always a live reference to it to directly use?

You are right, Reflection is relatively slow since its equal to object[attribute] in JS. I'll try to store a reference to Performance class to avoid frequent call to Reflection

@Officeyutong
Copy link
Copy Markdown
Contributor Author

Thanks for opening this PR!

Taking a look at the code, it seems like this is going to be somewhat inefficient, reflecting through the global scope on every single call.

Is my intuition wrong here? Could we not instead simply query for the Performance class when constructing Monotonic so that there's always a live reference to it to directly use?

Updated now. I also splitted wasm.rs to wasm_browser.rs and wasm_wasi.rs, since implementation of these two platforms are completely different

@Officeyutong
Copy link
Copy Markdown
Contributor Author

Hi @tobz , how's this issue going on?

Copy link
Copy Markdown
Member

@tobz tobz left a comment

Choose a reason for hiding this comment

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

Apologies: life and work have been pretty busy lately. :)

I left a common about how we caching the Performance object, and in particular about how we're overriding the fact that it's !Send/!Sync.

Comment thread src/clocks/monotonic/wasm_browser.rs Outdated
Comment thread Cargo.toml
Copy link
Copy Markdown
Member

@tobz tobz left a comment

Choose a reason for hiding this comment

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

Overall, these changes seem reasonable to me. 👍🏻

@Officeyutong
Copy link
Copy Markdown
Contributor Author

Overall, these changes seem reasonable to me. 👍🏻

Hi, when can we get this PR merged

@tobz
Copy link
Copy Markdown
Member

tobz commented Dec 12, 2024

👋🏻

Going to merge these now, but I'll have to fix the MSRV and CI afterwards and make sure things are still green before I can cut a release.

@tobz tobz merged commit d1e11f0 into metrics-rs:main Dec 12, 2024
@tobz
Copy link
Copy Markdown
Member

tobz commented Dec 13, 2024

Released as quanta@v0.12.4.

Thanks again for your contribution!

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