Skip to content

Commit b37f244

Browse files
committed
perf(sqlite-native): gate kv operation labels
1 parent 28c85fd commit b37f244

1 file changed

Lines changed: 21 additions & 47 deletions

File tree

  • rivetkit-typescript/packages/sqlite-native/src

rivetkit-typescript/packages/sqlite-native/src/vfs.rs

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ fn read_cache_enabled() -> bool {
108108

109109
*READ_CACHE_ENABLED.get_or_init(|| {
110110
std::env::var(READ_CACHE_ENV_VAR)
111-
.map(|value| matches!(value.to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on"))
111+
.map(|value| {
112+
matches!(
113+
value.to_ascii_lowercase().as_str(),
114+
"1" | "true" | "yes" | "on"
115+
)
116+
})
112117
.unwrap_or(false)
113118
})
114119
}
@@ -177,91 +182,62 @@ impl VfsContext {
177182
}
178183

179184
fn kv_get(&self, keys: Vec<Vec<u8>>) -> Result<KvGetResult, String> {
180-
let op_name = format!("get({}keys)", keys.len());
185+
let key_count = keys.len();
181186
let start = std::time::Instant::now();
182187
let result = self
183188
.rt_handle
184189
.block_on(self.kv.batch_get(&self.actor_id, keys))
185190
.map_err(|e| e.to_string());
186191
let elapsed = start.elapsed();
187-
if std::env::var("RIVET_TRACE_SQL").is_ok() {
188-
eprintln!(
189-
"[sql-trace] kv_roundtrip op={} duration={}us",
190-
op_name,
191-
elapsed.as_micros()
192-
);
193-
}
194192
tracing::debug!(
195-
op = %op_name,
193+
op = %format_args!("get({key_count}keys)"),
196194
duration_us = elapsed.as_micros() as u64,
197195
"kv round-trip"
198196
);
199197
result
200198
}
201199

202200
fn kv_put(&self, keys: Vec<Vec<u8>>, values: Vec<Vec<u8>>) -> Result<(), String> {
203-
let op_name = format!("put({}keys)", keys.len());
201+
let key_count = keys.len();
204202
let start = std::time::Instant::now();
205203
let result = self
206204
.rt_handle
207205
.block_on(self.kv.batch_put(&self.actor_id, keys, values))
208206
.map_err(|e| e.to_string());
209207
let elapsed = start.elapsed();
210-
if std::env::var("RIVET_TRACE_SQL").is_ok() {
211-
eprintln!(
212-
"[sql-trace] kv_roundtrip op={} duration={}us",
213-
op_name,
214-
elapsed.as_micros()
215-
);
216-
}
217208
tracing::debug!(
218-
op = %op_name,
209+
op = %format_args!("put({key_count}keys)"),
219210
duration_us = elapsed.as_micros() as u64,
220211
"kv round-trip"
221212
);
222213
result
223214
}
224215

225216
fn kv_delete(&self, keys: Vec<Vec<u8>>) -> Result<(), String> {
226-
let op_name = format!("del({}keys)", keys.len());
217+
let key_count = keys.len();
227218
let start = std::time::Instant::now();
228219
let result = self
229220
.rt_handle
230221
.block_on(self.kv.batch_delete(&self.actor_id, keys))
231222
.map_err(|e| e.to_string());
232223
let elapsed = start.elapsed();
233-
if std::env::var("RIVET_TRACE_SQL").is_ok() {
234-
eprintln!(
235-
"[sql-trace] kv_roundtrip op={} duration={}us",
236-
op_name,
237-
elapsed.as_micros()
238-
);
239-
}
240224
tracing::debug!(
241-
op = %op_name,
225+
op = %format_args!("del({key_count}keys)"),
242226
duration_us = elapsed.as_micros() as u64,
243227
"kv round-trip"
244228
);
245229
result
246230
}
247231

248232
fn kv_delete_range(&self, start: Vec<u8>, end: Vec<u8>) -> Result<(), String> {
249-
let op_name = "delRange";
250233
let start_time = std::time::Instant::now();
251234
let result = self
252235
.rt_handle
253236
.block_on(self.kv.delete_range(&self.actor_id, start, end))
254237
.map_err(|e| e.to_string());
255238
let elapsed = start_time.elapsed();
256-
if std::env::var("RIVET_TRACE_SQL").is_ok() {
257-
eprintln!(
258-
"[sql-trace] kv_roundtrip op={} duration={}us",
259-
op_name,
260-
elapsed.as_micros()
261-
);
262-
}
263239
tracing::debug!(
264-
op = %op_name,
240+
op = "delRange",
265241
duration_us = elapsed.as_micros() as u64,
266242
"kv round-trip"
267243
);
@@ -632,15 +608,11 @@ unsafe extern "C" fn kv_io_write(
632608

633609
let mut entries_to_write = Vec::with_capacity(plans.len() + 1);
634610
for plan in &plans {
635-
let existing_chunk = plan
636-
.cached_chunk
637-
.as_deref()
638-
.or_else(|| {
639-
plan
640-
.existing_chunk_index
641-
.and_then(|idx| existing_chunks.get(idx))
642-
.and_then(|value| value.as_ref())
643-
});
611+
let existing_chunk = plan.cached_chunk.as_deref().or_else(|| {
612+
plan.existing_chunk_index
613+
.and_then(|idx| existing_chunks.get(idx))
614+
.and_then(|value| value.as_ref())
615+
});
644616

645617
let mut new_chunk = if let Some(existing_chunk) = existing_chunk {
646618
let mut chunk = vec![0u8; std::cmp::max(existing_chunk.len(), plan.write_end)];
@@ -924,7 +896,9 @@ unsafe extern "C" fn kv_io_file_control(
924896

925897
// Move dirty buffer entries into the read cache so subsequent
926898
// reads can serve them without a KV round-trip.
927-
let flushed: Vec<_> = std::mem::take(&mut state.dirty_buffer).into_iter().collect();
899+
let flushed: Vec<_> = std::mem::take(&mut state.dirty_buffer)
900+
.into_iter()
901+
.collect();
928902
if let Some(read_cache) = state.read_cache.as_mut() {
929903
// Only chunk pages belong in the read cache. The metadata write above
930904
// still goes through KV, but should not be cached as a page.

0 commit comments

Comments
 (0)