Skip to content

Commit

Permalink
Fixed string cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Mar 3, 2024
1 parent 280222d commit 0d5c990
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
15 changes: 9 additions & 6 deletions ext/polars/src/functions/string_cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::RbResult;
use magnus::{RArray, Ruby, Value};
use polars_core::StringCacheHolder;

pub fn enable_string_cache() {
Expand All @@ -13,14 +15,15 @@ pub fn using_string_cache() -> bool {
}

#[magnus::wrap(class = "Polars::RbStringCacheHolder")]
pub struct RbStringCacheHolder {
_inner: StringCacheHolder,
}
pub struct RbStringCacheHolder;

impl RbStringCacheHolder {
pub fn new() -> Self {
Self {
_inner: StringCacheHolder::hold(),
}
Self {}
}

pub fn hold(&self) -> RbResult<Value> {
let _hold = StringCacheHolder::hold();
Ruby::get().unwrap().yield_splat(RArray::new())
}
}
1 change: 1 addition & 0 deletions ext/polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ fn init(ruby: &Ruby) -> RbResult<()> {
// string cache
let class = module.define_class("RbStringCacheHolder", ruby.class_object())?;
class.define_singleton_method("new", function!(RbStringCacheHolder::new, 0))?;
class.define_method("hold", method!(RbStringCacheHolder::hold, 0))?;

Ok(())
}
9 changes: 2 additions & 7 deletions lib/polars/string_cache.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
module Polars
# @private
# Context manager for enabling and disabling the global string cache.
class StringCache
def initialize
raise Todo

def initialize(&block)
@string_cache = RbStringCacheHolder.new
yield
# TODO fix
@string_cache = nil
@string_cache.hold(&block)
end
end

Expand Down
3 changes: 0 additions & 3 deletions test/docs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ def assert_examples(method, cls)
# TODO fix
return if [:align_frames, :coalesce, :cum_sum_horizontal, :cumsum_horizontal, :to_titlecase].include?(method.name)

# TODO fix string cache
return if [:is_local, :to_local].include?(method.name)

code = ""
method.tags(:example).each do |example|
# use variables from previous examples
Expand Down
11 changes: 11 additions & 0 deletions test/string_cache_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative "test_helper"

class StringCacheTest < Minitest::Test
def test_works
refute Polars.using_string_cache
Polars::StringCache.new do
assert Polars.using_string_cache
end
refute Polars.using_string_cache
end
end

0 comments on commit 0d5c990

Please sign in to comment.