Merge pull request #123 from jgallagher/gwenn-aggregate

Ensure there is no allocation in final aggregation step
This commit is contained in:
John Gallagher 2016-02-01 10:52:27 -05:00
commit e619f83819

View File

@ -489,9 +489,10 @@ impl InnerConnection {
where D: Aggregate<A, T>, where D: Aggregate<A, T>,
T: ToResult T: ToResult
{ {
unsafe fn aggregate_context<A>(ctx: *mut sqlite3_context) -> Option<*mut *mut A> { unsafe fn aggregate_context<A>(ctx: *mut sqlite3_context,
let pac = ffi::sqlite3_aggregate_context(ctx, ::std::mem::size_of::<*mut A>() as c_int) bytes: usize)
as *mut *mut A; -> Option<*mut *mut A> {
let pac = ffi::sqlite3_aggregate_context(ctx, bytes as c_int) as *mut *mut A;
if pac.is_null() { if pac.is_null() {
return None; return None;
} }
@ -525,7 +526,7 @@ impl InnerConnection {
assert!(!boxed_aggr.is_null(), assert!(!boxed_aggr.is_null(),
"Internal error - null aggregate pointer"); "Internal error - null aggregate pointer");
let pac = match aggregate_context(ctx) { let pac = match aggregate_context(ctx, ::std::mem::size_of::<*mut A>()) {
Some(pac) => pac, Some(pac) => pac,
None => { None => {
ffi::sqlite3_result_error_nomem(ctx); ffi::sqlite3_result_error_nomem(ctx);
@ -556,19 +557,18 @@ impl InnerConnection {
assert!(!boxed_aggr.is_null(), assert!(!boxed_aggr.is_null(),
"Internal error - null aggregate pointer"); "Internal error - null aggregate pointer");
let pac = match aggregate_context(ctx) { // Within the xFinal callback, it is customary to set N=0 in calls to
Some(pac) => pac, // sqlite3_aggregate_context(C,N) so that no pointless memory allocations occur.
None => { let a: Option<A> = match aggregate_context(ctx, 0) {
ffi::sqlite3_result_error_nomem(ctx); Some(pac) => {
return; if (*pac).is_null() {
None
} else {
let a = Box::from_raw(*pac);
Some(*a)
}
} }
}; None => None,
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) {