perf(agent): avoid cloning working-tier records in consolidation add_memory
score_surprise only reads r.embedding by reference, so cloning every Working-tier record's full chunk text + embedding Vec<f32> on every add_memory call was wasted work, discarded immediately after use. Collect Vec<&MemoryRecord> instead and change score_surprise's signature to take &[&MemoryRecord]. INT-14
This commit is contained in:
@@ -118,7 +118,7 @@ impl ImportanceScorer {
|
|||||||
|
|
||||||
/// Novelty score: 1.0 − max cosine similarity against all existing records.
|
/// Novelty score: 1.0 − max cosine similarity against all existing records.
|
||||||
/// Returns 1.0 when there are no existing memories.
|
/// Returns 1.0 when there are no existing memories.
|
||||||
pub fn score_surprise(embedding: &[f32], existing_memories: &[MemoryRecord]) -> f32 {
|
pub fn score_surprise(embedding: &[f32], existing_memories: &[&MemoryRecord]) -> f32 {
|
||||||
if existing_memories.is_empty() {
|
if existing_memories.is_empty() {
|
||||||
return 1.0;
|
return 1.0;
|
||||||
}
|
}
|
||||||
@@ -209,11 +209,10 @@ impl ConsolidationEngine {
|
|||||||
source: MemorySource,
|
source: MemorySource,
|
||||||
now: f64,
|
now: f64,
|
||||||
) -> u64 {
|
) -> u64 {
|
||||||
let working: Vec<MemoryRecord> = self
|
let working: Vec<&MemoryRecord> = self
|
||||||
.records
|
.records
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|r| r.tier == MemoryTier::Working)
|
.filter(|r| r.tier == MemoryTier::Working)
|
||||||
.cloned()
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let surprise = ImportanceScorer::score_surprise(&embedding, &working);
|
let surprise = ImportanceScorer::score_surprise(&embedding, &working);
|
||||||
@@ -464,7 +463,8 @@ mod tests {
|
|||||||
created_at: 0.0,
|
created_at: 0.0,
|
||||||
source: MemorySource::User,
|
source: MemorySource::User,
|
||||||
}];
|
}];
|
||||||
let score = ImportanceScorer::score_surprise(&emb, &existing);
|
let existing_refs: Vec<&MemoryRecord> = existing.iter().collect();
|
||||||
|
let score = ImportanceScorer::score_surprise(&emb, &existing_refs);
|
||||||
assert!(score < 0.01, "expected ~0.0, got {score}");
|
assert!(score < 0.01, "expected ~0.0, got {score}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user