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 D3B126EC5F; Thu, 15 Apr 2021 04:25:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D3B126EC5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618449957; bh=TP2t1j1Ri+R4PK/AQltjkRditWic9fwWm2ecAJP/Z1U=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=weXP9n4VmbmVCaPkRAz0S6SM6VM0Q/1S/s7Jxl+glZTX55XkJgHYQxYiht88v6uwq Ehz1tcrXS22pRHUOxLily9/ft9wcDt3VrpMvlUdDscuxDVGRfox3vTanUrFOoyNz5m DJ4rlgf+FAptkRqCXWx7XZpQooW9mHFPPHvQU0nE= Received: from smtp17.mail.ru (smtp17.mail.ru [94.100.176.154]) (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 9824A6EC5F for ; Thu, 15 Apr 2021 04:25:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9824A6EC5F Received: by smtp17.mail.ru with esmtpa (envelope-from ) id 1lWqlX-0005hx-H2; Thu, 15 Apr 2021 04:25:55 +0300 Date: Thu, 15 Apr 2021 04:25:54 +0300 To: Vladislav Shpilevoy Message-ID: <20210415012554.GA416841@tarantool.org> References: <20210413213652.GA37176@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480D608FE24BC85426BB1B55F651FED8C70182A05F538085040C399F635A3CCE3C7B68C2A8BCC66D93EAF459D997E1B3728EB90F966D8FBBE67 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73B44982FA5E78411EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637968EC5F77C2942FE8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2B5807D4DAAE78C7741C8B543F5B52FAFCB629EEF1311BF91D2E47CDBA5A96583C09775C1D3CA48CF4964A708C60C975A117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE7E9512AC99003B6BE9FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE7D3CEA5FFD76A5FA1D32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0430B4546ECB77842CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3E0287E8A3902ECA435872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D25379311020FFC8D4AD48C8163CD7E2ABE75FF279D2DF359BBC X-C1DE0DAB: 0D63561A33F958A58ADC9FAC45D85BD7F2F4CE1A870DDF63F1BF1E729BEB3B10D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D348BE83DFD8AFB1CAC62A4C6FD6CCAD216CF95F309E24C98E75086AF8B29847BF6DE6154423B42A0021D7E09C32AA3244C91FEB0A7DEC840C3918BAF9BDAFEACD197FE24653F78E668FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojrcJA+pXcDulYXIrmHIXoQw== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDC73D2D5F5A6932EBB68C2A8BCC66D93E41B07CD4FDF173FB5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 27/52] sql: introduce mem_set_str_*() functions 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: Mergen Imeev Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review. My answer and two diffs below. First diff for this patch, second - for similar patch with binary. On Thu, Apr 15, 2021 at 01:49:46AM +0200, Vladislav Shpilevoy wrote: > Thanks for working on this! > > >>> index ae55e4c29..171cb8946 100644 > >>> --- a/src/box/sql/vdbeaux.c > >>> +++ b/src/box/sql/vdbeaux.c > >>> @@ -1333,41 +1331,34 @@ sqlVdbeList(Vdbe * p) > >>> mem_set_int(pMem, pOp->p3, pOp->p3 < 0); > >>> pMem++; > >>> > >>> - if (sqlVdbeMemClearAndResize(pMem, 256)) { > >>> - assert(p->db->mallocFailed); > >>> + char *buf = sqlDbMallocRaw(sql_get(), 256); > >> > >> 2. I think you need some kind of mem_set_strlen(), or mem_grow()/mem_reserve(), > >> or something else to reserve the memory. To extend zMalloc. Otherwise you > >> can't reuse the memory which might already be in the mem object. > > Wouldn't mem_copy_*() be enough? In general, allocated by MEM memory should not > > be accessed for changing (except for MEM_Agg). > > Both your current solution and mem_copy suffer from unnecessary copying. > In the old code the string was created in-place. This is what I am talking > about. > > But nevermind, it is not a perf-critical place as I see. Can be tweaked > later. > > > diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c > > index 6c08e772d..484c66b29 100644 > > --- a/src/box/sql/vdbeapi.c > > +++ b/src/box/sql/vdbeapi.c > > @@ -125,6 +125,12 @@ setResultStrOrError(sql_context * pCtx, /* Function context */ > > void (*xDel) (void *) /* Destructor function */ > > ) > > { > > + if (xDel == SQL_STATIC) > > + return mem_set_strl_static(pCtx->pOut, (char *)z, n); > > + if (xDel == SQL_DYNAMIC) > > + return mem_set_strl_allocated(pCtx->pOut, (char *)z, n); > > + if (xDel != SQL_TRANSIENT) > > + return mem_set_strl_dynamic(pCtx->pOut, (char *)z, n); > > I just can't help myself. It makes my eyes bleed - for dynamic we use > allocated, and for non-transient we use dynamic! I see why, but I would > want us to hide it as deep inside of mem.c as possible, so as we at > least could change the names in a not very intrusive manner in the > future. Or delete it in future entirely. > > Please, move this hunk and the one below to a separate functions > like mem_set_strl(Mem, str, len_hint, destructor). Then it would be in > one terrible place instead of 2 terrible places. > > The same for the bins. > Done. > > if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0) > > pCtx->is_aborted = true; > > } > > @@ -762,7 +768,13 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */ > > if (zData == NULL) > > return 0; > > pVar = &p->aVar[i - 1]; > > - if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) > > + if (xDel == SQL_STATIC) > > + mem_set_strl_static(pVar, (char *)zData, nData); > > + else if (xDel == SQL_DYNAMIC) > > + mem_set_strl_allocated(pVar, (char *)zData, nData); > > + else if (xDel != SQL_TRANSIENT) > > + mem_set_strl_dynamic(pVar, (char *)zData, nData); > > + else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) > > return -1; > > return sql_bind_type(p, i, "text"); > > } Diff for str: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index f029ee0e5..258bb11e6 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -409,6 +409,18 @@ mem_set_strl_allocated(struct Mem *mem, char *value, int len_hint) mem_set_str_allocated(mem, value, len_hint); } +static inline void +mem_set_strl(struct Mem *mem, char *value, int len_hint, + void (*custom_free)(void *)) +{ + if (custom_free == SQL_STATIC) + return mem_set_strl_static(mem, value, len_hint); + if (custom_free == SQL_DYNAMIC) + return mem_set_strl_allocated(mem, value, len_hint); + if (custom_free != SQL_TRANSIENT) + return mem_set_strl_dynamic(mem, value, len_hint); +} + /** * Copy content of MEM from one MEM to another. In case source MEM contains * string or binary and allocation type is not STATIC, this value is copied to diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 3cd60864f..ae65a53d8 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -125,12 +125,8 @@ setResultStrOrError(sql_context * pCtx, /* Function context */ void (*xDel) (void *) /* Destructor function */ ) { - if (xDel == SQL_STATIC) - return mem_set_strl_static(pCtx->pOut, (char *)z, n); - if (xDel == SQL_DYNAMIC) - return mem_set_strl_allocated(pCtx->pOut, (char *)z, n); if (xDel != SQL_TRANSIENT) - return mem_set_strl_dynamic(pCtx->pOut, (char *)z, n); + return mem_set_strl(pCtx->pOut, (char *)z, n, xDel); if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0) pCtx->is_aborted = true; } @@ -768,12 +764,8 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */ if (zData == NULL) return 0; pVar = &p->aVar[i - 1]; - if (xDel == SQL_STATIC) - mem_set_strl_static(pVar, (char *)zData, nData); - else if (xDel == SQL_DYNAMIC) - mem_set_strl_allocated(pVar, (char *)zData, nData); - else if (xDel != SQL_TRANSIENT) - mem_set_strl_dynamic(pVar, (char *)zData, nData); + if (xDel != SQL_TRANSIENT) + mem_set_strl(pVar, (char *)zData, nData, xDel); else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) return -1; return sql_bind_type(p, i, "text"); Diff for bin: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index f728ac58a..5b9ac6a96 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -468,6 +468,18 @@ mem_set_bin_dynamic(struct Mem *mem, char *value, uint32_t size); void mem_set_bin_allocated(struct Mem *mem, char *value, uint32_t size); +static inline void +mem_set_binl(struct Mem *mem, char *value, uint32_t size, + void (*custom_free)(void *)) +{ + if (custom_free == SQL_STATIC) + return mem_set_bin_static(mem, value, size); + if (custom_free == SQL_DYNAMIC) + return mem_set_bin_allocated(mem, value, size); + if (custom_free != SQL_TRANSIENT) + return mem_set_bin_dynamic(mem, value, size); +} + /** * Copy content of MEM from one MEM to another. In case source MEM contains * string or binary and allocation type is not STATIC, this value is copied to diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 1ba2f4260..66908e2ec 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -158,12 +158,8 @@ sql_result_blob(sql_context * pCtx, ) { assert(n >= 0); - if (xDel == SQL_STATIC) - mem_set_bin_static(pCtx->pOut, (char *)z, n); - else if (xDel == SQL_DYNAMIC) - mem_set_bin_allocated(pCtx->pOut, (char *)z, n); - else if (xDel != SQL_TRANSIENT) - mem_set_bin_dynamic(pCtx->pOut, (char *)z, n); + if (xDel != SQL_TRANSIENT) + mem_set_binl(pCtx->pOut, (char *)z, n, xDel); else if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0) pCtx->is_aborted = true; } @@ -793,12 +789,8 @@ sql_bind_blob(sql_stmt * pStmt, if (zData == NULL) return 0; struct Mem *var = &p->aVar[i - 1]; - if (xDel == SQL_STATIC) - mem_set_bin_static(var, (char *)zData, nData); - else if (xDel == SQL_DYNAMIC) - mem_set_bin_allocated(var, (char *)zData, nData); - else if (xDel != SQL_TRANSIENT) - mem_set_bin_dynamic(var, (char *)zData, nData); + if (xDel != SQL_TRANSIENT) + mem_set_binl(var, (char *)zData, nData, xDel); else if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0) return -1; return sql_bind_type(p, i, "varbinary");