Skip to content
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
## 2024-05-24 - Avoid `push_str(&format!(...))` in loops
**Learning:** Using `push_str(&format!(...))` inside loops creates unnecessary temporary String allocations because `format!` allocates a new `String` which is then appended and immediately dropped.
**Action:** Use `std::fmt::Write` and the `write!` macro directly onto the target string buffer instead.
## 2025-02-12 - String allocation optimization
**Learning:** In heavily used loops, especially for rendering or stringifying tasks, `push_str(&format!(...))` allocates temporary strings unnecessarily. Replacing it with `write!` directly onto the mutable `String` buffer (using `std::fmt::Write`) skips the intermediate allocations and improves performance significantly.
**Action:** Use `std::fmt::Write` and the `write!` macro instead of `push_str(&format!(...))` when generating strings in loops to prevent temporary allocations and improve performance.
40 changes: 19 additions & 21 deletions stdlib/src/web.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::fmt::Write;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -109,7 +110,7 @@ fn render_website(html: &mut String, dsl: &techscript_runtime::value::DslBlockVa
match prop.name.as_str() {
"title" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<title>{}</title>", t));
let _ = write!(html, "<title>{}</title>", t);
}
}
_ => {}
Expand Down Expand Up @@ -148,12 +149,12 @@ fn render_website(html: &mut String, dsl: &techscript_runtime::value::DslBlockVa
fn render_page(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) {
html.push_str("<div class=\"page\">");
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = dsl.args.first() {
html.push_str(&format!("<h1>{}</h1>", t));
let _ = write!(html, "<h1>{}</h1>", t);
}
for prop in &dsl.properties {
if prop.name == "title" {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<h2>{}</h2>", t));
let _ = write!(html, "<h2>{}</h2>", t);
}
}
}
Expand All @@ -167,12 +168,12 @@ fn render_hero(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue
match prop.name.as_str() {
"title" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<h1>{}</h1>", t));
let _ = write!(html, "<h1>{}</h1>", t);
}
}
"subtitle" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<p class=\"subtitle\">{}</p>", t));
let _ = write!(html, "<p class=\"subtitle\">{}</p>", t);
}
}
_ => {}
Expand All @@ -188,12 +189,12 @@ fn render_section(html: &mut String, dsl: &techscript_runtime::value::DslBlockVa
match prop.name.as_str() {
"title" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<h2>{}</h2>", t));
let _ = write!(html, "<h2>{}</h2>", t);
}
}
"id" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<!-- id: {} -->", t));
let _ = write!(html, "<!-- id: {} -->", t);
}
}
_ => {}
Expand All @@ -211,17 +212,17 @@ fn render_card(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue
match prop.name.as_str() {
"title" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<h3>{}</h3>", t));
let _ = write!(html, "<h3>{}</h3>", t);
}
}
"text" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<p>{}</p>", t));
let _ = write!(html, "<p>{}</p>", t);
}
}
"image" => {
if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<img src=\"{}\" alt=\"card image\">", t));
let _ = write!(html, "<img src=\"{}\" alt=\"card image\">", t);
}
}
_ => {}
Expand Down Expand Up @@ -259,7 +260,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
.and_then(|p| p.value.as_ref())
.map(|v| v.to_string())
.unwrap_or_else(|| "Button".to_string());
html.push_str(&format!("<button>{}</button>", label));
let _ = write!(html, "<button>{}</button>", label);
}
"link" => {
let label = dsl
Expand All @@ -276,9 +277,9 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
.and_then(|p| p.value.as_ref())
.map(|v| v.to_string());
if let Some(u) = url {
html.push_str(&format!("<a href=\"{}\">{}</a>", u, label));
let _ = write!(html, "<a href=\"{}\">{}</a>", u, label);
} else {
html.push_str(&format!("<a href=\"#\">{}</a>", label));
let _ = write!(html, "<a href=\"#\">{}</a>", label);
}
}
"nav" => {
Expand All @@ -291,7 +292,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
for prop in &dsl.properties {
if prop.name == "title" {
if let Some(RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<h1>{}</h1>", t));
let _ = write!(html, "<h1>{}</h1>", t);
}
}
}
Expand All @@ -303,7 +304,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
for prop in &dsl.properties {
if prop.name == "text" {
if let Some(RuntimeValue::Str(t)) = &prop.value {
html.push_str(&format!("<p>{}</p>", t));
let _ = write!(html, "<p>{}</p>", t);
}
}
}
Expand All @@ -318,7 +319,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
.and_then(|p| p.value.as_ref())
.map(|v| v.to_string());
if let Some(p) = placeholder {
html.push_str(&format!("<input placeholder=\"{}\">", p));
let _ = write!(html, "<input placeholder=\"{}\">", p);
} else {
html.push_str("<input>");
}
Expand Down Expand Up @@ -346,13 +347,10 @@ fn dsl_to_html(val: &RuntimeValue) -> String {
.and_then(|p| p.value.as_ref())
.map(|v| v.to_string())
.unwrap_or_else(|| "Get Started".to_string());
html.push_str(&format!(
"<a class=\"start-button\" href=\"#\">{}</a>",
label
));
let _ = write!(html, "<a class=\"start-button\" href=\"#\">{}</a>", label);
}
_ => {
html.push_str(&format!("<!-- unknown DSL block: {} -->", dsl.kind));
let _ = write!(html, "<!-- unknown DSL block: {} -->", dsl.kind);
}
}
html
Expand Down
3 changes: 2 additions & 1 deletion stdlib/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Write;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};

Expand Down Expand Up @@ -44,7 +45,7 @@ impl StdlibRegistry {
if let RuntimeValue::Map { entries, .. } = &args[0] {
for (k, v) in entries.borrow().iter() {
let val_str = v.try_into_string().unwrap_or_default();
result.push_str(&format!("<{}>{}</{}>", k, val_str, k));
let _ = write!(result, "<{}>{}</{}>", k, val_str, k);
}
}
Ok(RuntimeValue::Str(result))
Expand Down
3 changes: 2 additions & 1 deletion stdlib/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Write;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};

Expand Down Expand Up @@ -47,7 +48,7 @@ impl StdlibRegistry {
if let RuntimeValue::Map { entries, .. } = &args[0] {
for (k, v) in entries.borrow().iter() {
let val_str = v.try_into_string().unwrap_or_default();
result.push_str(&format!("{}: {}\n", k, val_str));
let _ = write!(result, "{}: {}\n", k, val_str);
}
}
Ok(RuntimeValue::Str(result))
Expand Down
Loading