perf: Eliminate unnecessary string allocations in config handling

Aperta
#438 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Rust
Stelle
62
Fork
5
Merge medio
2h 27m
PR unite (30g)
1

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di terraphim/terraphim-ai

Tutte le issue di terraphim/terraphim-ai

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.