[{"id":33961,"web_url":"https://patchwork.libcamera.org/comment/33961/","msgid":"<174493516973.891349.13192449575746401771@ping.linuxembedded.co.uk>","date":"2025-04-18T00:12:49","subject":"Re: [PATCH v1] apps: cam: capture_script: Simplify bool array\n\tparsing","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Barnabás Pőcze (2025-04-17 12:33:42)\n> `std::vector<bool>` is a specialization that implements a dynamic\n> bit vector, therefore it is not suitable to provide storage for\n> an array of `bool`. Hence a statically sized array is used when\n> parsing an array of boolean values.\n> \n> Instead, use the array overload of `std::make_unique` since the\n> size is known beforehand.\n> \n\nSeems like a nice cleanup.\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  src/apps/cam/capture_script.cpp | 36 +++++++--------------------------\n>  1 file changed, 7 insertions(+), 29 deletions(-)\n> \n> diff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp\n> index 7d079721a..8ecd89f28 100644\n> --- a/src/apps/cam/capture_script.cpp\n> +++ b/src/apps/cam/capture_script.cpp\n> @@ -8,6 +8,7 @@\n>  #include \"capture_script.h\"\n>  \n>  #include <iostream>\n> +#include <memory>\n>  #include <stdio.h>\n>  #include <stdlib.h>\n>  \n> @@ -521,45 +522,22 @@ ControlValue CaptureScript::parseArrayControl(const ControlId *id,\n>         case ControlTypeNone:\n>                 break;\n>         case ControlTypeBool: {\n> -               /*\n> -                * This is unpleasant, but we cannot use an std::vector<> as its\n> -                * boolean type overload does not allow to access the raw data,\n> -                * as boolean values are stored in a bitmask for efficiency.\n> -                *\n> -                * As we need a contiguous memory region to wrap in a Span<>,\n> -                * use an array instead but be strict about not overflowing it\n> -                * by limiting the number of controls we can store.\n> -                *\n> -                * Be loud but do not fail, as the issue would present at\n> -                * runtime and it's not fatal.\n> -                */\n> -               static constexpr unsigned int kMaxNumBooleanControls = 1024;\n> -               std::array<bool, kMaxNumBooleanControls> values;\n> -               unsigned int idx = 0;\n> +               auto values = std::make_unique<bool[]>(repr.size());\n>  \n> -               for (const std::string &s : repr) {\n> -                       bool val;\n> +               for (std::size_t i = 0; i < repr.size(); i++) {\n> +                       const auto &s = repr[i];\n>  \n>                         if (s == \"true\") {\n> -                               val = true;\n> +                               values[i] = true;\n>                         } else if (s == \"false\") {\n> -                               val = false;\n> +                               values[i] = false;\n>                         } else {\n>                                 unpackFailure(id, s);\n>                                 return value;\n>                         }\n> -\n> -                       if (idx == kMaxNumBooleanControls) {\n> -                               std::cerr << \"Cannot parse more than \"\n> -                                         << kMaxNumBooleanControls\n> -                                         << \" boolean controls\" << std::endl;\n> -                               break;\n> -                       }\n> -\n> -                       values[idx++] = val;\n>                 }\n>  \n> -               value = Span<bool>(values.data(), idx);\n> +               value = Span<bool>(values.get(), repr.size());\n>                 break;\n>         }\n>         case ControlTypeByte: {\n> -- \n> 2.49.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 98A76C327D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Apr 2025 00:12:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DAFC268AC1;\n\tFri, 18 Apr 2025 02:12:54 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 10223617E6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Apr 2025 02:12:53 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DC02BF6;\n\tFri, 18 Apr 2025 02:10:48 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"qZvSkcsB\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1744935048;\n\tbh=xWxcl9qWYfDujDj4/mk1ifpKXNMAwx9oeAyNsPkaKXk=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=qZvSkcsBo9v1i4jKDqkA1o3Z+hQ0Z0FL1Ob2IfoR+TSZQBgEyO17Yyv4dvSAAxTNY\n\tRwnOZMwrL0ct5lSTGtjgMCqUAfPxXqJBwJjsqHdiCtNS8BSRZYbzzy5IxaQ/jtC5Pu\n\tuEc3noKYoBAM4artj+hcpW0cCMLmEnWnrRJg/0Yw=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250417113342.1137697-1-barnabas.pocze@ideasonboard.com>","References":"<20250417113342.1137697-1-barnabas.pocze@ideasonboard.com>","Subject":"Re: [PATCH v1] apps: cam: capture_script: Simplify bool array\n\tparsing","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 18 Apr 2025 01:12:49 +0100","Message-ID":"<174493516973.891349.13192449575746401771@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":33964,"web_url":"https://patchwork.libcamera.org/comment/33964/","msgid":"<y6zh72iwuevcffnp3op4e4fnvxtlk3l3w224gip6aje7u4qgp2@gveududlk5kg>","date":"2025-04-18T10:07:37","subject":"Re: [PATCH v1] apps: cam: capture_script: Simplify bool array\n\tparsing","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Barnabás\n\nOn Thu, Apr 17, 2025 at 01:33:42PM +0200, Barnabás Pőcze wrote:\n> `std::vector<bool>` is a specialization that implements a dynamic\n> bit vector, therefore it is not suitable to provide storage for\n> an array of `bool`. Hence a statically sized array is used when\n> parsing an array of boolean values.\n>\n> Instead, use the array overload of `std::make_unique` since the\n> size is known beforehand.\n>\n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  src/apps/cam/capture_script.cpp | 36 +++++++--------------------------\n>  1 file changed, 7 insertions(+), 29 deletions(-)\n>\n> diff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp\n> index 7d079721a..8ecd89f28 100644\n> --- a/src/apps/cam/capture_script.cpp\n> +++ b/src/apps/cam/capture_script.cpp\n> @@ -8,6 +8,7 @@\n>  #include \"capture_script.h\"\n>\n>  #include <iostream>\n> +#include <memory>\n>  #include <stdio.h>\n>  #include <stdlib.h>\n>\n> @@ -521,45 +522,22 @@ ControlValue CaptureScript::parseArrayControl(const ControlId *id,\n>  \tcase ControlTypeNone:\n>  \t\tbreak;\n>  \tcase ControlTypeBool: {\n> -\t\t/*\n> -\t\t * This is unpleasant, but we cannot use an std::vector<> as its\n> -\t\t * boolean type overload does not allow to access the raw data,\n> -\t\t * as boolean values are stored in a bitmask for efficiency.\n> -\t\t *\n> -\t\t * As we need a contiguous memory region to wrap in a Span<>,\n> -\t\t * use an array instead but be strict about not overflowing it\n> -\t\t * by limiting the number of controls we can store.\n> -\t\t *\n> -\t\t * Be loud but do not fail, as the issue would present at\n> -\t\t * runtime and it's not fatal.\n> -\t\t */\n> -\t\tstatic constexpr unsigned int kMaxNumBooleanControls = 1024;\n> -\t\tstd::array<bool, kMaxNumBooleanControls> values;\n> -\t\tunsigned int idx = 0;\n> +\t\tauto values = std::make_unique<bool[]>(repr.size());\n\nBrilliant, much better than fixed-size arrays\n\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n\n>\n> -\t\tfor (const std::string &s : repr) {\n> -\t\t\tbool val;\n> +\t\tfor (std::size_t i = 0; i < repr.size(); i++) {\n> +\t\t\tconst auto &s = repr[i];\n>\n>  \t\t\tif (s == \"true\") {\n> -\t\t\t\tval = true;\n> +\t\t\t\tvalues[i] = true;\n>  \t\t\t} else if (s == \"false\") {\n> -\t\t\t\tval = false;\n> +\t\t\t\tvalues[i] = false;\n>  \t\t\t} else {\n>  \t\t\t\tunpackFailure(id, s);\n>  \t\t\t\treturn value;\n>  \t\t\t}\n> -\n> -\t\t\tif (idx == kMaxNumBooleanControls) {\n> -\t\t\t\tstd::cerr << \"Cannot parse more than \"\n> -\t\t\t\t\t  << kMaxNumBooleanControls\n> -\t\t\t\t\t  << \" boolean controls\" << std::endl;\n> -\t\t\t\tbreak;\n> -\t\t\t}\n> -\n> -\t\t\tvalues[idx++] = val;\n>  \t\t}\n>\n> -\t\tvalue = Span<bool>(values.data(), idx);\n> +\t\tvalue = Span<bool>(values.get(), repr.size());\n>  \t\tbreak;\n>  \t}\n>  \tcase ControlTypeByte: {\n> --\n> 2.49.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id F1397C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Apr 2025 10:07:43 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1702868AC3;\n\tFri, 18 Apr 2025 12:07:43 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6F897617E7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Apr 2025 12:07:41 +0200 (CEST)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F161AF6;\n\tFri, 18 Apr 2025 12:05:36 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"kYLy5Dhd\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1744970737;\n\tbh=u02f1ekNxgrer70aU2GnoKNpriA9/UfGi44up42eywo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=kYLy5DhdLYQMrW2iUe/JKXe+PxIJmPaeQzY9exkkEwz11nBiwtlW26cpnJMJ0WTFu\n\tQ1sxfSFBI7RdZzR4TWqGPS6IGHDTlfswcpjhDVM4FHpgRUlgz7Jk2KXhwUAX8ohTzm\n\tDMn20i9dIO0LM/1OLVOXwkQMy4iLHU4kZjr9VpoQ=","Date":"Fri, 18 Apr 2025 12:07:37 +0200","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v1] apps: cam: capture_script: Simplify bool array\n\tparsing","Message-ID":"<y6zh72iwuevcffnp3op4e4fnvxtlk3l3w224gip6aje7u4qgp2@gveududlk5kg>","References":"<20250417113342.1137697-1-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250417113342.1137697-1-barnabas.pocze@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]