All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
To: Tommaso Merciai <tomm.merciai@gmail.com>
Cc: jacopo.mondi@ideasonboard.com, laurent.pinchart@ideasonboard.com,
	martin.hecht@avnet.eu, linuxfancy@googlegroups.com,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Hans Verkuil <hverkuil@xs4all.nl>,
	Marco Felsch <m.felsch@pengutronix.de>,
	Gerald Loacker <gerald.loacker@wolfvision.net>,
	Nicholas Roth <nicholas@rothemail.net>,
	Shawn Tu <shawnx.tu@intel.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Benjamin Mugnier <benjamin.mugnier@foss.st.com>,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org
Subject: Re: [PATCH v2 2/2] media: i2c: Add support for alvium camera
Date: Mon, 29 May 2023 14:34:09 +0200	[thread overview]
Message-ID: <7aaa6abc-09a7-7be5-9184-6cd7d702baf2@wanadoo.fr> (raw)
In-Reply-To: <ZHR5qup6412nLgzz@tom-HP-ZBook-Fury-15-G7-Mobile-Workstation>

Le 29/05/2023 à 12:08, Tommaso Merciai a écrit :

>>> +	int avail_fmt_cnt = 0;
>>> +
>>> +	alvium->alvium_csi2_fmt = NULL;
>>> +
>>> +	/* calculate fmt array size */
>>> +	for (fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
>>> +		if (alvium->is_mipi_fmt_avail[alvium_csi2_fmts[fmt].fmt_av_bit]) {
>>> +			if (!alvium_csi2_fmts[fmt].is_raw) {
>>> +				sz++;
>>> +			} else if (alvium_csi2_fmts[fmt].is_raw &&
>>> +			      alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit]) {
>>
>> It is makes sense, this if/else looks equivalent to:
>>
>> 			if (!alvium_csi2_fmts[fmt].is_raw ||
>> 			    alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit]) {
>> 				sz++;
>>
>> The same simplification could also be applied in the for loop below.
> 
> I Don't agree on this.
> This is not a semplification.
> This change the logic of the statement.
> 

Hmmm, unless I missed something obvious, I don't think it changes the logic.

Let me detail my PoV.

The original code is, for the inner if:

+	if (!alvium_csi2_fmts[fmt].is_raw) {
+		sz++;
+	} else if (alvium_csi2_fmts[fmt].is_raw &&
+	      alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit]) {
+		sz++;
+	}

So both branchs end to sz++, so it can be written:

+	if ((!alvium_csi2_fmts[fmt].is_raw) ||
+	    (alvium_csi2_fmts[fmt].is_raw &&
+	      alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit])) {
+		sz++;
+	}

A || (!A && B) is equivalent to A || B, so it can be rewritten as:

+	if ((!alvium_csi2_fmts[fmt].is_raw) ||
+	    (alvium->is_bay_avail[alvium_csi2_fmts[fmt].bay_av_bit])) {
+		sz++;
+	}

> Also initialization of sz and avail_fmt_cnt is needed.
> Maybe I can include fmt variable initialization into the for loop:

Agreed. I must have been unclear.
I was only speaking about the *inner* 'if', not the whole block of code.

