Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions crates/rustern-core/src/pipeline/jq_evaluate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::sync::Arc;

use futures::stream::{Stream, StreamExt};
use jaq_core::data::JustLut;
use jaq_core::load::{Arena, File, Loader};
use jaq_core::{Compiler, Ctx, Filter, Vars};
use jaq_json::Val;
use std::fmt::Write as _;
use std::sync::Arc;

use futures::stream::{Stream, StreamExt};

use crate::source::{LogEvent, LogSourceError};

Expand All @@ -23,6 +24,17 @@ pub enum QueryMode {
Filter,
}

fn format_outputs_joined(outputs: &[Val]) -> String {
let mut rendered = String::new();
for (i, v) in outputs.iter().enumerate() {
if i > 0 {
rendered.push('\n');
}
let _ = write!(rendered, "{v}");
}
rendered
}
Comment on lines +27 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

軽微: 容量事前確保でホットパスの再割当てを削減可能

String::new() は初期容量ゼロのため、要素数が多い場合に複数回の再割当てが発生し得ます。パイプラインのホットパスにおける per-line allocation 最適化が本PRの目的なので、with_capacity によるヒントもついでに検討すると良さそうです。

As per path instructions, "Performance on hot paths (per-line allocations, regex reuse)" が pipeline ディレクトリの重点項目です。

♻️ 提案
 fn format_outputs_joined(outputs: &[Val]) -> String {
-    let mut rendered = String::new();
+    let mut rendered = String::with_capacity(outputs.len() * 16);
     for (i, v) in outputs.iter().enumerate() {
         if i > 0 {
             rendered.push('\n');
         }
         let _ = write!(rendered, "{v}");
     }
     rendered
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn format_outputs_joined(outputs: &[Val]) -> String {
let mut rendered = String::new();
for (i, v) in outputs.iter().enumerate() {
if i > 0 {
rendered.push('\n');
}
let _ = write!(rendered, "{v}");
}
rendered
}
fn format_outputs_joined(outputs: &[Val]) -> String {
let mut rendered = String::with_capacity(outputs.len() * 16);
for (i, v) in outputs.iter().enumerate() {
if i > 0 {
rendered.push('\n');
}
let _ = write!(rendered, "{v}");
}
rendered
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/rustern-core/src/pipeline/jq_evaluate.rs` around lines 27 - 36, The
hot-path formatter in format_outputs_joined currently starts with String::new(),
which can trigger repeated reallocations as outputs grow. Update this helper to
preallocate the buffer with an appropriate capacity hint using with_capacity,
based on the expected number of Val items and separators, while keeping the
existing join logic in place. Focus the change inside format_outputs_joined so
the per-line allocation behavior in jq_evaluate is reduced without altering the
rendered output.

Source: Path instructions


#[derive(Clone)]
pub struct CompiledFilter {
inner: Arc<Filter<JustLut<Val>>>,
Expand Down Expand Up @@ -105,21 +117,13 @@ where
}
QueryMode::Replace => {
ev.structured = None;
let rendered = outputs
.iter()
.map(|v| format!("{v}"))
.collect::<Vec<_>>()
.join("\n");
let rendered = format_outputs_joined(&outputs);
ev.message = Arc::from(rendered.as_str());
Some(Ok(ev))
}
QueryMode::Append => {
ev.structured = None;
let rendered = outputs
.iter()
.map(|v| format!("{v}"))
.collect::<Vec<_>>()
.join("\n");
let rendered = format_outputs_joined(&outputs);
ev.message =
Arc::from(format!("{} | {}", ev.message, rendered).as_str());
Some(Ok(ev))
Expand Down