-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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: replace std Instant with wasm-compatible wrapper #9189
Conversation
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
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.
Thanks @waynexia
I took a look through the Cargo.lock in my datafusion checkout and it appears that adding instant
would be a new dependency. I am always worried when we potentially increase the dependencies of DataFusion
I, would it be possible to add a wasm
feature flag to datafusion and only pull in the needed libraries optionally?
Having a wasm feature flag might make it easier to disable other parts of the code that aren't compatible with WASM
For example, we could have something like
# in datafusion/common/src/wasm.rs
/// DataFusion wrapper around std::instant
/// uses std::time::Instant normally, but uses
/// instant::Instant library when targeting web assembly
struct Instant {
...
}
🤔
It would also really be nice to get some sort of test / way to run wasm to trigger a bug without changes
I wonder if we could extend https://github.com/apache/arrow-datafusion/tree/main/datafusion/wasmtest 🤔
Marking as draft as I don't think this PR is waiting on review anymore and I am trying to clear the review queue. |
Thanks for your review @alamb 👍 Enclose wasm-specific dependencies and logic with a feature gate looks great to me. I would warp one |
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
I've extracted all the references into I also extended The lock file of |
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
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.
Thanks @waynexia -- I think this looks good to me. I wonder if you want to put an "exclude" type lint in to prevent people from accidentally using std::time::Instant
Similar to how @DDtKey did this in https://github.com/apache/arrow-datafusion/blob/c439bc73b6a9ba9efa4c8a9b5d2fb6111e660e74/clippy.toml#L1-L4 / #9318
datafusion/common/src/wasm.rs
Outdated
|
||
//! WASM-related utilities | ||
|
||
#[cfg(target_family = "wasm")] |
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.
👍
// Parse SQL (using datafusion-sql) | ||
let sql = "SELECT 2 + 37"; | ||
let dialect = GenericDialect {}; // or AnsiDialect, or your own dialect ... | ||
let ast = Parser::parse_sql(&dialect, sql).unwrap(); | ||
log(&format!("Parsed SQL: {ast:?}")); | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { |
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.
💯 for the test
datafusion/common/src/wasm.rs
Outdated
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.
Just one question:
it's wrapper for Instant
, but module name is wasm
while "wasm" it or not depends on target family
A bit confusing to use wasm::Instant
in code, like it's something specific, e.g https://github.com/apache/arrow-datafusion/blob/857a2e34299b7ec5be97b40a5019b15d0fc23bd0/datafusion/core/benches/parquet_query_sql.rs#L28
Shouldn't it be datafusion_common::instant::Instant
instead? This way we fully encapsulate wrapper and can easily extend in the future if we wish
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.
Shouldn't it be
datafusion_common::instant::Instant
instead? This way we fully encapsulate wrapper and can easily extend in the future if we wish
I agree this would be a better home
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.
Makes sense 👍 please see c823e38
Unfortunately, I think it's not possible "as is" for this case, because it's actually just a type alias. However new-type pattern would work (wrap Somebody already tried, btw: rust-lang/rust-clippy#10406 |
Signed-off-by: Ruihang Xia <[email protected]>
TIL, thanks for your information. I find |
Signed-off-by: Ruihang Xia <[email protected]>
Signed-off-by: Ruihang Xia <[email protected]>
041c362
to
d620e32
Compare
Co-authored-by: Artem Medvedev <[email protected]>
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.
@@ -2,3 +2,7 @@ disallowed-methods = [ | |||
{ path = "tokio::task::spawn", reason = "To provide cancel-safety, use `SpawnedTask::spawn` instead (https://github.com/apache/arrow-datafusion/issues/6513)" }, | |||
{ path = "tokio::task::spawn_blocking", reason = "To provide cancel-safety, use `SpawnedTask::spawn` instead (https://github.com/apache/arrow-datafusion/issues/6513)" }, | |||
] | |||
|
|||
disallowed-types = [ | |||
{ path = "std::time::Instant", reason = "Use `datafusion_common::instant::Instant` instead for WASM compatibility" }, |
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 issue does this PR close?
Relates to #7651 and #7652
Rationale for this change
std::time::Instant
doesn't work on targetwasm32-unknown-unknown
. It would result in a runtime panic when using it. A detailed explanation can be found in the replacement wrapper crateinstant
's repository.It doesn't have a difference when runs on other platforms.
What changes are included in this PR?
Replace all the occurrences of
std::time::Instant
withinstant::Instant
.Are these changes tested?
No. I've used the compiled artifact on
wasm32-unknown-unknown
but I'm still trying to figure out how to write UT for that artifact.Are there any user-facing changes?
There is no difference on platforms other than
wasm32-unknown-unknown