Skip to content

Commit 610bc00

Browse files
committed
No more max strings. Allocate string memory more on-demand. Pool is now entirely its own thing.
1 parent eebc185 commit 610bc00

5 files changed

Lines changed: 126 additions & 124 deletions

File tree

crates/engine_bibtex/src/exec.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
check_brace_level, decr_brace_level, enough_text_chars, name_scan_for_and,
1818
von_name_ends_and_last_name_starts_stuff, von_token_found, QUOTE_NEXT_FN,
1919
},
20-
ASCIICode, Bibtex, BibtexError, BufPointer, GlobalItems, HashPointer, PoolPointer, StrIlk,
20+
ASCIICode, Bibtex, BibtexError, BufPointer, GlobalItems, HashPointer, StrIlk,
2121
};
2222
use std::ops::{Deref, DerefMut, Index};
2323
use crate::pool::{Checkpoint, MAX_PRINT_LINE, MIN_PRINT_LINE};
@@ -290,7 +290,7 @@ fn pop_whole_stack(
290290
Ok(())
291291
}
292292

293-
pub fn skip_brace_level_greater_than_one(str: &[ASCIICode], brace_level: &mut i32) -> PoolPointer {
293+
pub fn skip_brace_level_greater_than_one(str: &[ASCIICode], brace_level: &mut i32) -> usize {
294294
let mut pos = 0;
295295
while *brace_level > 1 && pos < str.len() {
296296
if str[pos] == b'}' {
@@ -741,7 +741,7 @@ fn add_pool_buf_and_push(
741741
) -> Result<(), BibtexError> {
742742
buffers.set_offset(BufTy::Ex, 1, buffers.init(BufTy::Ex));
743743
let str = &buffers.buffer(BufTy::Ex)[0..buffers.init(BufTy::Ex)];
744-
let val = ExecVal::String(pool.add_string(ctx, str)?);
744+
let val = ExecVal::String(pool.add_string(str));
745745
ctx.push_stack(val);
746746
Ok(())
747747
}
@@ -918,18 +918,18 @@ fn interp_concat(
918918
// Both strings are 'scratch', they must be next to each-other due to external invariants,
919919
// se we just make one new string covering both
920920
let new_len = pool.get_str(s1).len() + pool.get_str(s2).len();
921-
let new = pool.write_str(ctx, |cursor| cursor.extend(new_len))?;
921+
let new = pool.write_str(|cursor| cursor.extend(new_len));
922922
ctx.push_stack(ExecVal::String(new));
923923
} else if ctx.checkpoint.is_before(s2) {
924924
if pool.get_str(s2).is_empty() {
925925
ctx.push_stack(pop1);
926926
} else {
927927
// s2 is scratch, we add s1 to its end and return the new scratch string
928928
let s2_len = pool.get_str(s2).len();
929-
let new = pool.write_str(ctx, |cursor| {
929+
let new = pool.write_str(|cursor| {
930930
cursor.extend(s2_len);
931931
cursor.append_str(s1);
932-
})?;
932+
});
933933
ctx.push_stack(ExecVal::String(new));
934934
}
935935
} else if ctx.checkpoint.is_before(s1) {
@@ -939,7 +939,7 @@ fn interp_concat(
939939
if str2.is_empty() {
940940
// s1 is scratch and s2 is empty - just save s1 and return it
941941
let s1_len = str1.len();
942-
let new = pool.write_str(ctx, |cursor| cursor.extend(s1_len))?;
942+
let new = pool.write_str(|cursor| cursor.extend(s1_len));
943943
ctx.push_stack(ExecVal::String(new));
944944
} else if str1.is_empty() {
945945
// s1 is empty - just return s2
@@ -950,12 +950,12 @@ fn interp_concat(
950950

951951
// s1 is scratch and s2 is not - we want to copy s1 forward by the length of s2,
952952
// then write s2 in where it was, returning the new scratch string
953-
let new = pool.write_str(ctx, |cursor| {
953+
let new = pool.write_str(|cursor| {
954954
cursor.extend(s1_len + s2_len);
955955
let raw = cursor.bytes();
956956
raw.copy_within(0..s1_len, s2_len);
957957
cursor.insert_str(s2, 0);
958-
})?;
958+
});
959959
let val = ExecVal::String(new);
960960
ctx.push_stack(val);
961961
}
@@ -969,10 +969,10 @@ fn interp_concat(
969969
ctx.push_stack(pop1);
970970
} else {
971971
// Neither is scratch or empty - make a new scratch string from the concat of both
972-
let new = pool.write_str(ctx, |cursor| {
972+
let new = pool.write_str(|cursor| {
973973
cursor.append_str(s2);
974974
cursor.append_str(s1);
975-
})?;
975+
});
976976
let val = ExecVal::String(new);
977977
ctx.push_stack(val);
978978
}
@@ -1091,7 +1091,7 @@ fn interp_add_period(
10911091
// If scratch, save
10921092
if ctx.checkpoint.is_before(s1) {
10931093
let s1_len = pool.get_str(s1).len();
1094-
let new = pool.write_str(ctx, |cursor| cursor.extend(s1_len))?;
1094+
let new = pool.write_str(|cursor| cursor.extend(s1_len));
10951095
ctx.push_stack(ExecVal::String(new));
10961096
} else {
10971097
ctx.push_stack(pop1);
@@ -1100,14 +1100,14 @@ fn interp_add_period(
11001100
_ => {
11011101
let is_bst_str = ctx.checkpoint.is_before(s1);
11021102
let s1_len = pool.get_str(s1).len();
1103-
let new = pool.write_str(ctx, |cursor| {
1103+
let new = pool.write_str(|cursor| {
11041104
if is_bst_str {
11051105
cursor.extend(s1_len);
11061106
} else {
11071107
cursor.append_str(s1);
11081108
}
11091109
cursor.append(b'.');
1110-
})?;
1110+
});
11111111
let val = ExecVal::String(new);
11121112
ctx.push_stack(val);
11131113
}
@@ -1277,7 +1277,7 @@ fn interp_change_case(
12771277
idx += 1;
12781278
}
12791279
check_brace_level(ctx, pool, cites, s2, brace_level)?;
1280-
let val = ExecVal::String(pool.add_string(ctx, &scratch)?);
1280+
let val = ExecVal::String(pool.add_string(&scratch));
12811281
ctx.push_stack(val);
12821282
}
12831283
(ExecVal::String(_), _) => {
@@ -1346,12 +1346,12 @@ fn interp_dup(
13461346
ctx.push_stack(pop1);
13471347
} else {
13481348
let str_len = pool.get_str(s1).len();
1349-
let _ = pool.write_str(ctx, |cursor| {
1349+
let _ = pool.write_str(|cursor| {
13501350
cursor.extend(str_len);
1351-
})?;
1352-
let new = pool.write_str(ctx, |cursor| {
1351+
});
1352+
let new = pool.write_str(|cursor| {
13531353
cursor.append_str(s1);
1354-
})?;
1354+
});
13551355
let val = ExecVal::String(new);
13561356
ctx.push_stack(val);
13571357
}
@@ -1693,7 +1693,7 @@ fn interp_int_to_chr(
16931693
bst_ex_warn_print(ctx, pool, cites)?;
16941694
ctx.push_stack(ExecVal::String(ctx.s_null));
16951695
} else {
1696-
let val = ExecVal::String(pool.add_string(ctx, &[i1 as u8])?);
1696+
let val = ExecVal::String(pool.add_string(&[i1 as u8]));
16971697
ctx.push_stack(val);
16981698
}
16991699
Ok(())
@@ -1716,7 +1716,7 @@ fn interp_int_to_str(
17161716
};
17171717

17181718
let scratch = i1.to_string();
1719-
let val = ExecVal::String(pool.add_string(ctx, scratch.as_bytes())?);
1719+
let val = ExecVal::String(pool.add_string(scratch.as_bytes()));
17201720
ctx.push_stack(val);
17211721
Ok(())
17221722
}
@@ -1790,7 +1790,7 @@ fn interp_preamble(
17901790
for s in bibs.preamble() {
17911791
out.extend(pool.get_str(*s));
17921792
}
1793-
let s = pool.add_string(ctx, &out)?;
1793+
let s = pool.add_string(&out);
17941794
ctx.push_stack(ExecVal::String(s));
17951795
Ok(())
17961796
}
@@ -1888,14 +1888,14 @@ fn interp_purify(
18881888
}
18891889

18901890
scratch.truncate(write_idx);
1891-
let out = pool.add_string(ctx, &scratch)?;
1891+
let out = pool.add_string(&scratch);
18921892
ctx.push_stack(ExecVal::String(out));
18931893

18941894
Ok(())
18951895
}
18961896

18971897
fn interp_quote(ctx: &mut ExecCtx<'_, '_, '_>, pool: &mut StringPool) -> Result<(), BibtexError> {
1898-
let s = pool.add_string(ctx, b"\"")?;
1898+
let s = pool.add_string(b"\"");
18991899
ctx.push_stack(ExecVal::String(s));
19001900
Ok(())
19011901
}
@@ -1968,24 +1968,24 @@ fn interp_substr(
19681968
if len >= str.len() && (start == 1 || start == -1) {
19691969
if ctx.checkpoint.is_before(s3) {
19701970
let str_len = pool.get_str(s3).len();
1971-
let _ = pool.write_str(ctx, |cursor| cursor.extend(str_len))?;
1971+
let _ = pool.write_str(|cursor| cursor.extend(str_len));
19721972
}
19731973
ctx.push_stack(pop3);
19741974
return Ok(());
19751975
}
19761976

19771977
if start == 1 && ctx.checkpoint.is_before(s3) {
1978-
let new = pool.write_str(ctx, |cursor| {
1978+
let new = pool.write_str(|cursor| {
19791979
cursor.extend(len);
1980-
})?;
1980+
});
19811981
ctx.push_stack(ExecVal::String(new));
19821982
return Ok(());
19831983
}
19841984

19851985
// TODO: Remove this intermediate allocation, currently can't pass a `&str` from a StringPool
19861986
// to that StringPool.
19871987
let new_str = Vec::from(&str[SLRange { start, len }]);
1988-
let out = pool.add_string(ctx, &new_str)?;
1988+
let out = pool.add_string(&new_str);
19891989
ctx.push_stack(ExecVal::String(out));
19901990

19911991
Ok(())
@@ -2004,20 +2004,20 @@ fn interp_swap(
20042004
if ctx.checkpoint.is_before(s1) && ctx.checkpoint.is_before(s2) =>
20052005
{
20062006
let tmp = Vec::from(pool.get_str(s2));
2007-
let new = pool.write_str(ctx, |cursor| {
2007+
let new = pool.write_str(|cursor| {
20082008
cursor.append_str(s1);
2009-
})?;
2009+
});
20102010
let val = ExecVal::String(new);
20112011
ctx.push_stack(val);
2012-
let val = ExecVal::String(pool.add_string(ctx, &tmp)?);
2012+
let val = ExecVal::String(pool.add_string(&tmp));
20132013
ctx.push_stack(val);
20142014
return Ok(());
20152015
}
20162016
(ExecVal::String(s), _) | (_, ExecVal::String(s)) if ctx.checkpoint.is_before(s) => {
20172017
let str_len = pool.get_str(s).len();
2018-
let _ = pool.write_str(ctx, |cursor| {
2018+
let _ = pool.write_str(|cursor| {
20192019
cursor.extend(str_len);
2020-
})?;
2020+
});
20212021
}
20222022
(_, _) => (),
20232023
}
@@ -2133,7 +2133,7 @@ fn interp_text_prefix(
21332133
}
21342134

21352135
let is_before = ctx.checkpoint.is_before(s2);
2136-
let new = pool.write_str(ctx, |cursor| {
2136+
let new = pool.write_str(|cursor| {
21372137
if is_before {
21382138
cursor.extend(idx)
21392139
} else {
@@ -2142,7 +2142,7 @@ fn interp_text_prefix(
21422142
for _ in 0..brace_level {
21432143
cursor.append(b'}');
21442144
}
2145-
})?;
2145+
});
21462146

21472147
let val = ExecVal::String(new);
21482148
ctx.push_stack(val);
@@ -2541,7 +2541,7 @@ pub(crate) fn execute_fn(
25412541
} else {
25422542
let str_ent_ptr = globals.cites.ptr() * globals.entries.num_ent_strs() + *entry;
25432543
let str = globals.entries.strs(str_ent_ptr);
2544-
let val = ExecVal::String(globals.pool.add_string(ctx, str)?);
2544+
let val = ExecVal::String(globals.pool.add_string(str));
25452545
ctx.push_stack(val);
25462546
Ok(())
25472547
}
@@ -2556,7 +2556,7 @@ pub(crate) fn execute_fn(
25562556
ctx.push_stack(ExecVal::String(str_ptr));
25572557
} else {
25582558
let str = globals.globals.str(*glb_ptr);
2559-
let val = ExecVal::String(globals.pool.add_string(ctx, str)?);
2559+
let val = ExecVal::String(globals.pool.add_string(str));
25602560
ctx.push_stack(val);
25612561
}
25622562
Ok(())

crates/engine_bibtex/src/hash.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{auxi::AuxCommand, bibs::BibCommand, bst::BstCommand, exec::ControlSeq, hash, pool, pool::StrNumber, ASCIICode, Bibtex, BibtexError, CiteNumber, FnDefLoc, HashPointer, LookupRes, StrIlk};
1+
use crate::{auxi::AuxCommand, bibs::BibCommand, bst::BstCommand, exec::ControlSeq, pool, pool::StrNumber, ASCIICode, Bibtex, BibtexError, CiteNumber, FnDefLoc, HashPointer, LookupRes, StrIlk};
22
use crate::log::print_overflow;
33
use crate::pool::StringPool;
44

@@ -231,7 +231,7 @@ impl HashData {
231231

232232
pub fn lookup_str(&self, pool: &StringPool, str: &[ASCIICode], ilk: StrIlk) -> LookupRes {
233233
let h = self.hash_str(str);
234-
let mut p = h as HashPointer + hash::HASH_BASE as HashPointer;
234+
let mut p = h as HashPointer + HASH_BASE as HashPointer;
235235

236236
let exists = loop {
237237
let existing = self.text(p);
@@ -263,14 +263,14 @@ impl HashData {
263263
let h = self.hash_str(str);
264264
let mut str_num = StrNumber::default();
265265
// Get position by adding HASH_BASE
266-
let mut p = (h + hash::HASH_BASE) as HashPointer;
266+
let mut p = (h + HASH_BASE) as HashPointer;
267267

268268
// Look for an existing match, or the last slot
269269
let existing = loop {
270270
// Get the current text at the position
271271
let existing = self.text(p);
272272
// If the text exists and is the same as the text we're adding
273-
if pool.try_get_str(existing) == Ok(str) {
273+
if pool.try_get_str(existing) == Some(str) {
274274
// If an existing hash entry exists for this type, return it
275275
if self.node(p).kind() == ilk.kind() {
276276
return Ok(LookupRes {
@@ -296,7 +296,7 @@ impl HashData {
296296
loop {
297297
if self.len() == HASH_BASE {
298298
print_overflow(ctx);
299-
ctx.write_logs(&format!("hash size {}\n", hash::HASH_SIZE));
299+
ctx.write_logs(&format!("hash size {}\n", HASH_SIZE));
300300
return Err(BibtexError::Fatal);
301301
}
302302
self.set_len(self.len() - 1);
@@ -316,8 +316,7 @@ impl HashData {
316316
self.set_text(p, str_num);
317317
// The string isn't in the string pool - add it
318318
} else {
319-
let num = pool.add_string(ctx, str)?;
320-
self.set_text(p, num);
319+
self.set_text(p, pool.add_string(str));
321320
}
322321

323322
// Set the type of this slot
@@ -334,7 +333,7 @@ impl HashData {
334333
mod tests {
335334
use super::*;
336335
use crate::{Bibtex, BibtexConfig};
337-
use crate::pool::{LookupErr, StringPool};
336+
use crate::pool::StringPool;
338337
use crate::test_utils::with_cbs;
339338

340339
#[test]
@@ -349,7 +348,7 @@ mod tests {
349348
assert!(!res.exists);
350349
assert_eq!(
351350
pool.try_get_str(hash.text(res.loc)),
352-
Ok(b"a cool string" as &[_])
351+
Some(b"a cool string" as &[_])
353352
);
354353

355354
let res2 = hash
@@ -358,21 +357,21 @@ mod tests {
358357
assert!(res2.exists);
359358
assert_eq!(
360359
pool.try_get_str(hash.text(res2.loc)),
361-
Ok(b"a cool string" as &[_])
360+
Some(b"a cool string" as &[_])
362361
);
363362

364363
let res3 = hash.lookup_str(&pool, b"a cool string", StrIlk::Text);
365364
assert!(res3.exists);
366365
assert_eq!(
367366
pool.try_get_str(hash.text(res3.loc)),
368-
Ok(b"a cool string" as &[_])
367+
Some(b"a cool string" as &[_])
369368
);
370369

371370
let res4 = hash.lookup_str(&pool, b"a bad string", StrIlk::Text);
372371
assert!(!res4.exists);
373372
assert_eq!(
374373
pool.try_get_str(hash.text(res4.loc)),
375-
Err(LookupErr::DoesntExist)
374+
None,
376375
);
377376
})
378377
}

crates/engine_bibtex/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ pub(crate) enum StrIlk {
371371
type CiteNumber = usize;
372372
type ASCIICode = u8;
373373
type BufPointer = usize;
374-
type PoolPointer = usize;
375374
type HashPointer = usize;
376375
type BibNumber = usize;
377376
type FieldLoc = usize;

0 commit comments

Comments
 (0)