Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Sep 25, 2023
1 parent fe0fc2a commit 24a08b3
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,25 @@ impl Default for StringCacheHolder {
impl StringCacheHolder {
/// Hold the StringCache
pub fn hold() -> StringCacheHolder {
set_string_cache(true);
increment_string_cache_refcount();
StringCacheHolder { private_zst: () }
}
}

impl Drop for StringCacheHolder {
fn drop(&mut self) {
set_string_cache(false)
decrement_string_cache_refcount();
}
}

/// Increment or decrement the number of string cache uses.
fn set_string_cache(active: bool) {
if active {
STRING_CACHE_REFCOUNT.fetch_add(1, Ordering::Release);
} else {
let previous = STRING_CACHE_REFCOUNT.fetch_sub(1, Ordering::Release);
if previous == 0 || previous == 1 {
STRING_CACHE_REFCOUNT.store(0, Ordering::Release);
STRING_CACHE.clear()
}
fn increment_string_cache_refcount() {
STRING_CACHE_REFCOUNT.fetch_add(1, Ordering::Release);
}
fn decrement_string_cache_refcount() {
let previous = STRING_CACHE_REFCOUNT.fetch_sub(1, Ordering::Release);
if previous == 0 || previous == 1 {
STRING_CACHE_REFCOUNT.store(0, Ordering::Release);
STRING_CACHE.clear()
}
}

Expand All @@ -90,7 +88,7 @@ fn set_string_cache(active: bool) {
pub fn enable_string_cache() {
let was_enabled = STRING_CACHE_ENABLED.swap(true, Ordering::AcqRel);
if !was_enabled {
set_string_cache(true)
increment_string_cache_refcount();
}
}

Expand All @@ -101,7 +99,7 @@ pub fn enable_string_cache() {
pub fn disable_string_cache() {
let was_enabled = STRING_CACHE_ENABLED.swap(false, Ordering::AcqRel);
if was_enabled {
set_string_cache(false)
decrement_string_cache_refcount();
}
}

Expand Down

0 comments on commit 24a08b3

Please sign in to comment.