Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Aleksandr Lyapunov <alyapunov@tarantool.org>,
	tarantool-patches@dev.tarantool.org, tsafin@tarantool.org
Subject: Re: [Tarantool-patches] [PATCH small 1/1] region: new region_alloc_array, updated alloc_object
Date: Wed, 27 May 2020 02:00:26 +0200	[thread overview]
Message-ID: <7c6d16bd-b943-63d6-97e6-b7ef2d9ee8c9@tarantool.org> (raw)
In-Reply-To: <2914883e-7c71-fd12-0512-5a2e92543efb@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.

  reply	other threads:[~2020-05-27  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-21 20:32 Vladislav Shpilevoy
2020-05-25  6:52 ` Aleksandr Lyapunov
2020-05-27  0:00   ` Vladislav Shpilevoy [this message]
2020-06-05 12:11     ` Timur Safin
2020-06-03 21:27 ` Vladislav Shpilevoy
2020-06-05 12:12   ` Timur Safin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c6d16bd-b943-63d6-97e6-b7ef2d9ee8c9@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=alyapunov@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=tsafin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH small 1/1] region: new region_alloc_array, updated alloc_object' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox