perf: Eliminate unnecessary string allocations in config handling

Đang mở
#438 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
backend, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Rust
Star
62
Fork
5
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
1

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của terraphim/terraphim-ai

Tất cả issue của terraphim/terraphim-ai

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.