From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 913A2384EF70 for ; Fri, 16 Dec 2022 18:22:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 913A2384EF70 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671214944; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=A92oGGF1+dufAWR+xT/iUIYsv44xsXt1UnzFMcuNFBc=; b=Vdi2H7ITko4teRNC/WzwxIHCSybxgSLhsM2EoRd0dDBBCQ8u/Pf249vJ09sWf0Zc3Jc9PA yqgSrrLTWeO1IaMCY0vD9DItYDJmfS34pASrJxmKYUESpP9hrh1WISfW4eOSvpL/AIvUIZ MitJ9n/5AzXeYpoxvYznT17VqsHgEe0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-616-p11O5LVPNkeMosABuBLvKA-1; Fri, 16 Dec 2022 13:22:23 -0500 X-MC-Unique: p11O5LVPNkeMosABuBLvKA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A572480D0E5; Fri, 16 Dec 2022 18:22:22 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.61]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F3D040C2064; Fri, 16 Dec 2022 18:22:21 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella Netto Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v5 08/11] libio: Convert __vasprintf_internal to buffers References: <214908c0def333495c359630d7822e7ccb6b8154.1670858473.git.fweimer@redhat.com> <08779a8a-1dcb-495c-6d1c-5c629141a606@linaro.org> Date: Fri, 16 Dec 2022 19:22:19 +0100 In-Reply-To: <08779a8a-1dcb-495c-6d1c-5c629141a606@linaro.org> (Adhemerval Zanella Netto's message of "Thu, 15 Dec 2022 15:40:47 -0300") Message-ID: <87tu1vryxw.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * Adhemerval Zanella Netto: > On 12/12/22 12:23, Florian Weimer via Libc-alpha wrote: >> The buffer resizing algorithm is slightly different. The initial >> buffer is on the stack, and small buffers are directly allocated >> on the heap using the exact required size. The overhead of the >> additional copy is compensated by the lowered setup cost for buffers >> compared to libio streams. > > Patch look good, some comments below. Thanks. >> +struct __printf_buffer_asprintf >> +{ >> + /* base.write_base points either to a heap-allocated buffer, or to >> + the direct array below. */ >> + struct __printf_buffer base; >> + >> + /* Initial allocation. 200 should be large enough to copy almost >> + all asprintf usages with just a single (final, correctly sized) >> + heap allocation. */ >> + char direct[200]; >> +}; > > There are couple or more buffer sizes scattered in this patchset, maybe it > would be better to consolidate them in one place so it can be easily tuned. I see what I can do. > For instance, since it is used with memcpy below, maybe it would be better > to use a size that compiler can inline (it would be arch-dependent, but > using a size that most usual architecture inline is a net gain). It would need additional optimization hints for GCC, I believe. >> +void >> +__printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) >> +{ >> + size_t current_pos = buf->base.write_ptr - buf->base.write_base; >> + if (current_pos >= INT_MAX) > > Shouldn't it be SSIZE_MAX? Nope, asprintf has the same incorrect return type as printf. 8-( > >> + { >> + /* The result is not representable. No need to continue. */ >> + __set_errno (EOVERFLOW); >> + __printf_buffer_mark_failed (&buf->base); >> + return; >> + } >> + >> + size_t current_size = buf->base.write_end - buf->base.write_base; >> + /* Implement an exponentiatial sizing policy. Keep the size > > s/exponentiatial/exponential Fixed. >> + congruent 8 (mod 16), to account for the footer in glibc >> + malloc. */ >> + size_t new_size = ((current_size + current_size / 2) & -15) | 8; > > Maybe use ALIGN_UP ((current_size + current_size / 2), 16) here? Changed. Thanks, Florian