Skip to content

Commit

Permalink
Remove with_string_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Sep 25, 2023
1 parent b2cadcd commit 28b97a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,13 @@ pub fn enable_string_cache() {
///
/// **Warning**: Disabling the string cache this way may cause errors if there
/// are other threads that rely the global string cache being enabled.
/// Consider using either [`with_string_cache`] or [`StringCacheHolder`] for a
/// more reliable way of enabling and disabling the string cache.
/// Consider using [`StringCacheHolder`] for a more reliable way of enabling
/// and disabling the string cache.
pub fn disable_string_cache() {
USE_STRING_CACHE.store(0, Ordering::Release);
STRING_CACHE.clear()
}

/// Execute a function with the global string cache enabled.
pub fn with_string_cache<F: FnOnce() -> T, T>(func: F) -> T {
set_string_cache(true);
let out = func();
set_string_cache(false);
out
}

/// Check whether the global string cache is enabled.
pub fn using_string_cache() -> bool {
USE_STRING_CACHE.load(Ordering::Acquire) > 0
Expand Down
32 changes: 15 additions & 17 deletions crates/polars/tests/it/lazy/predicate_queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// used only if feature="is_in", feature="dtype-categorical"
#[allow(unused_imports)]
use polars_core::{with_string_cache, SINGLE_LOCK};
use polars_core::{StringCacheHolder, SINGLE_LOCK};

use super::*;

Expand Down Expand Up @@ -132,24 +132,22 @@ fn test_is_in_categorical_3420() -> PolarsResult<()> {
]?;

let _guard = SINGLE_LOCK.lock();
disable_string_cache();
let _sc = StringCacheHolder::hold();

let _: PolarsResult<_> = with_string_cache(|| {
let s = Series::new("x", ["a", "b", "c"]).strict_cast(&DataType::Categorical(None))?;
let out = df
.lazy()
.with_column(col("a").strict_cast(DataType::Categorical(None)))
.filter(col("a").is_in(lit(s).alias("x")))
.collect()?;

let mut expected = df![
"a" => ["a", "b", "c"],
"b" => [1, 2, 3]
]?;
expected.try_apply("a", |s| s.cast(&DataType::Categorical(None)))?;
assert!(out.frame_equal(&expected));
let s = Series::new("x", ["a", "b", "c"]).strict_cast(&DataType::Categorical(None))?;
let out = df
.lazy()
.with_column(col("a").strict_cast(DataType::Categorical(None)))
.filter(col("a").is_in(lit(s).alias("x")))
.collect()?;

Ok(())
});
let mut expected = df![
"a" => ["a", "b", "c"],
"b" => [1, 2, 3]
]?;
expected.try_apply("a", |s| s.cast(&DataType::Categorical(None)))?;
assert!(out.frame_equal(&expected));
Ok(())
}

Expand Down

0 comments on commit 28b97a6

Please sign in to comment.