> 
> for (int fmt = 0; fmt < ALVIUM_NUM_SUPP_MIPI_DATA_FMT; fmt++) {
> 
> But from my perspective this seems clear.

I fully agree with you, but that was not my point.


>>> +struct alvium_pixfmt {
>>> +	u8 id;
>>> +	u32 code;
>>> +	u32 colorspace;
>>> +	u8 fmt_av_bit;
>>> +	u8 bay_av_bit;
>>> +	u64 mipi_fmt_regval;
>>> +	u64 bay_fmt_regval;
>>> +	u8 is_raw;
>>
>> If moved a few lines above, there would be less holes in the struct.
> 
> ?

This is more or less the same comment that you got from Laurent Pinchart.

Using pahole on your struct, as-is, gives:

struct alvium_pixfmt {
	u8                         id;                   /*     0     1 */

	/* XXX 3 bytes hole, try to pack */

	u32                        code;                 /*     4     4 */
	u32                        colorspace;           /*     8     4 */
	u8                         fmt_av_bit;           /*    12     1 */
	u8                         bay_av_bit;           /*    13     1 */

	/* XXX 2 bytes hole, try to pack */

	u64                        mipi_fmt_regval;      /*    16     8 */
	u64                        bay_fmt_regval;       /*    24     8 */
	u8                         is_raw;               /*    32     1 */

	/* size: 40, cachelines: 1, members: 8 */
	/* sum members: 28, holes: 2, sum holes: 5 */
	/* padding: 7 */
	/* last cacheline: 40 bytes */
};

If you change the order of some fields, you can get, *for example*:

struct alvium_pixfmt {
	u8                         id;                   /*     0     1 */
	u8                         is_raw;               /*     1     1 */
	u8                         fmt_av_bit;           /*     2     1 */
	u8                         bay_av_bit;           /*     3     1 */
	u32                        code;                 /*     4     4 */
	u32                        colorspace;           /*     8     4 */

	/* XXX 4 bytes hole, try to pack */

	u64                        mipi_fmt_regval;      /*    16     8 */
	u64                        bay_fmt_regval;       /*    24     8 */

	/* size: 32, cachelines: 1, members: 8 */
	/* sum members: 28, holes: 1, sum holes: 4 */
	/* last cacheline: 32 bytes */
};

Now the whole structure require 32 bytes instead of 40, because their 
are less holes in it.
And "rounding" in the memory allocation layer would finally allocate 64 
bytes. So moving some fields would half (32 vs 64) the memory used!

But, the main point is to keep a layout that is logical to you. So up to 
you to re-arrange the struct or not.

(the same applies to some other struct in your patch)

CJ

  reply	other threads:[~2023-05-29 12:34 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-26 17:39 [PATCH v2 0/2] media: i2c: Add support for alvium camera Tommaso Merciai
2023-05-26 17:39 ` [PATCH v2 1/2] media: dt-bindings: alvium: add document YAML binding Tommaso Merciai
2023-05-26 19:00   ` Conor Dooley
2023-05-29  7:22     ` Tommaso Merciai
2023-05-28 21:16   ` Sakari Ailus
2023-05-29  6:39     ` Laurent Pinchart
2023-05-29  6:43       ` Laurent Pinchart
2023-05-31 10:20         ` Tommaso Merciai
2023-05-31 11:06           ` Laurent Pinchart
2023-05-31 14:01             ` Tommaso Merciai
2023-05-31 14:36               ` Laurent Pinchart
2023-05-29  7:57       ` Tommaso Merciai
2023-05-29  8:07         ` Laurent Pinchart
2023-05-29  7:41     ` Tommaso Merciai
2023-05-29  7:51       ` Laurent Pinchart
2023-05-30 15:53   ` Krzysztof Kozlowski
2023-05-26 17:39 ` [PATCH v2 2/2] media: i2c: Add support for alvium camera Tommaso Merciai
2023-05-26 18:39   ` Christophe JAILLET
2023-05-29 10:08     ` Tommaso Merciai
2023-05-29 12:34       ` Christophe JAILLET [this message]
2023-05-29 13:26         ` Tommaso Merciai
2023-05-29  7:40   ` Laurent Pinchart
2023-05-31 10:13     ` Tommaso Merciai
2023-05-31 11:33       ` Laurent Pinchart
2023-05-31 14:19         ` Tommaso Merciai
2023-05-31 14:42           ` Laurent Pinchart
2023-05-31 15:12             ` Tommaso Merciai
2023-06-01 17:05         ` Tommaso Merciai
2023-06-02  4:31           ` Laurent Pinchart
2023-06-13 12:00             ` Sakari Ailus
2023-06-13 13:24               ` Tommaso Merciai
2023-05-30 20:56   ` kernel test robot

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=7aaa6abc-09a7-7be5-9184-6cd7d702baf2@wanadoo.fr \
    --to=christophe.jaillet@wanadoo.fr \
    --cc=benjamin.mugnier@foss.st.com \
    --cc=gerald.loacker@wolfvision.net \
    --cc=hverkuil@xs4all.nl \
    --cc=jacopo.mondi@ideasonboard.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linuxfancy@googlegroups.com \
    --cc=m.felsch@pengutronix.de \
    --cc=martin.hecht@avnet.eu \
    --cc=mchehab@kernel.org \
    --cc=nicholas@rothemail.net \
    --cc=sakari.ailus@linux.intel.com \
    --cc=shawnx.tu@intel.com \
    --cc=tomm.merciai@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.