From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id DE31A6EC40; Thu, 19 Aug 2021 08:32:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DE31A6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629351162; bh=gBH68EEuXtZoA0SANFpkqLvnzXJEvtjmSCJtE0IiF0s=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=B1Bf17USUaJBw3+7NFGzZPhmEKsKcoS3EB1pLUZnyoUR6dtIRjdI78ucSyAY8LL1u e/32l+0HMoLU4sE2d7nO9obQ+/Kd7lVDZ2+x/JlpVVYCFwy6SlcEcGEcUfnJnJuirO PB42TjcUroLSxyfsZpImHgXQpOyDTKrWYsfOzTxA= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C8CD26EC44 for ; Thu, 19 Aug 2021 08:31:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C8CD26EC44 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mGaeG-0002KY-R5; Thu, 19 Aug 2021 08:31:29 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 19 Aug 2021 08:31:28 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD972FF4A7D76DB5E242D14FEF1BD8BF4AC182A05F5380850400BCC67820D6C7FADE425E173623AB0AF91846DDA61FEAD2C514210D0E0142D78 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AE0F15972A9DD0A8EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006372F19B29C465B909E8F08D7030A58E5AD1A62830130A00468AEEEE3FBA3A834EE7353EFBB55337566F160826E4E1956AEE8CDC2C9695C0B8B4BEFA603DDB0FAD3A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE767883B903EA3BAEA9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C33B72D73EA5828B68117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637AD0424077D726551EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CF160826E4E1956AEE8CDC2C9695C0B8B4BEFA603DDB0FAD39C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF5A3EDA775A1E0ED0699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34060C3C6DE316ECE47FBBBC2FEBE6AF509420D85A1D45D8FC6BAED9A889B6C8DB26395C3EC9C966F71D7E09C32AA3244C397F225AA43CD280D9018D428065F32205AB220A9D022EBC729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBFoEus1p+rFjg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D76D06AF39F8D4737BAC520BEC3B50C0283D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 2/5] sql: remove unnecessary MEM finalization X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" After removing the setting of the error in the patch "sql: modify arithmetic aggregate functions", we no longer need to finalize the MEM that has not been finalized. The logic is this: if the MEM has not been finalized, then something has happened, and we no longer need to finalize it. The MEM will be freed like a normal MEM with allocated memory. Part of #6105 --- src/box/sql/mem.c | 34 ++-------------------------------- src/box/sql/mem.h | 19 +------------------ src/box/sql/vdbe.c | 14 +++++++++++++- src/box/sql/vdbeapi.c | 10 ++-------- 4 files changed, 18 insertions(+), 59 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 74febd182..01d73968c 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -217,11 +217,7 @@ mem_create(struct Mem *mem) static inline void mem_clear(struct Mem *mem) { - if ((mem->type & (MEM_TYPE_AGG | MEM_TYPE_FRAME)) != 0 || - (mem->flags & MEM_Dyn) != 0) { - if (mem->type == MEM_TYPE_AGG) - sql_vdbemem_finalize(mem, mem->u.func); - assert(mem->type != MEM_TYPE_AGG); + if (mem->type == MEM_TYPE_FRAME || (mem->flags & MEM_Dyn) != 0) { if ((mem->flags & MEM_Dyn) != 0) { assert(mem->xDel != SQL_DYNAMIC && mem->xDel != NULL); mem->xDel((void *)mem->z); @@ -610,7 +606,7 @@ mem_set_frame(struct Mem *mem, struct VdbeFrame *frame) } int -mem_set_agg(struct Mem *mem, struct func *func, int size) +mem_set_agg(struct Mem *mem, int size) { mem_clear(mem); if (size <= 0) @@ -621,7 +617,6 @@ mem_set_agg(struct Mem *mem, struct func *func, int size) mem->n = size; mem->type = MEM_TYPE_AGG; assert(mem->flags == 0); - mem->u.func = func; return 0; } @@ -3045,31 +3040,6 @@ sqlVdbeMemTooBig(Mem * p) return 0; } -int -sql_vdbemem_finalize(struct Mem *mem, struct func *func) -{ - assert(func != NULL); - assert(func->def->language == FUNC_LANGUAGE_SQL_BUILTIN); - assert(func->def->aggregate == FUNC_AGGREGATE_GROUP); - assert(mem->type == MEM_TYPE_NULL || func == mem->u.func); - sql_context ctx; - memset(&ctx, 0, sizeof(ctx)); - Mem t; - memset(&t, 0, sizeof(t)); - t.type = MEM_TYPE_NULL; - assert(t.flags == 0); - t.db = mem->db; - ctx.pOut = &t; - ctx.pMem = mem; - ctx.func = func; - ((struct func_sql_builtin *)func)->finalize(&ctx); - assert((mem->flags & MEM_Dyn) == 0); - if (mem->szMalloc > 0) - sqlDbFree(mem->db, mem->zMalloc); - memcpy(mem, &t, sizeof(t)); - return ctx.is_aborted ? -1 : 0; -} - int sqlVdbeRecordCompareMsgpack(const void *key1, struct UnpackedRecord *key2) diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 543944b80..bddac1a67 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -70,11 +70,6 @@ struct Mem { bool b; /* Boolean value used when MEM_Bool is set in flags */ int nZero; /* Used when bit MEM_Zero is set in flags */ void *p; /* Generic pointer */ - /** - * A pointer to function implementation. - * Used only when flags==MEM_Agg. - */ - struct func *func; struct VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ struct tt_uuid uuid; decimal_t d; @@ -569,7 +564,7 @@ mem_set_frame(struct Mem *mem, struct VdbeFrame *frame); * hold the accumulation structure for the aggregate function. */ int -mem_set_agg(struct Mem *mem, struct func *func, int size); +mem_set_agg(struct Mem *mem, int size); /** Clear MEM and set it to special, "cleared", NULL. */ void @@ -961,18 +956,6 @@ int sqlVdbeMemTooBig(Mem *); #define VdbeMemDynamic(X) (((X)->flags & MEM_Dyn) != 0 ||\ ((X)->type & (MEM_TYPE_AGG | MEM_TYPE_FRAME)) != 0) -/** MEM manipulate functions. */ - -/** - * Memory cell mem contains the context of an aggregate function. - * This routine calls the finalize method for that function. The - * result of the aggregate is stored back into mem. - * - * Returns -1 if the finalizer reports an error. 0 otherwise. - */ -int -sql_vdbemem_finalize(struct Mem *mem, struct func *func); - /** * Perform comparison of two tuples: unpacked (key1) and packed (key2) * diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 98ea37c67..9fb103e82 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -4219,8 +4219,20 @@ case OP_AggFinal: { assert(pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor)); pMem = &aMem[pOp->p1]; assert(mem_is_null(pMem) || mem_is_agg(pMem)); - if (sql_vdbemem_finalize(pMem, pOp->p4.func) != 0) + struct sql_context ctx; + memset(&ctx, 0, sizeof(ctx)); + struct Mem t; + mem_create(&t); + ctx.pOut = &t; + ctx.pMem = pMem; + ((struct func_sql_builtin *)pOp->p4.func)->finalize(&ctx); + if (ctx.is_aborted) goto abort_due_to_error; + assert((pMem->flags & MEM_Dyn) == 0); + if (pMem->szMalloc > 0) + sqlDbFree(pMem->db, pMem->zMalloc); + memcpy(pMem, &t, sizeof(t)); + UPDATE_MAX_BLOBSIZE(pMem); if (sqlVdbeMemTooBig(pMem)) { goto too_big; diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 8031ee0dc..52d008c5f 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -383,10 +383,7 @@ sqlStmtCurrentTime(sql_context * p) void * sql_aggregate_context(sql_context * p, int nByte) { - assert(p != NULL && p->func != NULL); - assert(p->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN); - assert(p->func->def->aggregate == FUNC_AGGREGATE_GROUP); - if (!mem_is_agg(p->pMem) && mem_set_agg(p->pMem, p->func, nByte) != 0) + if (!mem_is_agg(p->pMem) && mem_set_agg(p->pMem, nByte) != 0) return NULL; void *accum; if (mem_get_agg(p->pMem, &accum) != 0) @@ -397,12 +394,9 @@ sql_aggregate_context(sql_context * p, int nByte) struct Mem * sql_context_agg_mem(struct sql_context *ctx) { - assert(ctx != NULL && ctx->func != NULL); - assert(ctx->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN); - assert(ctx->func->def->aggregate == FUNC_AGGREGATE_GROUP); struct Mem *mem; if (!mem_is_agg(ctx->pMem)) { - if (mem_set_agg(ctx->pMem, ctx->func, sizeof(*mem)) != 0) + if (mem_set_agg(ctx->pMem, sizeof(*mem)) != 0) return NULL; if (mem_get_agg(ctx->pMem, (void **)&mem) != 0) return NULL; -- 2.25.1