Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PERF]: remove mutex around tokenizer #2735

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 6 additions & 31 deletions rust/index/src/fulltext/tokenizer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use parking_lot::Mutex;
use std::sync::Arc;
use tantivy::tokenizer::{NgramTokenizer, Token, TokenStream, Tokenizer};

pub trait ChromaTokenStream: Send {
fn process(&mut self, sink: &mut dyn FnMut(&Token));
fn get_tokens(&self) -> &Vec<Token>;
}

Expand All @@ -18,36 +15,28 @@ impl TantivyChromaTokenStream {
}

impl ChromaTokenStream for TantivyChromaTokenStream {
fn process(&mut self, sink: &mut dyn FnMut(&Token)) {
for token in &self.tokens {
sink(token);
}
}

fn get_tokens(&self) -> &Vec<Token> {
&self.tokens
}
}

pub trait ChromaTokenizer: Send {
pub trait ChromaTokenizer: Send + Sync {
fn encode(&self, text: &str) -> Box<dyn ChromaTokenStream>;
}

pub struct TantivyChromaTokenizer {
tokenizer: Arc<Mutex<Box<NgramTokenizer>>>,
tokenizer: NgramTokenizer,
}

impl TantivyChromaTokenizer {
pub fn new(tokenizer: Box<NgramTokenizer>) -> Self {
TantivyChromaTokenizer {
tokenizer: Arc::new(Mutex::new(tokenizer)),
}
pub fn new(tokenizer: NgramTokenizer) -> Self {
TantivyChromaTokenizer { tokenizer }
}
}

impl ChromaTokenizer for TantivyChromaTokenizer {
fn encode(&self, text: &str) -> Box<dyn ChromaTokenStream> {
let mut tokenizer = self.tokenizer.lock();
let mut tokenizer = self.tokenizer.clone();
let mut token_stream = tokenizer.token_stream(text);
let mut tokens = Vec::new();
token_stream.process(&mut |token| {
Expand All @@ -60,23 +49,9 @@ impl ChromaTokenizer for TantivyChromaTokenizer {
mod test {
use super::*;

#[test]
fn test_chroma_tokenizer() {
HammadB marked this conversation as resolved.
Show resolved Hide resolved
let tokenizer: Box<NgramTokenizer> = Box::new(NgramTokenizer::new(1, 1, false).unwrap());
let chroma_tokenizer = TantivyChromaTokenizer::new(tokenizer);
let mut token_stream = chroma_tokenizer.encode("hello world");
let mut tokens = Vec::new();
token_stream.process(&mut |token| {
tokens.push(token.clone());
});
assert_eq!(tokens.len(), 11);
assert_eq!(tokens[0].text, "h");
assert_eq!(tokens[1].text, "e");
}

#[test]
fn test_get_tokens() {
let tokenizer: Box<NgramTokenizer> = Box::new(NgramTokenizer::new(1, 1, false).unwrap());
let tokenizer = NgramTokenizer::new(1, 1, false).unwrap();
let chroma_tokenizer = TantivyChromaTokenizer::new(tokenizer);
let token_stream = chroma_tokenizer.encode("hello world");
let tokens = token_stream.get_tokens();
Expand Down
Loading
Loading