Skip to content

Commit

Permalink
syslinreader.rs pre-create FixedOffset strings
Browse files Browse the repository at this point in the history
Pre-create strings of the FixedOffset used by SyslineReader.
Avoid creating a new String for every sysline processed.

Issue #84
  • Loading branch information
jtmoon79 committed May 2, 2023
1 parent cf9153b commit 3b95001
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
33 changes: 17 additions & 16 deletions src/data/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4963,6 +4963,7 @@ pub(crate) fn captures_to_buffer_bytes(
captures: &regex::bytes::Captures,
year_opt: &Option<Year>,
tz_offset: &FixedOffset,
tz_offset_string: &String,
dtfs: &DTFSSet,
) -> usize {
defn!("(…, …, year_opt {:?}, tz_offset {:?}, …)", year_opt, tz_offset);
Expand Down Expand Up @@ -5177,9 +5178,7 @@ pub(crate) fn captures_to_buffer_bytes(
defo!("process <tz>…");
match dtfs.tz {
DTFS_Tz::_fill => {
// TODO: cost-savings: pass pre-created TZ `&str`
let tzs: String = tz_offset.to_string();
copy_slice_to_buffer!(tzs.as_bytes(), buffer, at);
copy_slice_to_buffer!(tz_offset_string.as_bytes(), buffer, at);
}
DTFS_Tz::z | DTFS_Tz::zc | DTFS_Tz::zp => {
// for data passed to chrono `DateTime::parse_from_str`,
Expand Down Expand Up @@ -5235,24 +5234,18 @@ pub(crate) fn captures_to_buffer_bytes(
true => {
// given an ambiguous timezone name, fallback to
// passed TZ offset
// TODO: cost-savings: pre-create the `tz_offset` entries as bytes
let tzs: String = tz_offset.to_string();
copy_slice_to_buffer!(tzs.as_bytes(), buffer, at);
copy_slice_to_buffer!(tz_offset_string.as_bytes(), buffer, at);
}
false => {
// given an unambiguous timezone name, use
// associated offset
// TODO: cost-savings: pre-create the `tz_offset` entries as bytes
let tzs: String = tz_offset_val.to_string();
copy_slice_to_buffer!(tzs.as_bytes(), buffer, at);
// given an unambiguous timezone name, use associated offset
copy_slice_to_buffer!(tz_offset_val.as_bytes(), buffer, at);
}
}
}
None => {
// cannot find entry in MAP_TZZ_TO_TZz, use passed TZ offset
// TODO: cost-savings: pre-create the `tz_offset` entries as bytes
let tzs: String = tz_offset.to_string();
copy_slice_to_buffer!(tzs.as_bytes(), buffer, at);
debug_panic!("captured named timezone {:?} not found in MAP_TZZ_TO_TZz", tzZ);
copy_slice_to_buffer!(tz_offset_string.as_bytes(), buffer, at);
}
}
}
Expand All @@ -5275,8 +5268,9 @@ pub fn bytes_to_regex_to_datetime(
index: &DateTimeParseInstrsIndex,
year_opt: &Option<Year>,
tz_offset: &FixedOffset,
tz_offset_string: &String,
) -> Option<CapturedDtData> {
defn!("(…, {:?}, {:?}, {:?})", index, year_opt, tz_offset);
defn!("(…, {:?}, {:?}, {:?}, {:?})", index, year_opt, tz_offset, tz_offset_string);

let regex_: &Regex = match DATETIME_PARSE_DATAS_REGEX_VEC.get(*index) {
Some(val) => val,
Expand Down Expand Up @@ -5351,7 +5345,14 @@ pub fn bytes_to_regex_to_datetime(
// TODO: [2022/06/26] cost-savings: avoid a `String` alloc by passing precreated buffer
const BUFLEN: usize = 35;
let mut buffer: [u8; BUFLEN] = [0; BUFLEN];
let copiedn = captures_to_buffer_bytes(&mut buffer, &captures, year_opt, tz_offset, &dtpd.dtfs);
let copiedn = captures_to_buffer_bytes(
&mut buffer,
&captures,
year_opt,
tz_offset,
tz_offset_string,
&dtpd.dtfs
);

// use the `dt_format` to parse the buffer of regex matches
let buffer_s: &str = u8_to_str(&buffer[0..copiedn]).unwrap();
Expand Down
7 changes: 6 additions & 1 deletion src/readers/syslinereader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ pub struct SyslineReader {
///
/// [`FixedOffset`]: https://docs.rs/chrono/0.4.22/chrono/offset/struct.FixedOffset.html
tz_offset: FixedOffset,
/// precompute `tz_offset` as a `String`
tz_offset_string: String,
/// Enable or disable the internal LRU cache for `find_sysline()`.
find_sysline_lru_cache_enabled: bool,
/// Internal [LRU cache] for `find_sysline()`.
Expand Down Expand Up @@ -480,6 +482,7 @@ impl SyslineReader {
dt_patterns_counts,
dt_patterns_indexes,
tz_offset,
tz_offset_string: tz_offset.to_string(),
find_sysline_lru_cache_enabled: SyslineReader::CACHE_ENABLE_DEFAULT,
find_sysline_lru_cache: SyslinesLRUCache::new(
std::num::NonZeroUsize::new(SyslineReader::FIND_SYSLINE_LRU_CACHE_SZ).unwrap(),
Expand Down Expand Up @@ -1144,6 +1147,7 @@ impl SyslineReader {
charsz: CharSz,
year_opt: &Option<Year>,
tz_offset: &FixedOffset,
tz_offset_string: &String,
get_boxptrs_singleptr: &mut Count,
get_boxptrs_doubleptr: &mut Count,
get_boxptrs_multiptr: &mut Count,
Expand Down Expand Up @@ -1304,7 +1308,7 @@ impl SyslineReader {
let dt_beg: LineIndex;
let dt_end: LineIndex;
(dt_beg, dt_end, dt) =
match bytes_to_regex_to_datetime(slice_, index, year_opt, tz_offset) {
match bytes_to_regex_to_datetime(slice_, index, year_opt, tz_offset, tz_offset_string) {
None => continue,
Some(val) => val,
};
Expand Down Expand Up @@ -1582,6 +1586,7 @@ impl SyslineReader {
charsz,
year_opt,
&self.tz_offset,
&self.tz_offset_string,
&mut self.get_boxptrs_singleptr,
&mut self.get_boxptrs_doubleptr,
&mut self.get_boxptrs_multiptr,
Expand Down
1 change: 1 addition & 0 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ lazy_static! {

// FixedOffset Local
pub static ref FO_L: FixedOffset = *LOCAL_NOW.offset();
pub static ref FO_L_STR: String = String::from(&*FO_L.to_string());
// FixedOffset Zulu / UTC
pub static ref FO_Z: FixedOffset = FixedOffset::east_opt(0).unwrap();
pub static ref FO_0: FixedOffset = FixedOffset::east_opt(0).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/datetime_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ fn test_DATETIME_PARSE_DATAS_test_cases(index: usize) {
year_opt = Some(YEAR_FALLBACKDUMMY_VAL);
}
let s = buffer_to_String_noraw(data);
match bytes_to_regex_to_datetime(data, &index, &year_opt, &FO_L) {
match bytes_to_regex_to_datetime(data, &index, &year_opt, &FO_L, &FO_L_STR) {
Some(capdata) => {
eprintln!(
"Passed dtpd declared at line {} result {:?}, test data {:?}",
Expand Down
28 changes: 16 additions & 12 deletions src/tests/syslinereader_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5227,7 +5227,7 @@ fn test_ezcheck_slice(
0,
4,
0,
476,
484,
0,
0,
0,
Expand All @@ -5236,9 +5236,10 @@ fn test_ezcheck_slice(
24,
0,
7,
452,
460,
0,
7
7;
"NTF_GZ_8BYTE_FPATH"
)]
#[test_case(
&*NTF_SYSLINE_1_PATH,
Expand All @@ -5265,7 +5266,7 @@ fn test_ezcheck_slice(
1,
0,
4,
70,
71,
0,
0,
0,
Expand All @@ -5274,8 +5275,9 @@ fn test_ezcheck_slice(
4,
0,
0,
70,
0
71,
0;
"NTF_SYSLINE_1_PATH (cache)"
)]
#[test_case(
&*NTF_SYSLINE_1_PATH,
Expand All @@ -5302,7 +5304,7 @@ fn test_ezcheck_slice(
0,
0,
4,
70,
71,
0,
0,
0,
Expand All @@ -5311,8 +5313,9 @@ fn test_ezcheck_slice(
4,
0,
0,
70,
0
71,
0;
"NTF_SYSLINE_1_PATH (no cache)"
)]
#[test_case(
&*NTF_SYSLINE_2_PATH,
Expand Down Expand Up @@ -5340,16 +5343,17 @@ fn test_ezcheck_slice(
0,
0,
0,
148,
150,
0,
0,
0,
0,
8,
0,
0,
140,
0
142,
0;
"NTF_SYSLINE_2_PATH"
)]
fn test_syslinereadersummary(
path: &FPath,
Expand Down

0 comments on commit 3b95001

Please sign in to comment.