Received: from eggs.gnu.org ([2001:4830:134:3::10]:39416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c1xTo-0000j0-Dc for qemu-devel@nongnu.org; Wed, 02 Nov 2016 11:29:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c1xTl-0007Yx-8t for qemu-devel@nongnu.org; Wed, 02 Nov 2016 11:29:32 -0400 References: <20161012204907.25941-1-mreitz@redhat.com> <20161012204907.25941-3-mreitz@redhat.com> <7be11d16-d2ae-e892-5f97-17f971859a3a@linux.vnet.ibm.com> <20161102111524.GE6182@noname.redhat.com> From: Eric Blake Message-ID: Date: Wed, 2 Nov 2016 10:29:16 -0500 MIME-Version: 1.0 In-Reply-To: <20161102111524.GE6182@noname.redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="jLQNnbSS7CBk0EPwdCGV0LWkHwNXSCaVG" Subject: Re: [Qemu-devel] [PATCH 2/3] block: Emit modules in bdrv_iterate_format() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf , Hao QingFeng Cc: Stefan Hajnoczi , qemu-devel@nongnu.org, qemu-block@nongnu.org, Max Reitz This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --jLQNnbSS7CBk0EPwdCGV0LWkHwNXSCaVG From: Eric Blake To: Kevin Wolf , Hao QingFeng Cc: Stefan Hajnoczi , qemu-devel@nongnu.org, qemu-block@nongnu.org, Max Reitz Message-ID: Subject: Re: [Qemu-devel] [PATCH 2/3] block: Emit modules in bdrv_iterate_format() References: <20161012204907.25941-1-mreitz@redhat.com> <20161012204907.25941-3-mreitz@redhat.com> <7be11d16-d2ae-e892-5f97-17f971859a3a@linux.vnet.ibm.com> <20161102111524.GE6182@noname.redhat.com> In-Reply-To: <20161102111524.GE6182@noname.redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 11/02/2016 06:15 AM, Kevin Wolf wrote: > Am 02.11.2016 um 11:52 hat Hao QingFeng geschrieben: >> Sorry for a bit late response. The function looks fine but just some >> doubt on g_renew in this piece of code(and the legacy), does >> g_renew(realloc) has much performance impact if it's call many times >> since alloc and memory copy are both also run many times? So how >> about increase the memory for more instead of 1 each time? >> >> formats =3D g_renew(const char *, formats, count + 10); >> >> A new counter also needs to be introduced to record the space size. You are correct that the naive code has O(n^2) complexity, but so does your replacement. The only way to get to O(n) amortized complexity is to change count by a geometric scale (the simplest is doubling each time you have to realloc, but even something like increasing by 50% any time an increase is needed would also work). So if it is ever worth tracking allocation size to reduce reallocation costs, it is worth doing it right by using geometric rather than linear growth. >=20 > This code is not on a hot path, so keeping the code simple and easy to > maintain is preferrable to complicating it just so --help can save a fe= w > CPU cycles. But Kevin makes a good point - for small values of n, the difference between running a complex O(n) algorithm or a simpler naive O(n^2) algorithm can actually be faster for the naive algorithm; complexity alone is not necessarily a good measure of performance until you have large enough n that it becomes the dominating factor. And this certainly counts as code that is not executed frequently, nor where we expect to have so many distinct formats that n will ever grow large enough that the O(n^2) nature of the algorithm is noticeable to the humans reading the help output. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --jLQNnbSS7CBk0EPwdCGV0LWkHwNXSCaVG Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJYGgZMAAoJEKeha0olJ0Nqp4wH/3Lzx7R145whphJ370Bcbc+Y YQwHoCtWqBOm+uEouLab6p/qVHzi+vIY0gnpoLXHzjC5dsKw+CS5vCkZxlDFN+iq DfBFu5KKx9usiA2O3vT0dLhifg8tnIwqhJYxJyezP2FUJZxmB4aXD9hZFz5rtMF/ VPELtPAkBq0yonVzxigyjC2S06vxK5/xBmRSAvYtv8fitG6NAfnnK9GkZOmVvEGg YU+5coYTUY9crZUOXF/mSn72fhzN7feuYEpfJ6ACJMm7h31pU+9fDZSpZptt549K H+AuFFsw1+jF0WAzgGjgVF5pE3K6wCtO8jrbzI+7X3CwE5VAviy0g6d0/4Suzvg= =d7Jm -----END PGP SIGNATURE----- --jLQNnbSS7CBk0EPwdCGV0LWkHwNXSCaVG--