perf: Eliminate unnecessary string allocations in config handling
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- rust
- Domain
- backend, performance
Research direction
Start in crates/terraphim_service/src/lib.rs at lines 86-89, 195-198, 342-348, 421-428, and 527-532, then trace callers of the configuration helpers. Review the performance analysis from PR #429 and check all affected return types. Done means the unnecessary clones and allocations are removed while configuration handling retains its behavior and the expected allocation improvement is verified.
Written by the indexing model from the issue text.
Description
Issue Description
Unnecessary string cloning and allocations in configuration lookups add GC pressure.
Location
crates/terraphim_service/src/lib.rs (lines 86-89, 195-198, 342-348, 421-428, 527-532)
Current Code
```rust
// Line 86-89: Cloning entire HashMap
let nested_map: AHashMap<String, Value> = nested_obj
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
// Line 195-198: Unnecessary to_string()
fn get_string_extra(extra: &AHashMap<String, Value>, key: &str) -> Option {
extra.get(key).and_then(|v| v.as_str().map(|s| s.to_string()))
// ^^^^^^^^^^^^^^^^ Unnecessary allocation
}
```
Impact
- MEDIUM priority - Performance
- Adds GC pressure for frequent calls
- Unnecessary memory allocations
Recommended Fix
```rust
// Return Cow to avoid allocation when possible
fn get_string_extra<'a>(
extra: &'a AHashMap<String, Value>,
key: &str
) -> Option<Cow<'a, str>> {
extra.get(key).and_then(|v| {
v.as_str().map(|s| Cow::Borrowed(s))
})
}
// For nested_map, use references instead
fn build_from_nested_extra(
nested_obj: &serde_json::Map<String, Value>
) -> Option<Arc> {
// Use get_string_extra directly on nested_obj
if let Some(provider) = nested_obj.get("llm_provider")
.and_then(|v| v.as_str())
{
// ...
}
}
```
Expected Improvement
- 20-30% reduction in allocations for config lookups
- Reduced memory pressure
- Faster config operations
References
- Identified in performance analysis for PR #429
- Related to configuration handling
- Dominant language
- Rust
- Stars
- 62
- Forks
- 5
- Avg merge
- 2h 27m
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from terraphim/terraphim-ai
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
terraphim/terraphim-ai#885 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
terraphim/terraphim-ai#871 ·
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
terraphim/terraphim-ai#810 · 2 comments ·
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
terraphim/terraphim-ai#729 ·
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
terraphim/terraphim-ai#728 ·
All issues in terraphim/terraphim-ai
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
bevyengine/bevy#25861 ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
enhancement remote
Difficulty 2/5 1-3 hours Newbie friendliness 68/100