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 D094B6DB02; Tue, 5 Oct 2021 12:32:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D094B6DB02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633426373; bh=N+bBXVVexF8wF/oIM4rJ7pQwa3yzhpM+tBmyxSOCs9Q=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=wmG9FIfokflPcBZimyTG7BYSz7y30kas6blipCI+bi1nBTnHN1WLvKtZcabaecCpT aGs1PwDbv0qfiCTLDxUl0+i8yuSbi881q8QcZP59DNjOEMuLSAR+q5ajXYiBfgrGCM 4dU/jx2RjcHihMVx99W9uLYTvvq3FC4t6iB0f7IE= 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 4F8DA6DB02 for ; Tue, 5 Oct 2021 12:32:52 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4F8DA6DB02 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mXgod-0005pz-HL; Tue, 05 Oct 2021 12:32:52 +0300 Date: Tue, 5 Oct 2021 12:32:50 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20211005093250.GC55311@tarantool.org> References: <7447231b-f112-5228-ebaa-40a93f62aefa@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <7447231b-f112-5228-ebaa-40a93f62aefa@tarantool.org> X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9064ADF4728AA0EE950704E1C1E1FED60C49BF516B31B232E182A05F538085040EBA9E648AD720706F166228BB2AD511057F8DEC8F477D1D3DD374BF17586024D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75AD53DF1D86BACA3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378629C75C8EFA8C148638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86F6F2B3516529480A8E91EC11AC706F4117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD182CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5CD39F8E84CAD5D86CF778B4A9CBB83C74F3D3C39C08F7E77D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75C4D20244F7083972410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3475FE4AA98865E2354F013D91B150B4AC82ADF8DF8168509172ED054750A0992CF5A2502474749EE21D7E09C32AA3244C4BA8F68DE49218D8AB4745AD7C2E5B1624AF4FAF06DA24FD729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojhAh8SZXECpBmYjbM9iyIjw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D46AEB9340CCE7FD17E25DE6CE0752D8F83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v4 06/16] sql: introduce mem_append() 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 Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answer, diff and new patch below. On Mon, Oct 04, 2021 at 11:52:43PM +0200, Vladislav Shpilevoy wrote: > Thanks for working on this! > > > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > > index 079083fa1..b8ead8f41 100644 > > --- a/src/box/sql/mem.c > > +++ b/src/box/sql/mem.c > > @@ -1925,6 +1925,27 @@ mem_move(struct Mem *to, struct Mem *from) > > from->zMalloc = NULL; > > } > > > > +int > > +mem_append(struct Mem *mem, const char *value, uint32_t len) > > +{ > > + assert((mem->type & (MEM_TYPE_BIN | MEM_TYPE_STR)) != 0); > > + if (len == 0) > > + return 0; > > + int new_size = mem->n + len; > > + if (((mem->flags & (MEM_Static | MEM_Dyn | MEM_Ephem)) != 0) || > > + mem->szMalloc < new_size) { > > + /* > > + * Force exponential buffer size growth to avoid having to call > > + * this routine too often. > > + */ > > + if (sqlVdbeMemGrow(mem, new_size + mem->n, 1) != 0) > > Looks like you could call sqlVdbeMemGrow() without all these checks > above. The grow function does them already. > True, fixed. There is a case where the behavior will be different - I mean the case where new_size < szMalloc and new_size + n > szMalloc, but that doesn't seem to be a very common case. Sorry, I wrote this comment in the wrong place first (in the first patch). > > + return -1; > > + } > > + memcpy(&mem->z[mem->n], value, len); > > + mem->n = new_size; > > + return 0; > > +} Diff: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index de4c41ccf..343a730b3 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1934,15 +1934,12 @@ mem_append(struct Mem *mem, const char *value, uint32_t len) if (len == 0) return 0; int new_size = mem->n + len; - if (((mem->flags & (MEM_Static | MEM_Dyn | MEM_Ephem)) != 0) || - mem->szMalloc < new_size) { - /* - * Force exponential buffer size growth to avoid having to call - * this routine too often. - */ - if (sqlVdbeMemGrow(mem, new_size + mem->n, 1) != 0) - return -1; - } + /* + * Force exponential buffer size growth to avoid having to call this + * routine too often. + */ + if (sqlVdbeMemGrow(mem, new_size + mem->n, 1) != 0) + return -1; memcpy(&mem->z[mem->n], value, len); mem->n = new_size; return 0; New patch: commit ea59a75e3ebbe7138fab0bb73d25a46367698e74 Author: Mergen Imeev Date: Mon Sep 6 13:49:47 2021 +0300 sql: introduce mem_append() This patch introduces the mem_append() function. This function appends the specified string to the end of the STRING or VARBINARY contained in MEM. In case MEM needs to increase the size of allocated memory, extra memory is allocated in an attempt to reduce the total number of allocations. Needed for #4145 diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index f1b61dec6..343a730b3 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1927,6 +1927,24 @@ mem_move(struct Mem *to, struct Mem *from) from->zMalloc = NULL; } +int +mem_append(struct Mem *mem, const char *value, uint32_t len) +{ + assert((mem->type & (MEM_TYPE_BIN | MEM_TYPE_STR)) != 0); + if (len == 0) + return 0; + int new_size = mem->n + len; + /* + * Force exponential buffer size growth to avoid having to call this + * routine too often. + */ + if (sqlVdbeMemGrow(mem, new_size + mem->n, 1) != 0) + return -1; + memcpy(&mem->z[mem->n], value, len); + mem->n = new_size; + return 0; +} + int mem_concat(const struct Mem *a, const struct Mem *b, struct Mem *result) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 1574da22d..b567aea50 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -582,6 +582,14 @@ mem_copy_as_ephemeral(struct Mem *to, const struct Mem *from); void mem_move(struct Mem *to, struct Mem *from); +/** + * Append the given string to the end of the STRING or VARBINARY contained in + * MEM. In case MEM needs to increase the size of allocated memory, additional + * memory is allocated in an attempt to reduce the total number of allocations. + */ +int +mem_append(struct Mem *mem, const char *value, uint32_t len); + /** * Concatenate strings or binaries from the first and the second MEMs and write * to the result MEM. In case the first MEM or the second MEM is NULL, the diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index 4c2bd11ba..3015760e1 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -1294,14 +1294,9 @@ sqlVdbeList(Vdbe * p) if (mem_copy_bin(pSub, bin, size) != 0) return -1; } else if (j == nSub) { - struct Mem tmp; - mem_create(&tmp); - uint32_t size = sizeof(SubProgram *); char *bin = (char *)&pOp->p4.pProgram; - mem_set_bin_ephemeral(&tmp, bin, size); - int rc = mem_concat(pSub, &tmp, pSub); - mem_destroy(&tmp); - if (rc != 0) + uint32_t size = sizeof(SubProgram *); + if (mem_append(pSub, bin, size) != 0) return -1; } }