Avoid creating an aggregation context unnecessarily if the function is

called against 0 rows.
This commit is contained in:
John Gallagher 2016-01-07 15:14:24 -05:00
parent 267018b80d
commit ca761d7697

View File

@ -329,8 +329,9 @@ impl<'a> Context<'a> {
/// `A` is the type of the aggregation context and `T` is the type of the final result. /// `A` is the type of the aggregation context and `T` is the type of the final result.
/// Implementations should be stateless. /// Implementations should be stateless.
pub trait Aggregate<A, T> where T: ToResult { pub trait Aggregate<A, T> where T: ToResult {
/// Initializes the aggregation context. Will be called exactly once for each /// Initializes the aggregation context. Will be called prior to the first call
/// invocation of the function. /// to `step()` to set up the context for an invocation of the function. (Note:
/// `init()` will not be called if the there are no rows.)
fn init(&self) -> A; fn init(&self) -> A;
/// "step" function called once for each row in an aggregate group. May be called /// "step" function called once for each row in an aggregate group. May be called
@ -338,8 +339,11 @@ pub trait Aggregate<A, T> where T: ToResult {
fn step(&self, &mut Context, &mut A) -> Result<()>; fn step(&self, &mut Context, &mut A) -> Result<()>;
/// Computes and returns the final result. Will be called exactly once for each /// Computes and returns the final result. Will be called exactly once for each
/// invocation of the function. /// invocation of the function. If `step()` was called at least once, will be given
fn finalize(&self, A) -> Result<T>; /// `Some(A)` (the same `A` as was created by `init` and given to `step`); if `step()`
/// was not called (because the function is running against 0 rows), will be given
/// `None`.
fn finalize(&self, Option<A>) -> Result<T>;
} }
impl Connection { impl Connection {
@ -485,21 +489,13 @@ impl InnerConnection {
where D: Aggregate<A, T>, where D: Aggregate<A, T>,
T: ToResult T: ToResult
{ {
// Get our aggregation context from the sqlite3_context. unsafe fn aggregate_context<A>(ctx: *mut sqlite3_context) -> Option<*mut *mut A> {
unsafe fn aggregate_context<A, D, T>(agg: &D, ctx: *mut sqlite3_context) -> Result<*mut A>
where D: Aggregate<A, T>,
T: ToResult
{
let pac = ffi::sqlite3_aggregate_context(ctx, ::std::mem::size_of::<*mut A>() as c_int) let pac = ffi::sqlite3_aggregate_context(ctx, ::std::mem::size_of::<*mut A>() as c_int)
as *mut *mut A; as *mut *mut A;
if pac.is_null() { if pac.is_null() {
return Err(Error::SqliteFailure(ffi::Error::new(ffi::SQLITE_NOMEM), None)); return None;
} }
if (*pac).is_null() { Some(pac)
let a = agg.init();
*pac = Box::into_raw(Box::new(a));
}
Ok(*pac)
} }
unsafe fn report_aggregate_error(ctx: *mut sqlite3_context, err: Error) { unsafe fn report_aggregate_error(ctx: *mut sqlite3_context, err: Error) {
@ -529,21 +525,25 @@ impl InnerConnection {
assert!(!boxed_aggr.is_null(), assert!(!boxed_aggr.is_null(),
"Internal error - null aggregate pointer"); "Internal error - null aggregate pointer");
let agg_ctx = match aggregate_context(&*boxed_aggr, ctx) { let pac = match aggregate_context(ctx) {
Ok(agg_ctx) => agg_ctx, Some(pac) => pac,
Err(_) => { None => {
ffi::sqlite3_result_error_nomem(ctx); ffi::sqlite3_result_error_nomem(ctx);
return; return;
} }
}; };
if (*pac).is_null() {
*pac = Box::into_raw(Box::new((*boxed_aggr).init()));
}
let mut ctx = Context { let mut ctx = Context {
ctx: ctx, ctx: ctx,
args: slice::from_raw_parts(argv, argc as usize), args: slice::from_raw_parts(argv, argc as usize),
}; };
match (*boxed_aggr).step(&mut ctx, &mut *agg_ctx) { match (*boxed_aggr).step(&mut ctx, &mut **pac) {
Ok(_) => {}, Ok(_) => {}
Err(err) => report_aggregate_error(ctx.ctx, err), Err(err) => report_aggregate_error(ctx.ctx, err),
}; };
} }
@ -556,17 +556,22 @@ impl InnerConnection {
assert!(!boxed_aggr.is_null(), assert!(!boxed_aggr.is_null(),
"Internal error - null aggregate pointer"); "Internal error - null aggregate pointer");
let agg_ctx = match aggregate_context(&*boxed_aggr, ctx) { let pac = match aggregate_context(ctx) {
Ok(agg_ctx) => agg_ctx, Some(pac) => pac,
Err(_) => { None => {
ffi::sqlite3_result_error_nomem(ctx); ffi::sqlite3_result_error_nomem(ctx);
return; return;
} }
}; };
let a = Box::from_raw(agg_ctx); // to be freed let a: Option<A> = if (*pac).is_null() {
None
} else {
let a = Box::from_raw(*pac);
Some(*a)
};
match (*boxed_aggr).finalize(*a) { match (*boxed_aggr).finalize(a) {
Ok(r) => r.set_result(ctx), Ok(r) => r.set_result(ctx),
Err(err) => report_aggregate_error(ctx, err), Err(err) => report_aggregate_error(ctx, err),
}; };
@ -780,13 +785,13 @@ mod test {
struct Sum; struct Sum;
struct Count; struct Count;
impl Aggregate<Option<i64>, Option<i64>> for Sum { impl Aggregate<i64, Option<i64>> for Sum {
fn init(&self) -> Option<i64> { fn init(&self) -> i64 {
None 0
} }
fn step(&self, ctx: &mut Context, sum: &mut Option<i64>) -> Result<()> { fn step(&self, ctx: &mut Context, sum: &mut i64) -> Result<()> {
*sum = Some(sum.unwrap_or(0) + try!(ctx.get::<i64>(0))); *sum += try!(ctx.get::<i64>(0));
Ok(()) Ok(())
} }
@ -805,8 +810,8 @@ mod test {
Ok(()) Ok(())
} }
fn finalize(&self, sum: i64) -> Result<i64> { fn finalize(&self, sum: Option<i64>) -> Result<i64> {
Ok(sum) Ok(sum.unwrap_or(0))
} }
} }