From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp14.mail.ru (smtp14.mail.ru [94.100.181.95]) (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 09EE4469710 for ; Wed, 27 May 2020 03:00:28 +0300 (MSK) References: <2914883e-7c71-fd12-0512-5a2e92543efb@tarantool.org> From: Vladislav Shpilevoy Message-ID: <7c6d16bd-b943-63d6-97e6-b7ef2d9ee8c9@tarantool.org> Date: Wed, 27 May 2020 02:00:26 +0200 MIME-Version: 1.0 In-Reply-To: <2914883e-7c71-fd12-0512-5a2e92543efb@tarantool.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [Tarantool-patches] [PATCH small 1/1] region: new region_alloc_array, updated alloc_object List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Aleksandr Lyapunov , tarantool-patches@dev.tarantool.org, tsafin@tarantool.org Hi! Thanks for the review! > On 5/21/20 11:32 PM, Vladislav Shpilevoy wrote: >> Also the patch adds an out parameter 'size' for both macros. It >> simplifies total size calculation, which is needed almost always, >> because total size is included into an error message, if the >> allocation fails. > I don't like the size returning. Even for array allocation it looks annoying. > It's too easy to calculate the size, and compilers will omit the second > multiplication with the same args. > For single allocation it looks ugly. It's not even a calculation. This is not a matter of how easy it is to calculate a size. This is a matter of code duplication. Type and count are needed in 2 places: in region_alloc_array() and then in diag_set(). This is much more ugly, and occupies significantly more space, when you need to write them both twice. Compare: struct key_def **keys = region_alloc_array(&fiber()->gc, typeof(keys[0]), key_count); if (keys == NULL) { diag_set(OutOfMemory, sizeof(keys[0]) * key_count, "region_alloc_array", "keys"); return NULL; } And size_t bsize; struct key_def **keys = region_alloc_array(&fiber()->gc, typeof(keys[0]), key_count, &bsize); if (keys == NULL) { diag_set(OutOfMemory, bsize, "region_alloc_array", "keys"); return NULL; } In the first option 'keys[0]' and 'count' need to be written 2 times. This is getting worse, when variable name is like 'region_links', or count is 'def->field_count'. Worse again it becomes, when size is needed not only in diag, but also in case of success. For example, to make a memset(0). In this case it is either triple code duplication, or double code duplication but with size_t size/bsize variable anyway. region_alloc_object() got the out parameter to be consistent with region_alloc_array(). I tried removing the out parameter, and it started looking worse.