public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Qing Zhao <qing.zhao@oracle.com>
Cc: "rguenther@suse.de" <rguenther@suse.de>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Fixing PR107411
Date: Mon, 20 Feb 2023 16:17:26 +0100	[thread overview]
Message-ID: <Y/OPBpgIgJACaSka@tucnak> (raw)
In-Reply-To: <AF2B089C-E35F-4465-84B1-78EBE6715AC5@oracle.com>

On Mon, Feb 20, 2023 at 03:04:51PM +0000, Qing Zhao via Gcc-patches wrote:
> 
> 
> > On Feb 17, 2023, at 5:35 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> > 
> > On Fri, Feb 17, 2023 at 10:26:03PM +0000, Qing Zhao via Gcc-patches wrote:
> >> +	      else if (!DECL_NAME (lhs_var))
> >> +		{
> >> +		  char *lhs_var_name_str
> >> +		    = xasprintf ("D.%u", DECL_UID (lhs_var));
> > 
> > Why xasprintf?
> 
> Just emulated the code in “gimple_add_init_for_auto_var” without thinking too much. -:)
> >  D.%u can be sprintfed into a fixed size automatic buffer,
> > say 3 + (HOST_BITS_PER_INT + 2) / 3 would be a good upper bound for the size
> > of the buffer.  Then you don't need to free it...
> 
> xasprintf is "like a sprintf but provided a pointer to malloc’d storage (without fail)”. If free the pointer properly, then it should be okay, right?
> In addition to “no need to free”, what other benefit to use sprintf other than xasprintf?

xasprintf+free being significantly slower, exactly because it needs to
malloc and free later, where both are fairly expensive functions.
The glibc asprintf for short strings like the above uses a ~ 200 byte
static buffer, stores in there, later mallocs the needed amount of memory
and copies it there (so again, another waste because the string needs to be
copied around), while for longer it can do perhaps many allocations and
realloc at the end to the right size.
The libiberty function actually performs the printing twice, once without
writing result anywhere to compute size, then malloc, then again into the
malloced buffer.

	Jakub


  reply	other threads:[~2023-02-20 15:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-17 22:26 Qing Zhao
2023-02-17 22:35 ` Jakub Jelinek
2023-02-20 15:04   ` Qing Zhao
2023-02-20 15:17     ` Jakub Jelinek [this message]
2023-02-20 17:45       ` Qing Zhao

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=Y/OPBpgIgJACaSka@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=qing.zhao@oracle.com \
    --cc=rguenther@suse.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).