Skip to content

Commit 7386c29

Browse files
authored
remove unnecessary copies to reduce memory usage (#11)
tiktoken-node goes from 194mb -> 140mb as shown by `heap -s <nodejs_pid>` on macOS
1 parent d08521b commit 7386c29

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/corebpe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ fn hash_current_thread() -> usize {
162162
u64::from(x) as usize
163163
}
164164

165-
const MAX_NUM_THREADS: usize = 128;
165+
const MAX_NUM_THREADS: usize = 8;
166166

167167
#[derive(Debug)]
168168
pub struct CoreBPE {
169-
encoder: HashMap<Vec<u8>, usize>,
169+
encoder: Arc<HashMap<Vec<u8>, usize>>,
170170
special_tokens_encoder: HashMap<String, usize>,
171171
decoder: HashMap<usize, Vec<u8>>,
172172
special_tokens_decoder: HashMap<usize, Vec<u8>>,
@@ -429,7 +429,7 @@ impl CoreBPE {
429429

430430
impl CoreBPE {
431431
pub fn new(
432-
encoder: HashMap<Vec<u8>, usize>,
432+
encoder: Arc<HashMap<Vec<u8>, usize>>,
433433
special_tokens_encoder: HashMap<String, usize>,
434434
pattern: &str,
435435
) -> Result<Self, fancy_regex::Error> {

src/encoding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Encoding {
1414
/// The regular expression pattern used to split text into pieces.
1515
pat_str: String,
1616
/// The map from mergeable byte sequences to their ranks.
17-
mergeable_ranks: HashMap<Vec<u8>, usize>,
17+
mergeable_ranks: Arc<HashMap<Vec<u8>, usize>>,
1818
/// The maximum length of the keys in `mergeable_ranks`.
1919
mergeable_ranks_max_key_len: usize,
2020
/// All prefixes of the mergeable ranks. May or may not be tokens themselves!
@@ -64,7 +64,7 @@ impl Encoding {
6464
pub fn new(
6565
name: &str,
6666
pat_str: &str,
67-
mergeable_ranks: HashMap<Vec<u8>, usize>,
67+
mergeable_ranks: Arc<HashMap<Vec<u8>, usize>>,
6868
special_tokens: HashMap<String, usize>,
6969
explicit_n_vocab: Option<usize>,
7070
) -> Result<Self, EncodingError> {

src/load.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use sha2::Sha256;
55
// call its methods without adding to the namespace.
66
use base64::engine::general_purpose::STANDARD as BASE64;
77
use base64::engine::Engine as _;
8+
use std::sync::Arc;
89

910
// define the error
1011
#[derive(Debug, Clone)]
@@ -16,7 +17,7 @@ pub enum Error {
1617
pub fn load_tiktoken_bpe(
1718
tiktoken_bpe_contents: &[u8],
1819
shasum: &str,
19-
) -> Result<HashMap<Vec<u8>, usize>, Error> {
20+
) -> Result<Arc<HashMap<Vec<u8>, usize>>, Error> {
2021
// check the shasum
2122
let mut hasher = Sha256::new();
2223
hasher.update(tiktoken_bpe_contents);
@@ -42,5 +43,5 @@ pub fn load_tiktoken_bpe(
4243
map.insert(token, rank);
4344
}
4445
map.shrink_to_fit();
45-
Ok(map)
46+
Ok(Arc::new(map))
4647
}

0 commit comments

Comments
 (0)