Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

perf(rust-sdk): build a mount's exception context only when a handler is in scope (#2783)

LJ πŸ₯₯🌴76b7dbb12a5c
diff --git a/rust/sdk/cocoindex/src/ctx.rs b/rust/sdk/cocoindex/src/ctx.rs
index b69b20b1..c4bbf35c 100644
--- a/rust/sdk/cocoindex/src/ctx.rs
+++ b/rust/sdk/cocoindex/src/ctx.rs
@@ -1080,18 +1080,15 @@ impl Ctx {
f.clone(),
processor_name.clone(),
);
- let on_error = build_chained_on_error(
- &handler_chain,
- ExceptionContext {
- env_name: env_name.clone(),
- stable_path: stable_path.clone(),
- parent_stable_path: None,
- processor_name: Some(processor_name.clone()),
- mount_kind: MountKind::UpdateFull,
- is_background: ready_marked && controller.is_live(),
- source: ExceptionSource::Component,
- },
- );
+ let on_error = build_chained_on_error(&handler_chain, || ExceptionContext {
+ env_name: env_name.clone(),
+ stable_path: stable_path.clone(),
+ parent_stable_path: None,
+ processor_name: Some(processor_name.clone()),
+ mount_kind: MountKind::UpdateFull,
+ is_background: ready_marked && controller.is_live(),
+ source: ExceptionSource::Component,
+ });
match controller.update_full(processor, on_error).await {
Ok(()) => {
controller.mark_ready().await;
@@ -1405,18 +1402,15 @@ impl Ctx {
.with_context_memo_state(has_context_memo_state)
.with_state_inputs(memo.map(|memo| memo.state_inputs).unwrap_or_default());
- let on_error = build_chained_on_error(
- &handler_chain,
- ExceptionContext {
- env_name: self.state.name.clone(),
- stable_path: child_path.to_string(),
- parent_stable_path: Some(self.mount_base_path(comp_ctx).to_string()),
- processor_name: Some(processor_name),
- mount_kind: MountKind::Mount,
- is_background: true,
- source: ExceptionSource::Component,
- },
- );
+ let on_error = build_chained_on_error(&handler_chain, || ExceptionContext {
+ env_name: self.state.name.clone(),
+ stable_path: child_path.to_string(),
+ parent_stable_path: Some(self.mount_base_path(comp_ctx).to_string()),
+ processor_name: Some(processor_name),
+ mount_kind: MountKind::Mount,
+ is_background: true,
+ source: ExceptionSource::Component,
+ });
child_component
.mount(comp_ctx, processor, on_error)
.await
diff --git a/rust/sdk/cocoindex/src/live_component.rs b/rust/sdk/cocoindex/src/live_component.rs
index 7add4649..44b5c0f6 100644
--- a/rust/sdk/cocoindex/src/live_component.rs
+++ b/rust/sdk/cocoindex/src/live_component.rs
@@ -128,13 +128,18 @@ pub type ExceptionHandler =
/// rewritten) error propagates to the next outer handler, with
/// [`ExceptionContext::source`] set to [`ExceptionSource::Handler`], and
/// finally out.
+///
+/// `make_ctx` runs only when `chain` has a handler: with none in scope (the
+/// common case) the context is never built, so a mount pays no path
+/// formatting for an error that may never happen.
pub(crate) fn build_chained_on_error(
chain: &[ExceptionHandler],
- ctx: ExceptionContext,
+ make_ctx: impl FnOnce() -> ExceptionContext,
) -> Option<OnError> {
if chain.is_empty() {
return None;
}
+ let ctx = make_ctx();
let chain: Vec<ExceptionHandler> = chain.to_vec();
Some(Arc::new(move |err: CoreError| {
let chain = chain.clone();
@@ -269,7 +274,7 @@ impl LiveComponentOperator {
format!("{}:process", self.name),
);
let on_error =
- build_chained_on_error(&self.handler_chain, self.exc_ctx(MountKind::UpdateFull));
+ build_chained_on_error(&self.handler_chain, || self.exc_ctx(MountKind::UpdateFull));
self.controller
.update_full(processor, on_error)
.await
@@ -306,10 +311,9 @@ impl LiveComponentOperator {
None,
format!("{}:update", self.name),
);
- let on_error = build_chained_on_error(
- &self.handler_chain,
- self.child_exc_ctx(MountKind::Update, &child_path),
- );
+ let on_error = build_chained_on_error(&self.handler_chain, || {
+ self.child_exc_ctx(MountKind::Update, &child_path)
+ });
let handle = self
.controller
.update(child_path, processor, on_error)
@@ -337,10 +341,9 @@ impl LiveComponentOperator {
/// returned handle's [`ready`](ReadyHandle::ready) awaits its sync.
pub async fn start_delete<K: Display>(&self, key: K) -> Result<ReadyHandle> {
let child_path = self.child_path(&key);
- let on_error = build_chained_on_error(
- &self.handler_chain,
- self.child_exc_ctx(MountKind::Delete, &child_path),
- );
+ let on_error = build_chained_on_error(&self.handler_chain, || {
+ self.child_exc_ctx(MountKind::Delete, &child_path)
+ });
let handle = self
.controller
.delete(child_path, on_error)