At a lot of places we should get away with just using properly lifetimed &str instead of String.
Go through the code and fix all the cases where this happens.
At a lot of places we should get away with just using properly lifetimed &str instead of String.
Go through the code and fix all the cases where this happens.
I had the AI analyse the codebase and it found several patterns causing unnecessary string allocations:
option_display function (85+ usages across 23 files)
String but is often used inside format!() macros, causing double allocationString::from("?") instead of "?" for the None caseUnnecessary String::from() in closures (~10 occurrences)
.unwrap_or_else(|| String::from("?")).unwrap_or("?")String literals in table construction
String::from("Title") where &str might suffice (depends on table API)Cow<'static, str>usestd::borrow::Cow;pubfn option_display<T: ToString>(opt: &Option<T>)-> Cow<'static,str>{opt.as_ref().map(|t|Cow::Owned(t.to_string())).unwrap_or(Cow::Borrowed("?"))}Pros:
Some, returns borrowed "?" for NoneDeref implementationformat!() and other string operationsCons:
Some case (unavoidable since we need ToString)Display trait wrapperpubstruct OptionDisplay<'a,T>(&'aOption<T>);impl<'a,T: ToString>DisplayforOptionDisplay<'a,T>{fn fmt(&self,f: &mutFormatter<'_>)-> fmt::Result{match&self.0{Some(v)=>write!(f,"{}",v.to_string()),None=>write!(f,"?"),}}}pubfn option_display<T>(opt: &Option<T>)-> OptionDisplay<'_,T>{OptionDisplay(opt)}Pros:
Cons:
Option A (Cow) provides immediate benefits with minimal disruption. We can then address the remaining allocations in the table construction separately if needed.
Happy to implement whichever approach you prefer, or explore other solutions if you have different ideas.
No due date set.
No dependencies set.
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?