Skip to content

Commit

Permalink
Avoid cloning hashmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
CurtHagenlocher authored and rtyler committed Aug 28, 2023
1 parent d4d4665 commit 89cdfa7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions rust/src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use serde_json::{Map, Value};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::mem::take;
use std::str::FromStr;

use crate::delta_config::IsolationLevel;
Expand Down Expand Up @@ -112,7 +113,7 @@ fn decode_path(raw_path: &str) -> Result<String, ProtocolError> {
}

/// Struct used to represent minValues and maxValues in add action statistics.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum ColumnValueStat {
/// Composite HashMap representation of statistics.
Expand Down Expand Up @@ -140,7 +141,7 @@ impl ColumnValueStat {
}

/// Struct used to represent nullCount in add action statistics.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum ColumnCountStat {
/// Composite HashMap representation of statistics.
Expand Down Expand Up @@ -202,19 +203,22 @@ struct PartialStats {

impl PartialStats {
/// Fills in missing HashMaps
pub fn as_stats(&self) -> Stats {
pub fn as_stats(&mut self) -> Stats {
let min_values = take(&mut self.min_values);
let max_values = take(&mut self.max_values);
let null_count = take(&mut self.null_count);
Stats {
num_records: self.num_records,
min_values: match &self.min_values {
Some(minv) => minv.clone(),
min_values: match min_values {
Some(minv) => minv,
None => HashMap::default()
},
max_values: match &self.max_values {
Some(maxv) => maxv.clone(),
max_values: match max_values {
Some(maxv) => maxv,
None => HashMap::default()
},
null_count: match &self.null_count {
Some(nc) => nc.clone(),
null_count: match null_count {
Some(nc) => nc,
None => HashMap::default()
},
}
Expand Down Expand Up @@ -462,7 +466,7 @@ impl Add {
.map_or(Ok(None), |s| serde_json::from_str(s));

match ps {
Ok(Some(partial)) => Ok(Some(partial.as_stats())),
Ok(Some(mut partial)) => Ok(Some(partial.as_stats())),
Ok(None) => Ok(None),
Err(e) => Err(e)
}
Expand Down

0 comments on commit 89cdfa7

Please sign in to comment.