Skip to content

Instantly share code, notes, and snippets.

@Arkoniak
Last active September 1, 2025 08:48
Show Gist options
  • Save Arkoniak/d93a3741ae4102bf4af1e12f6fcda622 to your computer and use it in GitHub Desktop.
Save Arkoniak/d93a3741ae4102bf4af1e12f6fcda622 to your computer and use it in GitHub Desktop.
Turso db counter performance
use turso_ext::{register_extension, AggFunc, AggregateDerive, Value};
register_extension! {
aggregates: { TestCount1a, TestCount1, TestCount2, TestCount3 }
}
#[derive(AggregateDerive)]
struct TestCount1a;
impl AggFunc for TestCount1a {
type State = i64;
type Error = &'static str;
const NAME: &'static str = "test_count1a";
const ARGS: i32 = 0;
fn step(state: &mut Self::State, args: &[Value]) {
_ = args;
*state += 1;
}
fn finalize(state: Self::State) -> Result<Value, Self::Error> {
Ok(Value::from_integer(state))
}
}
#[derive(AggregateDerive)]
struct TestCount1;
impl AggFunc for TestCount1 {
type State = i64;
type Error = &'static str;
const NAME: &'static str = "test_count1";
const ARGS: i32 = 1;
fn step(state: &mut Self::State, args: &[Value]) {
_ = args;
*state += 1;
}
fn finalize(state: Self::State) -> Result<Value, Self::Error> {
Ok(Value::from_integer(state))
}
}
#[derive(AggregateDerive)]
struct TestCount2;
impl AggFunc for TestCount2 {
type State = i64;
type Error = &'static str;
const NAME: &'static str = "test_count2";
const ARGS: i32 = 1;
fn step(state: &mut Self::State, args: &[Value]) {
if let Some(x) = args.first().and_then(Value::to_integer) {
_ = x;
*state += 1;
}
}
fn finalize(state: Self::State) -> Result<Value, Self::Error> {
Ok(Value::from_integer(state))
}
}
#[derive(AggregateDerive)]
struct TestCount3;
impl AggFunc for TestCount3 {
type State = i64;
type Error = &'static str;
const NAME: &'static str = "test_count3";
const ARGS: i32 = 1;
fn step(state: &mut Self::State, args: &[Value]) {
if let Some(x) = args.first().and_then(Value::to_float) {
_ = x;
*state += 1;
}
}
fn finalize(state: Self::State) -> Result<Value, Self::Error> {
Ok(Value::from_integer(state))
}
}
@Arkoniak
Copy link
Author

Arkoniak commented Sep 1, 2025

hyperfine measurments

> hyperfine -w 1 -r 10 'turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count1a() FROM test;"'
Benchmark 1: turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count1a() FROM test;"
  Time (mean ± σ):     388.8 ms ±   4.5 ms    [User: 354.3 ms, System: 33.9 ms]
  Range (min … max):   381.4 ms … 394.2 ms    10 runs
 
> hyperfine -w 1 -r 10 'turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count1(x) FROM test;"'
Benchmark 1: turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count1(x) FROM test;"
  Time (mean ± σ):     752.1 ms ±   3.8 ms    [User: 714.3 ms, System: 36.9 ms]
  Range (min … max):   744.1 ms … 758.0 ms    10 runs
 
> hyperfine -w 1 -r 10 'turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count2(x) FROM test;"'
Benchmark 1: turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count2(x) FROM test;"
  Time (mean ± σ):     759.3 ms ±   9.3 ms    [User: 721.7 ms, System: 36.5 ms]
  Range (min … max):   749.2 ms … 783.4 ms    10 runs
 
> hyperfine -w 1 -r 10 'turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count3(x) FROM test;"'
Benchmark 1: turso/target/release/tursodb -m list db2 "SELECT load_extension(\"turso/target/release/libturso_statistics\"); SELECT test_count3(x) FROM test;"
  Time (mean ± σ):     761.1 ms ±  10.4 ms    [User: 722.4 ms, System: 37.8 ms]
  Range (min … max):   751.0 ms … 788.1 ms    10 runs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment