Remove key_size() method from Column trait (#34021)

This helper simply called std::mem::size_of<Self::Index>(). However, all
of the underlying functions that create keys manually copy fields into a
byte array. The fields are copied in end-to-end whereas size_of() might
include alignment bytes.

For example, a (u64, u32) only has 12 bytes of "data", but it would
have size 16 due to the 4 alignment padding bytes that would be
added to get the u32 (size 4) aligned with the u64 (size 8).
This commit is contained in:
steviez 2023-11-19 23:05:32 -06:00 committed by GitHub
parent f9b96fa112
commit 9a7b681f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -743,17 +743,23 @@ fn analyze_column<
db: &Database, db: &Database,
name: &str, name: &str,
) { ) {
let mut key_len: u64 = 0;
let mut key_tot: u64 = 0; let mut key_tot: u64 = 0;
let mut val_hist = histogram::Histogram::new(); let mut val_hist = histogram::Histogram::new();
let mut val_tot: u64 = 0; let mut val_tot: u64 = 0;
let mut row_hist = histogram::Histogram::new(); let mut row_hist = histogram::Histogram::new();
let a = C::key_size() as u64; for (key, val) in db.iter::<C>(blockstore_db::IteratorMode::Start).unwrap() {
for (_x, y) in db.iter::<C>(blockstore_db::IteratorMode::Start).unwrap() { // Key length is fixed, only need to calculate it once
let b = y.len() as u64; if key_len == 0 {
key_tot += a; key_len = C::key(key).len() as u64;
val_hist.increment(b).unwrap(); }
val_tot += b; let val_len = val.len() as u64;
row_hist.increment(a + b).unwrap();
key_tot += key_len;
val_hist.increment(val_len).unwrap();
val_tot += val_len;
row_hist.increment(key_len + val_len).unwrap();
} }
let json_result = if val_hist.entries() > 0 { let json_result = if val_hist.entries() > 0 {
@ -761,7 +767,7 @@ fn analyze_column<
"column":name, "column":name,
"entries":val_hist.entries(), "entries":val_hist.entries(),
"key_stats":{ "key_stats":{
"max":a, "max":key_len,
"total_bytes":key_tot, "total_bytes":key_tot,
}, },
"val_stats":{ "val_stats":{
@ -790,7 +796,7 @@ fn analyze_column<
"column":name, "column":name,
"entries":val_hist.entries(), "entries":val_hist.entries(),
"key_stats":{ "key_stats":{
"max":a, "max":key_len,
"total_bytes":0, "total_bytes":0,
}, },
"val_stats":{ "val_stats":{

View File

@ -712,10 +712,6 @@ impl Rocks {
pub trait Column { pub trait Column {
type Index; type Index;
fn key_size() -> usize {
std::mem::size_of::<Self::Index>()
}
fn key(index: Self::Index) -> Vec<u8>; fn key(index: Self::Index) -> Vec<u8>;
fn index(key: &[u8]) -> Self::Index; fn index(key: &[u8]) -> Self::Index;
// This trait method is primarily used by `Database::delete_range_cf()`, and is therefore only // This trait method is primarily used by `Database::delete_range_cf()`, and is therefore only