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

disable tsfn calls with null. #40

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ crate-type = ["cdylib", "lib"]
[dependencies]
ahash = "0.7"
bincode = "1.3"
napi = {version = "2.10", default-features = false, features = ["napi8", "serde-json", "experimental"]}
napi-derive = {version = "2.9", default-features = false}
napi = {version = "2.10.9", default-features = false, features = ["napi8", "serde-json", "experimental"]}
napi-derive = {version = "2.9.4", default-features = false}
polars-core = {git = "https://github.com/pola-rs/polars.git", rev = "43598c3e21a2b6c1246a565005045ef553e4f688", default-features = false}
thiserror = "1.0.20"

Expand Down Expand Up @@ -92,7 +92,7 @@ git = "https://github.com/pola-rs/polars.git"
rev = "43598c3e21a2b6c1246a565005045ef553e4f688"

[build-dependencies]
napi-build = "2"
napi-build = "2.0.1"

[profile.release]
codegen-units = 1
Expand Down
155 changes: 79 additions & 76 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,42 +1146,43 @@ impl JsDataFrame {
}
#[napi]
pub fn to_rows_cb(&self, callback: napi::JsFunction, env: Env) -> napi::Result<()> {
use napi::threadsafe_function::*;
use polars_core::utils::rayon::prelude::*;
let (height, _) = self.df.shape();
let tsfn: ThreadsafeFunction<
Either<Vec<JsAnyValue>, napi::JsNull>,
ErrorStrategy::CalleeHandled,
> = callback.create_threadsafe_function(
0,
|ctx: ThreadSafeCallContext<Either<Vec<JsAnyValue>, napi::JsNull>>| Ok(vec![ctx.value]),
)?;

polars_core::POOL.install(|| {
(0..height).into_par_iter().for_each(|idx| {
let tsfn = tsfn.clone();
let values = self
.df
.get_columns()
.iter()
.map(|s| {
let av: JsAnyValue = s.get(idx).into();
av
})
.collect::<Vec<_>>();

tsfn.call(
Ok(Either::A(values)),
ThreadsafeFunctionCallMode::NonBlocking,
);
});
});
tsfn.call(
Ok(Either::B(env.get_null().unwrap())),
ThreadsafeFunctionCallMode::NonBlocking,
);

Ok(())
panic!("not implemented");
// use napi::threadsafe_function::*;
// use polars_core::utils::rayon::prelude::*;
// let (height, _) = self.df.shape();
// let tsfn: ThreadsafeFunction<
// Either<Vec<JsAnyValue>, napi::JsNull>,
// ErrorStrategy::CalleeHandled,
// > = callback.create_threadsafe_function(
// 0,
// |ctx: ThreadSafeCallContext<Either<Vec<JsAnyValue>, napi::JsNull>>| Ok(vec![ctx.value]),
// )?;

// polars_core::POOL.install(|| {
// (0..height).into_par_iter().for_each(|idx| {
// let tsfn = tsfn.clone();
// let values = self
// .df
// .get_columns()
// .iter()
// .map(|s| {
// let av: JsAnyValue = s.get(idx).into();
// av
// })
// .collect::<Vec<_>>();

// tsfn.call(
// Ok(Either::A(values)),
// ThreadsafeFunctionCallMode::NonBlocking,
// );
// });
// });
// tsfn.call(
// Ok(Either::B(env.get_null().unwrap())),
// ThreadsafeFunctionCallMode::NonBlocking,
// );

// Ok(())
}
#[napi]
pub fn to_row_obj(&self, idx: Either<i64, f64>, env: Env) -> napi::Result<Object> {
Expand Down Expand Up @@ -1221,48 +1222,50 @@ impl JsDataFrame {
}
Ok(rows)
}

#[napi]
pub fn to_objects_cb(&self, callback: napi::JsFunction, env: Env) -> napi::Result<()> {
use napi::threadsafe_function::*;
use polars_core::utils::rayon::prelude::*;
use std::collections::HashMap;
let (height, _) = self.df.shape();
let tsfn: ThreadsafeFunction<
Either<HashMap<String, JsAnyValue>, napi::JsNull>,
ErrorStrategy::CalleeHandled,
> = callback.create_threadsafe_function(
0,
|ctx: ThreadSafeCallContext<Either<HashMap<String, JsAnyValue>, napi::JsNull>>| {
Ok(vec![ctx.value])
},
)?;

polars_core::POOL.install(|| {
(0..height).into_par_iter().for_each(|idx| {
let tsfn = tsfn.clone();
let values = self
.df
.get_columns()
.iter()
.map(|s| {
let key = s.name().to_owned();
let av: JsAnyValue = s.get(idx).into();
(key, av)
})
.collect::<HashMap<_, _>>();

tsfn.call(
Ok(Either::A(values)),
ThreadsafeFunctionCallMode::NonBlocking,
);
});
});
tsfn.call(
Ok(Either::B(env.get_null().unwrap())),
ThreadsafeFunctionCallMode::NonBlocking,
);

Ok(())
panic!("not implemented");
// use napi::threadsafe_function::*;
// use polars_core::utils::rayon::prelude::*;
// use std::collections::HashMap;
// let (height, _) = self.df.shape();
// let tsfn: ThreadsafeFunction<
// Either<HashMap<String, JsAnyValue>, napi::JsNull>,
// ErrorStrategy::CalleeHandled,
// > = callback.create_threadsafe_function(
// 0,
// |ctx: ThreadSafeCallContext<Either<HashMap<String, JsAnyValue>, napi::JsNull>>| {
// Ok(vec![ctx.value])
// },
// )?;

// polars_core::POOL.install(|| {
// (0..height).into_par_iter().for_each(|idx| {
// let tsfn = tsfn.clone();
// let values = self
// .df
// .get_columns()
// .iter()
// .map(|s| {
// let key = s.name().to_owned();
// let av: JsAnyValue = s.get(idx).into();
// (key, av)
// })
// .collect::<HashMap<_, _>>();

// tsfn.call(
// Ok(Either::A(values)),
// ThreadsafeFunctionCallMode::NonBlocking,
// );
// });
// });
// tsfn.call(
// Ok(Either::B(env.get_null().unwrap())),
// ThreadsafeFunctionCallMode::NonBlocking,
// );

// Ok(())
}

#[napi]
Expand Down