Skip to content

Commit 8be22f7

Browse files
author
Alexander Beedie
committed
Avoid format-related heap allocations
1 parent e81eb14 commit 8be22f7

1 file changed

Lines changed: 41 additions & 61 deletions

File tree

src/ast/mod.rs

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,11 +3306,11 @@ impl Display for Set {
33063306
role_name,
33073307
} => {
33083308
let role_name = role_name.clone().unwrap_or_else(|| Ident::new("NONE"));
3309-
write!(
3310-
f,
3311-
"SET {modifier}ROLE {role_name}",
3312-
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
3313-
)
3309+
f.write_str("SET ")?;
3310+
if let Some(m) = context_modifier {
3311+
write!(f, "{m}")?;
3312+
}
3313+
write!(f, "ROLE {role_name}")
33143314
}
33153315
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
33163316
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
@@ -3363,10 +3363,13 @@ impl Display for Set {
33633363
variable,
33643364
values,
33653365
} => {
3366+
f.write_str("SET ")?;
3367+
if let Some(s) = scope {
3368+
write!(f, "{s}")?;
3369+
}
33663370
write!(
33673371
f,
3368-
"SET {}{}{} = {}",
3369-
scope.map(|s| format!("{s}")).unwrap_or_default(),
3372+
"{}{} = {}",
33703373
if *hivevar { "HIVEVAR:" } else { "" },
33713374
variable,
33723375
display_comma_separated(values)
@@ -5991,21 +5994,16 @@ impl fmt::Display for Statement {
59915994
sequence_options,
59925995
owned_by,
59935996
} => {
5994-
let as_type: String = if let Some(dt) = data_type.as_ref() {
5995-
//Cannot use format!(" AS {}", dt), due to format! is not available in --target thumbv6m-none-eabi
5996-
// " AS ".to_owned() + &dt.to_string()
5997-
[" AS ", &dt.to_string()].concat()
5998-
} else {
5999-
"".to_string()
6000-
};
60015997
write!(
60025998
f,
6003-
"CREATE {temporary}SEQUENCE {if_not_exists}{name}{as_type}",
5999+
"CREATE {temporary}SEQUENCE {if_not_exists}{name}",
60046000
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
60056001
temporary = if *temporary { "TEMPORARY " } else { "" },
60066002
name = name,
6007-
as_type = as_type
60086003
)?;
6004+
if let Some(dt) = data_type.as_ref() {
6005+
write!(f, " AS {dt}")?;
6006+
}
60096007
for sequence_option in sequence_options {
60106008
write!(f, "{sequence_option}")?;
60116009
}
@@ -6339,13 +6337,10 @@ pub struct SetAssignment {
63396337

63406338
impl fmt::Display for SetAssignment {
63416339
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6342-
write!(
6343-
f,
6344-
"{}{} = {}",
6345-
self.scope.map(|s| format!("{s}")).unwrap_or_default(),
6346-
self.name,
6347-
self.value
6348-
)
6340+
if let Some(s) = self.scope {
6341+
write!(f, "{s}")?;
6342+
}
6343+
write!(f, "{} = {}", self.name, self.value)
63496344
}
63506345
}
63516346

@@ -6663,35 +6658,31 @@ pub enum FetchDirection {
66636658
impl fmt::Display for FetchDirection {
66646659
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66656660
match self {
6666-
FetchDirection::Count { limit } => f.write_str(&limit.to_string())?,
6661+
FetchDirection::Count { limit } => write!(f, "{limit}")?,
66676662
FetchDirection::Next => f.write_str("NEXT")?,
66686663
FetchDirection::Prior => f.write_str("PRIOR")?,
66696664
FetchDirection::First => f.write_str("FIRST")?,
66706665
FetchDirection::Last => f.write_str("LAST")?,
66716666
FetchDirection::Absolute { limit } => {
6672-
f.write_str("ABSOLUTE ")?;
6673-
f.write_str(&limit.to_string())?;
6667+
write!(f, "ABSOLUTE {limit}")?;
66746668
}
66756669
FetchDirection::Relative { limit } => {
6676-
f.write_str("RELATIVE ")?;
6677-
f.write_str(&limit.to_string())?;
6670+
write!(f, "RELATIVE {limit}")?;
66786671
}
66796672
FetchDirection::All => f.write_str("ALL")?,
66806673
FetchDirection::Forward { limit } => {
66816674
f.write_str("FORWARD")?;
66826675

66836676
if let Some(l) = limit {
6684-
f.write_str(" ")?;
6685-
f.write_str(&l.to_string())?;
6677+
write!(f, " {l}")?;
66866678
}
66876679
}
66886680
FetchDirection::ForwardAll => f.write_str("FORWARD ALL")?,
66896681
FetchDirection::Backward { limit } => {
66906682
f.write_str("BACKWARD")?;
66916683

66926684
if let Some(l) = limit {
6693-
f.write_str(" ")?;
6694-
f.write_str(&l.to_string())?;
6685+
write!(f, " {l}")?;
66956686
}
66966687
}
66976688
FetchDirection::BackwardAll => f.write_str("BACKWARD ALL")?,
@@ -10498,35 +10489,24 @@ pub struct ShowStatementOptions {
1049810489

1049910490
impl Display for ShowStatementOptions {
1050010491
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10501-
let (like_in_infix, like_in_suffix) = match &self.filter_position {
10502-
Some(ShowStatementFilterPosition::Infix(filter)) => {
10503-
(format!(" {filter}"), "".to_string())
10504-
}
10505-
Some(ShowStatementFilterPosition::Suffix(filter)) => {
10506-
("".to_string(), format!(" {filter}"))
10507-
}
10508-
None => ("".to_string(), "".to_string()),
10509-
};
10510-
write!(
10511-
f,
10512-
"{like_in_infix}{show_in}{starts_with}{limit}{from}{like_in_suffix}",
10513-
show_in = match &self.show_in {
10514-
Some(i) => format!(" {i}"),
10515-
None => String::new(),
10516-
},
10517-
starts_with = match &self.starts_with {
10518-
Some(s) => format!(" STARTS WITH {s}"),
10519-
None => String::new(),
10520-
},
10521-
limit = match &self.limit {
10522-
Some(l) => format!(" LIMIT {l}"),
10523-
None => String::new(),
10524-
},
10525-
from = match &self.limit_from {
10526-
Some(f) => format!(" FROM {f}"),
10527-
None => String::new(),
10528-
}
10529-
)?;
10492+
if let Some(ShowStatementFilterPosition::Infix(filter)) = &self.filter_position {
10493+
write!(f, " {filter}")?;
10494+
}
10495+
if let Some(i) = &self.show_in {
10496+
write!(f, " {i}")?;
10497+
}
10498+
if let Some(s) = &self.starts_with {
10499+
write!(f, " STARTS WITH {s}")?;
10500+
}
10501+
if let Some(l) = &self.limit {
10502+
write!(f, " LIMIT {l}")?;
10503+
}
10504+
if let Some(lf) = &self.limit_from {
10505+
write!(f, " FROM {lf}")?;
10506+
}
10507+
if let Some(ShowStatementFilterPosition::Suffix(filter)) = &self.filter_position {
10508+
write!(f, " {filter}")?;
10509+
}
1053010510
Ok(())
1053110511
}
1053210512
}

0 commit comments

Comments
 (0)