From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C0352727EE for ; Sat, 7 Feb 2026 23:36:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770507415; cv=none; b=hxhVOLspHAkNGafkh66oXoK3Vbx8GZK2k6NEKBpjoR9bqus6BBefEshAi1OgKR9OOHO3Eg0tWUcPkmuVOrex9Yr4UM6zhvQe+4KLTBi4DNgHng1vLz/EDuVRfAVA5/o/+otecVZ9Z/L2XHDlqdJPI+IqZgsTj8y4SdIGipPlqvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770507415; c=relaxed/simple; bh=O7spVkXSdSHCRdcRQnY1ryYdcqWw1S+6eiK5DPkDRzg=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cyP8QIIeug8iEdBjCwR1SnGBEQyOol6ZwPovrz+sow4W+35CxBZq71kgpBomGSCvs0zsGhx98YnGhQq63F1mBdLFUnJqppbHWnhKcTQqO7WdPskdRpix1FdHckcaOMwwjSLyzqtUnNLSjx96BmyWsPFpdJSf1/KpFhefcWK6Iko= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TAEWDiFH; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TAEWDiFH" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4376acce52eso21777f8f.1 for ; Sat, 07 Feb 2026 15:36:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770507413; x=1771112213; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=snoYKbaZJVQ1ORCxmz+5IRYvnNhDCxviuvrpApE+qT0=; b=TAEWDiFH1EYXRNlWYTbRNOgStwIpW9bEBHJ8oxmlukJOSt0ezWM5UE0mWq6DqPpJtX i78z6NpkNonl47TEC+oqh5Egqt62H0qn0G4jHG6zpnmv/pjFkpZKsBHjcjgNHyDGGV8Y J2/O+lS6FhRkZ+GnjONe70SlhEd7u3yBHJ7CASJyoYjkPMN7z1KBeXBLL7jnwnOAzTSC z50OYMKS146i8zY5cxDyXFxwfhUIaCzXsf7701ergeWsxGw7x5qHwdGdNftN20NQ4qLv CrAu4Fa1/qPusQupjajZjRJDQDxkjdawg8wJpD+jQoGnyfqtp/mOt3yCe0Ta0NKZeCBI rbQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770507413; x=1771112213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=snoYKbaZJVQ1ORCxmz+5IRYvnNhDCxviuvrpApE+qT0=; b=HSB2kfZ9Ji68zraEHaU1FGSegelcGxcQTjRbKVWXLwBcIEIGYFgtiupY/HZcSnKYwE G+btl346bZoMv6dHbqK7oJRWSNTuzr0tZ0XUcipiHXMPLWQ9NAKrZ2pwsmN0PeS/+/n0 IG0RjjhXR7RuuDYy8AAl34DxtwXlOcpvotU2H3hbX/K5wx1s+oux2Lsw0GCv/jRskhTk PgyMwmHsRhyxJd3NUi4mLaPSj1F6MO9GdLrQaE/aJcwWCnNhzG6Sm57DvKK6LYD9jrqb S50s2h4Fbtjg0I4CoUIETyubVAAbloZIXuJnMwnP7ijGkne5PBgslqY54V/11IuR7VPm kyvQ== X-Forwarded-Encrypted: i=1; AJvYcCUiXW3MQMKGwnIc7Vj9teiomBLCJdYhiemBmMCh+707lHL1Izm9KdPLoq2A8D1ESyrHWpozlYjqLJlTfQY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzoqd05uQIVJY7avRhhif42fpt7BvlKexB4L+t70QAtaia0cocl Se1dARUGXfQ2F8OvF9vgbt2+E3zdLnnnUwRlBFFbDr9uBOe22hudOIwF X-Gm-Gg: AZuq6aIx2PxYKEVMvcYiTwRzqLG8rkJ8/vQmhwL/BARfKxSoOT3yGtdP+frutv+wM3D Hy/bvObFN8PfzkULVqYNHoyqyc1nJiVi6WoA4rEOO4FHXgZo++npSECzHi2ePQWQQ8SEQYm9Rf+ laaDQV8vbMQyYr4bfsoRJ50/1o0lWRwDknkRgzpEUmmHbw3COOUvwX9VF82EtB8qYeF3XcLbVYe KCjW37DEUhivV/v6XTZNavXtbUXQkNMQperDz4ys+PRP6vMO6SFG7iiiK7iBVQpkDTuIqpe82bx 25KFAY5nTAnLKUFR7RnrWsUGzBLscv6Mrtg+f82g4yzw2IDzyTDHAeaWqWe1+MKib4T9bGmAHSW gKg/62FNNknEMBlulibn2r/oUi4oAH8oX2ltxKE7IRqOMTGPM4Cf4zsvnoxPbTJBgumQBsVXk/H 3zJ8waMpWzD/79fXeAKqbqFI8p3qxvelzRAbTdqaHuNAtrPS5x+/+k X-Received: by 2002:a05:6000:240c:b0:436:1a2e:6dc with SMTP id ffacd0b85a97d-4362904b844mr10280377f8f.10.1770507413257; Sat, 07 Feb 2026 15:36:53 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4376accfc58sm459375f8f.16.2026.02.07.15.36.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Feb 2026 15:36:52 -0800 (PST) Date: Sat, 7 Feb 2026 23:36:51 +0000 From: David Laight To: Willy Tarreau Cc: Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= , linux-kernel@vger.kernel.org, Cheng Li Subject: Re: [PATCH v2 next 03/11] tools/nolibc/printf: Add buffering to vfprintf() callback. Message-ID: <20260207233651.577f0fcb@pumpkin> In-Reply-To: References: <20260206191121.3602-1-david.laight.linux@gmail.com> <20260206191121.3602-4-david.laight.linux@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 7 Feb 2026 20:29:34 +0100 Willy Tarreau wrote: > On Fri, Feb 06, 2026 at 07:11:13PM +0000, david.laight.linux@gmail.com wrote: > > From: David Laight > > > > Add per-call buffering to the vprintf() callback. > > While this adds some extra code it will speed things up and > > makes a massive difference to anyone looking at strace output. > > This patch alone adds more than 200 extra bytes to the smallest binary > for something that was never expressed as a need by users: > > $ size hello-patch* > text data bss dec hex filename > 1859 48 24 1931 78b hello-patch1 > 2071 48 24 2143 85f hello-patch2 > > I doubt it would make sense to have a build option to choose this. > Or alternately one could decide do disable it when __OPTIMIZE_SIZE__ > is defined. I just tried quickly and it does the job: That probably makes sense. For anything non-trivial the extra size is noise. Actually is would be easier to read with a single #if covering the whole lot. > > @@ -390,6 +390,7 @@ struct __nolibc_fprintf_cb_state { > > static int __nolibc_fprintf_cb(void *v_state, const char *buf, size_t size) > { > +#if !defined(__OPTIMIZE_SIZE__) > struct __nolibc_fprintf_cb_state *state = v_state; > unsigned int off = state->buf_offset; > > @@ -407,16 +408,24 @@ static int __nolibc_fprintf_cb(void *v_state, const char *buf, size_t size) > memcpy(state->buf + off, buf, size); > } > return 0; > +#else > + /* v_state is the stream */ > + return size ? _fwrite(buf, size, v_state) : 0; > +#endif > } > > static __attribute__((unused, format(printf, 2, 0))) > int vfprintf(FILE *stream, const char *fmt, va_list args) > { > +#if !defined(__OPTIMIZE_SIZE__) > struct __nolibc_fprintf_cb_state state; > > state.stream = stream; > state.buf_offset = 0; > return __nolibc_printf(__nolibc_fprintf_cb, &state, fmt, args); > +#else > + return __nolibc_printf(__nolibc_fprintf_cb, stream, fmt, args); > +#endif > } > > static __attribute__((unused, format(printf, 1, 0))) > > > Signed-off-by: David Laight > > --- > > > > Changes for v2: > > Formally patch 2, unchanged. > > > > tools/include/nolibc/stdio.h | 32 +++++++++++++++++++++++++++++--- > > 1 file changed, 29 insertions(+), 3 deletions(-) > > > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > > index 36733ecd4261..552f09d51d82 100644 > > --- a/tools/include/nolibc/stdio.h > > +++ b/tools/include/nolibc/stdio.h > > @@ -382,15 +382,41 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list > > return written; > > } > > > > -static int __nolibc_fprintf_cb(void *stream, const char *buf, size_t size) > > +struct __nolibc_fprintf_cb_state { > > + FILE *stream; > > + unsigned int buf_offset; > > + char buf[128]; > > +}; > > So that's the other state I was wondering if we could merge with the first > one. The snprintf state is the 'address to write the next character to' and 'the amount of space remaining'. No real overlap at all. > > > +static int __nolibc_fprintf_cb(void *v_state, const char *buf, size_t size) > > { > > - return size ? _fwrite(buf, size, stream) : 0; > > + struct __nolibc_fprintf_cb_state *state = v_state; > > + unsigned int off = state->buf_offset; > > + > > + if (off + size > sizeof(state->buf) || buf == NULL) { > > Please mention that special case of buf==NULL in a comment above the > function. That's an internal API choice. ok - it might just go above this line though. David > > > + state->buf_offset = 0; > > + if (off && _fwrite(state->buf, off, state->stream)) > > + return -1; > > + if (size > sizeof(state->buf)) > > + return _fwrite(buf, size, state->stream); > > + off = 0; > > + } > > + > > + if (size) { > > + state->buf_offset = off + size; > > + memcpy(state->buf + off, buf, size); > > + } > > + return 0; > > } > > > > static __attribute__((unused, format(printf, 2, 0))) > > int vfprintf(FILE *stream, const char *fmt, va_list args) > > { > > - return __nolibc_printf(__nolibc_fprintf_cb, stream, fmt, args); > > + struct __nolibc_fprintf_cb_state state; > > + > > + state.stream = stream; > > + state.buf_offset = 0; > > + return __nolibc_printf(__nolibc_fprintf_cb, &state, fmt, args); > > } > > > > static __attribute__((unused, format(printf, 1, 0))) > > Thanks, > Willy