mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Converted perf function to be a macro. Utilized the perf macro within the polars plugin. (#13224)
In this pull request, I converted the `perf` function within `nu_utils` to a macro. This change facilitates easier usage within plugins by allowing the use of `env_logger` and setting `RUST_LOG=nu_plugin_polars` (or another plugin). Without this conversion, the `RUST_LOG` variable would need to be set to `RUST_LOG=nu_utils::utils`, which is less intuitive and impossible to narrow the perf results to one plugin.
This commit is contained in:
@ -20,6 +20,7 @@ bench = false
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.95.1" }
|
||||
nu-plugin = { path = "../nu-plugin", version = "0.95.1" }
|
||||
nu-path = { path = "../nu-path", version = "0.95.1" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.95.1" }
|
||||
|
||||
# Potential dependencies for extras
|
||||
chrono = { workspace = true, features = ["std", "unstable-locales"], default-features = false }
|
||||
@ -36,6 +37,8 @@ polars-ops = { version = "0.40"}
|
||||
polars-plan = { version = "0.40", features = ["regex"]}
|
||||
polars-utils = { version = "0.40"}
|
||||
typetag = "0.2"
|
||||
env_logger = "0.11.3"
|
||||
log.workspace = true
|
||||
uuid = { version = "1.9", features = ["v4", "serde"] }
|
||||
|
||||
[dependencies.polars]
|
||||
@ -78,4 +81,4 @@ nu-engine = { path = "../nu-engine", version = "0.95.1" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.95.1" }
|
||||
nu-command = { path = "../nu-command", version = "0.95.1" }
|
||||
nu-plugin-test-support = { path = "../nu-plugin-test-support", version = "0.95.1" }
|
||||
tempfile.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
23
crates/nu_plugin_polars/src/cache/mod.rs
vendored
23
crates/nu_plugin_polars/src/cache/mod.rs
vendored
@ -13,7 +13,9 @@ use nu_plugin::{EngineInterface, PluginCommand};
|
||||
use nu_protocol::{LabeledError, ShellError, Span};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{plugin_debug, values::PolarsPluginObject, EngineWrapper, PolarsPlugin};
|
||||
use crate::{values::PolarsPluginObject, EngineWrapper, PolarsPlugin};
|
||||
|
||||
use log::debug;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CacheValue {
|
||||
@ -60,22 +62,16 @@ impl Cache {
|
||||
|
||||
let removed = if force || reference_count.unwrap_or_default() < 1 {
|
||||
let removed = lock.remove(key);
|
||||
plugin_debug!(
|
||||
engine,
|
||||
"PolarsPlugin: removing {key} from cache: {removed:?}"
|
||||
);
|
||||
debug!("PolarsPlugin: removing {key} from cache: {removed:?}");
|
||||
removed
|
||||
} else {
|
||||
plugin_debug!(
|
||||
engine,
|
||||
"PolarsPlugin: decrementing reference count for {key}"
|
||||
);
|
||||
debug!("PolarsPlugin: decrementing reference count for {key}");
|
||||
None
|
||||
};
|
||||
|
||||
// Once there are no more entries in the cache
|
||||
// we can turn plugin gc back on
|
||||
plugin_debug!(engine, "PolarsPlugin: Cache is empty enabling GC");
|
||||
debug!("PolarsPlugin: Cache is empty enabling GC");
|
||||
engine.set_gc_disabled(false).map_err(LabeledError::from)?;
|
||||
drop(lock);
|
||||
Ok(removed)
|
||||
@ -91,14 +87,11 @@ impl Cache {
|
||||
span: Span,
|
||||
) -> Result<Option<CacheValue>, ShellError> {
|
||||
let mut lock = self.lock()?;
|
||||
plugin_debug!(
|
||||
engine,
|
||||
"PolarsPlugin: Inserting {uuid} into cache: {value:?}"
|
||||
);
|
||||
debug!("PolarsPlugin: Inserting {uuid} into cache: {value:?}");
|
||||
// turn off plugin gc the first time an entry is added to the cache
|
||||
// as we don't want the plugin to be garbage collected if there
|
||||
// is any live data
|
||||
plugin_debug!(engine, "PolarsPlugin: Cache has values disabling GC");
|
||||
debug!("PolarsPlugin: Cache has values disabling GC");
|
||||
engine.set_gc_disabled(true).map_err(LabeledError::from)?;
|
||||
let cache_value = CacheValue {
|
||||
uuid,
|
||||
|
@ -1,10 +1,10 @@
|
||||
use crate::{
|
||||
dataframe::values::NuSchema,
|
||||
perf,
|
||||
values::{CustomValueSupport, NuLazyFrame},
|
||||
PolarsPlugin,
|
||||
EngineWrapper, PolarsPlugin,
|
||||
};
|
||||
use nu_path::expand_path_with;
|
||||
use nu_utils::perf;
|
||||
|
||||
use super::super::values::NuDataFrame;
|
||||
use nu_plugin::PluginCommand;
|
||||
@ -399,13 +399,10 @@ fn from_jsonl(
|
||||
inner: vec![],
|
||||
})?;
|
||||
|
||||
perf(
|
||||
engine,
|
||||
perf!(
|
||||
"Lazy json lines dataframe open",
|
||||
start_time,
|
||||
file!(),
|
||||
line!(),
|
||||
column!(),
|
||||
engine.use_color()
|
||||
);
|
||||
|
||||
let df = NuLazyFrame::new(false, df);
|
||||
@ -441,13 +438,10 @@ fn from_jsonl(
|
||||
})?
|
||||
.into();
|
||||
|
||||
perf(
|
||||
engine,
|
||||
perf!(
|
||||
"Eager json lines dataframe open",
|
||||
start_time,
|
||||
file!(),
|
||||
line!(),
|
||||
column!(),
|
||||
engine.use_color()
|
||||
);
|
||||
|
||||
df.cache_and_to_value(plugin, engine, call.head)
|
||||
@ -524,14 +518,7 @@ fn from_csv(
|
||||
})?
|
||||
.into();
|
||||
|
||||
perf(
|
||||
engine,
|
||||
"Lazy CSV dataframe open",
|
||||
start_time,
|
||||
file!(),
|
||||
line!(),
|
||||
column!(),
|
||||
);
|
||||
perf!("Lazy CSV dataframe open", start_time, engine.use_color());
|
||||
|
||||
df.cache_and_to_value(plugin, engine, call.head)
|
||||
} else {
|
||||
@ -569,14 +556,7 @@ fn from_csv(
|
||||
inner: vec![],
|
||||
})?;
|
||||
|
||||
perf(
|
||||
engine,
|
||||
"Eager CSV dataframe open",
|
||||
start_time,
|
||||
file!(),
|
||||
line!(),
|
||||
column!(),
|
||||
);
|
||||
perf!("Eager CSV dataframe open", start_time, engine.use_color());
|
||||
|
||||
let df = NuDataFrame::new(false, df);
|
||||
df.cache_and_to_value(plugin, engine, call.head)
|
||||
|
@ -45,52 +45,6 @@ impl EngineWrapper for &EngineInterface {
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! plugin_debug {
|
||||
($env_var_provider:tt, $($arg:tt)*) => {{
|
||||
if $env_var_provider.get_env_var("POLARS_PLUGIN_DEBUG")
|
||||
.filter(|s| s == "1" || s == "true")
|
||||
.is_some() {
|
||||
eprintln!($($arg)*);
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn perf(
|
||||
env: impl EngineWrapper,
|
||||
msg: &str,
|
||||
dur: std::time::Instant,
|
||||
file: &str,
|
||||
line: u32,
|
||||
column: u32,
|
||||
) {
|
||||
if env
|
||||
.get_env_var("POLARS_PLUGIN_PERF")
|
||||
.filter(|s| s == "1" || s == "true")
|
||||
.is_some()
|
||||
{
|
||||
if env.use_color() {
|
||||
eprintln!(
|
||||
"perf: {}:{}:{} \x1b[32m{}\x1b[0m took \x1b[33m{:?}\x1b[0m",
|
||||
file,
|
||||
line,
|
||||
column,
|
||||
msg,
|
||||
dur.elapsed(),
|
||||
);
|
||||
} else {
|
||||
eprintln!(
|
||||
"perf: {}:{}:{} {} took {:?}",
|
||||
file,
|
||||
line,
|
||||
column,
|
||||
msg,
|
||||
dur.elapsed(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PolarsPlugin {
|
||||
pub(crate) cache: Cache,
|
||||
|
@ -5,5 +5,6 @@ use nu_plugin_polars::PolarsPlugin;
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
serve_plugin(&PolarsPlugin::default(), MsgPackSerializer {})
|
||||
}
|
||||
|
Reference in New Issue
Block a user