db::read -- io::Cursor instead of take
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dmitry Belyaev 2022-11-09 21:31:38 +03:00
parent 6a3b3647b3
commit be6b17a8e2
2 changed files with 16 additions and 19 deletions

View File

@ -3,21 +3,21 @@
95 MB json.zip 95 MB json.zip
--- ---
hyperfine -n print -n print2 -n zip-print -w 100 -m 100 ".\target\release\chgk_ledb.exe print 444" ".\target\release\chgk_ledb.exe print2 444" ".\target\release\chgk_ledb.exe zip-print 4 84" hyperfine -n print -n print2 -n zip-print -w 400 -m 400 ".\target\release\chgk_ledb.exe print 444" ".\target\release\chgk_ledb.exe print2 444" ".\target\release\chgk_ledb.exe zip-print 4 84"
Benchmark 1: print Benchmark 1: print
Time (mean ± σ): 20.0 ms ± 1.7 ms [User: 5.8 ms, System: 12.3 ms] Time (mean ± σ): 19.0 ms ± 1.5 ms [User: 5.6 ms, System: 13.1 ms]
Range (min … max): 18.0 ms … 30.9 ms 100 runs Range (min … max): 16.8 ms … 24.5 ms 400 runs
Benchmark 2: print2 Benchmark 2: print2
Time (mean ± σ): 19.1 ms ± 1.2 ms [User: 5.7 ms, System: 12.3 ms] Time (mean ± σ): 18.6 ms ± 1.6 ms [User: 5.5 ms, System: 12.6 ms]
Range (min … max): 17.2 ms … 22.1 ms 100 runs Range (min … max): 16.1 ms … 29.5 ms 400 runs
Benchmark 3: zip-print Benchmark 3: zip-print
Time (mean ± σ): 41.1 ms ± 1.4 ms [User: 16.1 ms, System: 22.2 ms] Time (mean ± σ): 40.8 ms ± 3.3 ms [User: 15.4 ms, System: 21.6 ms]
Range (min … max): 38.9 ms … 45.4 ms 100 runs Range (min … max): 36.5 ms … 67.5 ms 400 runs
Summary Summary
'print2' ran 'print2' ran
1.05 ± 0.11 times faster than 'print' 1.02 ± 0.12 times faster than 'print'
2.16 ± 0.15 times faster than 'zip-print' 2.20 ± 0.26 times faster than 'zip-print'

View File

@ -191,28 +191,25 @@ where
return Err("index out of range".into()); return Err("index out of range".into());
} }
let next_pos: usize = (index + 1) * LEN_SIZE;
let next_end: usize = next_pos + LEN_SIZE;
// read item data pos // read item data pos
let data_pos = if 0 == index { let data_pos = if 0 == index {
self.first_pos self.first_pos
} else { } else {
let tab_pos: usize = index * LEN_SIZE; let tab_pos: usize = index * LEN_SIZE;
let pos_curr_data: [u8; LEN_SIZE] = self.mmap[tab_pos..(tab_pos + LEN_SIZE)] let pos_curr_data: [u8; LEN_SIZE] =
.try_into() self.mmap[tab_pos..next_pos].try_into().str_err()?;
.str_err()?;
LSize::from_le_bytes(pos_curr_data) LSize::from_le_bytes(pos_curr_data)
} as usize; } as usize;
// read next item pos // read next item pos
let next_pos: usize = (index + 1) * LEN_SIZE; let pos_next_data: [u8; LEN_SIZE] = self.mmap[next_pos..next_end].try_into().str_err()?;
let pos_next_data: [u8; LEN_SIZE] = self.mmap[next_pos..(next_pos + LEN_SIZE)]
.try_into()
.str_err()?;
let data_pos_next = LSize::from_le_bytes(pos_next_data) as usize; let data_pos_next = LSize::from_le_bytes(pos_next_data) as usize;
// calc item data length
let data_len = data_pos_next - data_pos;
// read & unpack item data // read & unpack item data
let reader = self.mmap[data_pos..data_pos_next].take(data_len as u64); let reader = io::Cursor::new(self.mmap[data_pos..data_pos_next].as_ref());
let data = zstd::decode_all(reader).str_err()?; let data = zstd::decode_all(reader).str_err()?;
// decode item // decode item