Skip to content

Commit 2bbc924

Browse files
committed
refactor: atualizar versão do pacote quickleaf para 0.4.3 e otimizar operações de filtro
1 parent 553932a commit 2bbc924

File tree

7 files changed

+86
-206
lines changed

7 files changed

+86
-206
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "quickleaf"
3-
version = "0.4.2"
3+
version = "0.4.3"
44
edition = "2021"
55
license = "Apache-2.0"
66
authors = ["Philippe Assis <codephilippe@gmail.com>"]

src/cache.rs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::error::Error;
22
use crate::event::Event;
3-
use crate::fast_filters::apply_filter_fast;
3+
use crate::filters::apply_filter_fast;
44
use crate::list_props::{ListProps, Order, StartAfter};
55
use crate::prefetch::{Prefetch, PrefetchExt};
6-
use crate::string_pool::StringPool;
76
use indexmap::IndexMap;
87
use std::fmt::Debug;
98
use std::sync::mpsc::Sender;
@@ -222,7 +221,6 @@ pub struct Cache {
222221
capacity: usize,
223222
default_ttl: Option<Duration>,
224223
sender: Option<Sender<Event>>,
225-
string_pool: StringPool,
226224
#[cfg(feature = "persist")]
227225
persist_path: Option<std::path::PathBuf>,
228226
_phantom: std::marker::PhantomData<Value>,
@@ -254,7 +252,6 @@ impl Cache {
254252
capacity,
255253
default_ttl: None,
256254
sender: None,
257-
string_pool: StringPool::new(),
258255
#[cfg(feature = "persist")]
259256
persist_path: None,
260257
_phantom: std::marker::PhantomData,
@@ -285,7 +282,6 @@ impl Cache {
285282
capacity,
286283
default_ttl: None,
287284
sender: Some(sender),
288-
string_pool: StringPool::new(),
289285
#[cfg(feature = "persist")]
290286
persist_path: None,
291287
_phantom: std::marker::PhantomData,
@@ -312,7 +308,6 @@ impl Cache {
312308
capacity,
313309
default_ttl: Some(default_ttl),
314310
sender: None,
315-
string_pool: StringPool::new(),
316311
#[cfg(feature = "persist")]
317312
persist_path: None,
318313
_phantom: std::marker::PhantomData,
@@ -347,7 +342,6 @@ impl Cache {
347342
capacity,
348343
default_ttl: Some(default_ttl),
349344
sender: Some(sender),
350-
string_pool: StringPool::new(),
351345
#[cfg(feature = "persist")]
352346
persist_path: None,
353347
_phantom: std::marker::PhantomData,
@@ -693,42 +687,32 @@ impl Cache {
693687
/// ```
694688
pub fn insert<T, V>(&mut self, key: T, value: V)
695689
where
696-
T: Into<String> + Clone + AsRef<str>,
690+
T: Into<String>,
697691
V: ToValueBehavior,
698692
{
699-
let key_str = key.as_ref();
700-
701-
let interned_key = if key_str.len() < 50 {
702-
self.string_pool.get_or_intern(key_str).to_string()
703-
} else {
704-
key.into()
705-
};
706-
707-
if self.string_pool.len() > 1000 {
708-
self.string_pool.clear_if_large();
709-
}
693+
let key = key.into();
710694

711695
let item = if let Some(default_ttl) = self.default_ttl {
712696
CacheItem::with_ttl(value.to_value(), default_ttl)
713697
} else {
714698
CacheItem::new(value.to_value())
715699
};
716700

717-
if let Some(existing_item) = self.map.get(&interned_key) {
701+
if let Some(existing_item) = self.map.get(&key) {
718702
if existing_item.value == item.value {
719703
return;
720704
}
721705
}
722706

723-
if self.map.len() >= self.capacity && !self.map.contains_key(&interned_key) {
707+
if self.map.len() >= self.capacity && !self.map.contains_key(&key) {
724708
if let Some((first_key, first_item)) = self.map.shift_remove_index(0) {
725709
self.send_remove(first_key, first_item.value);
726710
}
727711
}
728712

729-
self.map.insert(interned_key.clone(), item.clone());
713+
self.map.insert(key.clone(), item.clone());
730714

731-
self.send_insert(interned_key, item.value);
715+
self.send_insert(key, item.value);
732716
}
733717

734718
/// Inserts a key-value pair with a specific TTL.
@@ -806,19 +790,11 @@ impl Cache {
806790
/// ```
807791
#[inline]
808792
pub fn get(&mut self, key: &str) -> Option<&Value> {
809-
let pooled_key = if key.len() <= 50 {
810-
Some(self.string_pool.get_or_intern(key))
811-
} else {
812-
None
813-
};
814-
815-
let lookup_key = pooled_key.as_deref().unwrap_or(key);
816-
817-
if let Some((_, item)) = self.map.get_key_value(lookup_key) {
793+
if let Some((_, item)) = self.map.get_key_value(key) {
818794
item.prefetch_read();
819795
}
820796

821-
let is_expired = match self.map.get(lookup_key) {
797+
let is_expired = match self.map.get(key) {
822798
Some(item) => {
823799
if let Some(ttl) = item.ttl_millis {
824800
(current_time_millis() - item.created_at) > ttl
@@ -830,12 +806,12 @@ impl Cache {
830806
};
831807

832808
if is_expired {
833-
if let Some(expired_item) = self.map.swap_remove(lookup_key) {
834-
self.send_remove(lookup_key.to_string(), expired_item.value);
809+
if let Some(expired_item) = self.map.swap_remove(key) {
810+
self.send_remove(key.to_string(), expired_item.value);
835811
}
836812
None
837813
} else {
838-
self.map.get(lookup_key).map(|item| &item.value)
814+
self.map.get(key).map(|item| &item.value)
839815
}
840816
}
841817

@@ -874,16 +850,8 @@ impl Cache {
874850
}
875851

876852
pub fn remove(&mut self, key: &str) -> Result<(), Error> {
877-
let pooled_key = if key.len() <= 50 {
878-
Some(self.string_pool.get_or_intern(key))
879-
} else {
880-
None
881-
};
882-
883-
let lookup_key = pooled_key.as_deref().unwrap_or(key);
884-
885-
if let Some(item) = self.map.swap_remove(lookup_key) {
886-
self.send_remove(lookup_key.to_string(), item.value);
853+
if let Some(item) = self.map.swap_remove(key) {
854+
self.send_remove(key.to_string(), item.value);
887855
Ok(())
888856
} else {
889857
Err(Error::KeyNotFound)
@@ -892,7 +860,6 @@ impl Cache {
892860

893861
pub fn clear(&mut self) {
894862
self.map.clear();
895-
self.string_pool.clear();
896863
self.send_clear();
897864
}
898865

src/fast_filters.rs

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/filters.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Optimized filter operations - simple and fast!
2+
3+
use crate::filter::Filter;
4+
5+
/// Fast and safe prefix matching using Rust's optimized implementation
6+
#[inline(always)]
7+
pub fn fast_prefix_match(text: &str, prefix: &str) -> bool {
8+
text.starts_with(prefix)
9+
}
10+
11+
/// Fast and safe suffix matching using Rust's optimized implementation
12+
#[inline(always)]
13+
pub fn fast_suffix_match(text: &str, suffix: &str) -> bool {
14+
text.ends_with(suffix)
15+
}
16+
17+
/// Optimized filter application - same interface, better performance
18+
#[inline]
19+
pub fn apply_filter_fast(key: &str, filter: &Filter) -> bool {
20+
match filter {
21+
Filter::None => true,
22+
Filter::StartWith(prefix) => key.starts_with(prefix),
23+
Filter::EndWith(suffix) => key.ends_with(suffix),
24+
Filter::StartAndEndWith(prefix, suffix) => key.starts_with(prefix) && key.ends_with(suffix),
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
use crate::filter::Filter;
32+
33+
#[test]
34+
fn test_fast_prefix_match() {
35+
assert!(fast_prefix_match("hello_world", "hello"));
36+
assert!(fast_prefix_match("hello", "hello"));
37+
assert!(!fast_prefix_match("hello", "hello_world"));
38+
assert!(fast_prefix_match("test", ""));
39+
}
40+
41+
#[test]
42+
fn test_fast_suffix_match() {
43+
assert!(fast_suffix_match("hello_world", "world"));
44+
assert!(fast_suffix_match("world", "world"));
45+
assert!(!fast_suffix_match("world", "hello_world"));
46+
assert!(fast_suffix_match("test", ""));
47+
}
48+
49+
#[test]
50+
fn test_apply_filter_fast() {
51+
assert!(apply_filter_fast("test", &Filter::None));
52+
assert!(apply_filter_fast(
53+
"hello_world",
54+
&Filter::StartWith("hello".to_string())
55+
));
56+
assert!(apply_filter_fast(
57+
"hello_world",
58+
&Filter::EndWith("world".to_string())
59+
));
60+
assert!(apply_filter_fast(
61+
"hello_world",
62+
&Filter::StartAndEndWith("hello".to_string(), "world".to_string())
63+
));
64+
assert!(!apply_filter_fast(
65+
"hello_world",
66+
&Filter::StartWith("goodbye".to_string())
67+
));
68+
}
69+
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@
327327
mod cache;
328328
mod error;
329329
mod event;
330-
mod fast_filters;
331330
mod filter;
331+
pub mod filters;
332332
mod list_props;
333333
#[cfg(test)]
334334
#[cfg(feature = "persist")]
@@ -338,7 +338,6 @@ pub mod prelude;
338338
mod quickleaf;
339339
#[cfg(feature = "persist")]
340340
mod sqlite_store;
341-
mod string_pool;
342341
#[cfg(test)]
343342
mod tests;
344343
#[cfg(test)]

0 commit comments

Comments
 (0)