perf: Eliminate unnecessary string allocations in config handling

Open
#438 0 comments 0 reactions 0 assignees View on GitHub

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

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

enhancement rust

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from terraphim/terraphim-ai

All issues in terraphim/terraphim-ai

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.