Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove allocations in Markup by switching to Cow<'static, str> #585

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions numbat-cli/src/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,23 @@ impl Highlighter for NumbatHighlighter {
if ctx.variable_names().any(|n| n == candidate)
|| ctx.function_names().any(|n| format!("{n}(") == candidate)
{
Cow::Owned(ansi_format(&markup::identifier(candidate), false))
Cow::Owned(ansi_format(
&markup::identifier(candidate.to_string()),
false,
))
} else if ctx
.unit_names()
.iter()
.any(|un| un.iter().any(|n| n == candidate))
{
Cow::Owned(ansi_format(&markup::unit(candidate), false))
Cow::Owned(ansi_format(&markup::unit(candidate.to_string()), false))
} else if ctx.dimension_names().iter().any(|n| n == candidate) {
Cow::Owned(ansi_format(&markup::type_identifier(candidate), false))
Cow::Owned(ansi_format(
&markup::type_identifier(candidate.to_string()),
false,
))
} else if KEYWORDS.iter().any(|k| k == &candidate) {
Cow::Owned(ansi_format(&markup::keyword(candidate), false))
Cow::Owned(ansi_format(&markup::keyword(candidate.to_string()), false))
} else {
Cow::Borrowed(candidate)
}
Expand Down
2 changes: 1 addition & 1 deletion numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl Cli {
let m = m::text(
"successfully saved session history to",
) + m::space()
+ m::string(dst);
+ m::string(dst.to_string());
println!("{}", ansi_format(&m, interactive));
}
Err(err) => {
Expand Down
35 changes: 20 additions & 15 deletions numbat/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,26 @@ impl PrettyPrint for BinaryOperator {
fn pretty_print(&self) -> Markup {
use BinaryOperator::*;

let operator = m::operator(match self {
Add => "+",
Sub => "-",
Mul => "×",
Div => "/",
Power => "^",
ConvertTo => "➞",
LessThan => "<",
GreaterThan => ">",
LessOrEqual => "≤",
GreaterOrEqual => "≥",
Equal => "==",
NotEqual => "≠",
LogicalAnd => "&&",
LogicalOr => "||",
});

match self {
Add => m::space() + m::operator("+") + m::space(),
Sub => m::space() + m::operator("-") + m::space(),
Mul => m::space() + m::operator("×") + m::space(),
Div => m::space() + m::operator("/") + m::space(),
Power => m::operator("^"),
ConvertTo => m::space() + m::operator("➞") + m::space(),
LessThan => m::space() + m::operator("<") + m::space(),
GreaterThan => m::space() + m::operator(">") + m::space(),
LessOrEqual => m::space() + m::operator("≤") + m::space(),
GreaterOrEqual => m::space() + m::operator("≥") + m::space(),
Equal => m::space() + m::operator("==") + m::space(),
NotEqual => m::space() + m::operator("≠") + m::space(),
LogicalAnd => m::space() + m::operator("&&") + m::space(),
LogicalOr => m::space() + m::operator("||") + m::space(),
Power => operator,
_ => m::space() + operator + m::space(),
}
}
}
Expand Down Expand Up @@ -365,7 +370,7 @@ impl PrettyPrint for TypeExpression {
fn pretty_print(&self) -> Markup {
match self {
TypeExpression::Unity(_) => m::type_identifier("1"),
TypeExpression::TypeIdentifier(_, ident) => m::type_identifier(ident),
TypeExpression::TypeIdentifier(_, ident) => m::type_identifier(ident.clone()),
TypeExpression::Multiply(_, lhs, rhs) => {
lhs.pretty_print() + m::space() + m::operator("×") + m::space() + rhs.pretty_print()
}
Expand Down
7 changes: 4 additions & 3 deletions numbat/src/column_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ impl ColumnFormatter {

if min_num_columns < 1 {
for entry in entries {
result += Markup::from(FormattedString(OutputType::Normal, format, entry))
+ m::whitespace(" ".repeat(self.padding));
result +=
Markup::from(FormattedString(OutputType::Normal, format, entry.into()))
+ m::whitespace(" ".repeat(self.padding));
}
return result;
}
Expand Down Expand Up @@ -81,7 +82,7 @@ impl ColumnFormatter {
result += Markup::from(FormattedString(
OutputType::Normal,
format,
(*entry).into(),
entry.to_string().into(),
));
result += m::whitespace(" ".repeat(whitespace_length));
} else {
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn help_markup() -> m::Markup {
let mut example_context = Context::new(BuiltinModuleImporter::default());
let _use_prelude_output = evaluate_example(&mut example_context, "use prelude");
for example in examples.iter() {
output += m::text(">>> ") + m::text(example) + m::nl();
output += m::text(">>> ") + m::text(*example) + m::nl();
output += evaluate_example(&mut example_context, example) + m::nl();
}
output
Expand Down
44 changes: 26 additions & 18 deletions numbat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ impl Context {
.ok()
.map(|(_, md)| md)
{
let mut help = m::text("Unit: ") + m::unit(md.name.as_deref().unwrap_or(keyword));
let mut help =
m::text("Unit: ") + m::unit(md.name.unwrap_or_else(|| keyword.to_string()));
if let Some(url) = &md.url {
help += m::text(" (") + m::string(url_encode(url)) + m::text(")");
}
Expand All @@ -357,12 +358,13 @@ impl Context {
let desc = "Description: ";
let mut lines = description.lines();
help += m::text(desc)
+ m::text(lines.by_ref().next().unwrap_or("").trim())
+ m::text(lines.by_ref().next().unwrap_or("").trim().to_string())
+ m::nl();

for line in lines {
help +=
m::whitespace(" ".repeat(desc.len())) + m::text(line.trim()) + m::nl();
help += m::whitespace(" ".repeat(desc.len()))
+ m::text(line.trim().to_string())
+ m::nl();
}
}

Expand All @@ -385,17 +387,17 @@ impl Context {
if !prefix.is_none() {
help += m::nl()
+ m::value("1 ")
+ m::unit(keyword)
+ m::unit(keyword.to_string())
+ m::text(" = ")
+ m::value(prefix.factor().pretty_print())
+ m::space()
+ m::unit(&full_name);
+ m::unit(full_name.clone());
}

if let Some(BaseUnitAndFactor(prod, num)) = x {
help += m::nl()
+ m::value("1 ")
+ m::unit(&full_name)
+ m::unit(full_name.clone())
+ m::text(" = ")
+ m::value(num.pretty_print())
+ m::space()
Expand All @@ -407,7 +409,7 @@ impl Context {
Some(m::FormatType::Unit),
);
} else {
help += m::nl() + m::unit(&full_name) + m::text(" is a base unit");
help += m::nl() + m::unit(full_name.clone()) + m::text(" is a base unit");
}
};

Expand All @@ -420,9 +422,9 @@ impl Context {
if let Some(l) = self.interpreter.lookup_global(keyword) {
let mut help = m::text("Variable: ");
if let Some(name) = &l.metadata.name {
help += m::text(name);
help += m::text(name.clone());
} else {
help += m::identifier(keyword);
help += m::identifier(keyword.to_string());
}
if let Some(url) = &l.metadata.url {
help += m::text(" (") + m::string(url_encode(url)) + m::text(")");
Expand All @@ -432,11 +434,14 @@ impl Context {
if let Some(description) = &l.metadata.description {
let desc = "Description: ";
let mut lines = description.lines();
help +=
m::text(desc) + m::text(lines.by_ref().next().unwrap_or("").trim()) + m::nl();
help += m::text(desc)
+ m::text(lines.by_ref().next().unwrap_or("").trim().to_string())
+ m::nl();

for line in lines {
help += m::whitespace(" ".repeat(desc.len())) + m::text(line.trim()) + m::nl();
help += m::whitespace(" ".repeat(desc.len()))
+ m::text(line.trim().to_string())
+ m::nl();
}
}

Expand Down Expand Up @@ -465,9 +470,9 @@ impl Context {

let mut help = m::text("Function: ");
if let Some(name) = &metadata.name {
help += m::text(name);
help += m::text(name.to_string());
} else {
help += m::identifier(keyword);
help += m::identifier(keyword.to_string());
}
if let Some(url) = &metadata.url {
help += m::text(" (") + m::string(url_encode(url)) + m::text(")");
Expand All @@ -482,11 +487,14 @@ impl Context {
if let Some(description) = &metadata.description {
let desc = "Description: ";
let mut lines = description.lines();
help +=
m::text(desc) + m::text(lines.by_ref().next().unwrap_or("").trim()) + m::nl();
help += m::text(desc)
+ m::text(lines.by_ref().next().unwrap_or("").trim().to_string())
+ m::nl();

for line in lines {
help += m::whitespace(" ".repeat(desc.len())) + m::text(line.trim()) + m::nl();
help += m::whitespace(" ".repeat(desc.len()))
+ m::text(line.trim().to_string())
+ m::nl();
}
}

Expand Down
Loading
Loading