From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 4A6E23858D20 for ; Wed, 9 Aug 2023 14:43:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A6E23858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1bc0d39b52cso44656755ad.2 for ; Wed, 09 Aug 2023 07:43:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1691592179; x=1692196979; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JCdcWU2yLzt0Outfc1yOdk0gUhmOfcE+so0qMrQejf4=; b=XZwz+JcK0YM8L87ZCQrhCXc8pO/IzM76njSgu4i7+tf4yQBFCSwIAaKyKLCJ7Zdl3j txaAds9dU9KiIxNCZwgMyfEu/81Q4qmrrXei6cNVWc2GE8KjmgDUnTJyRcaKcF6YqUva Okv/1gFbz9OdpxPEdMAiYfZOC0Yibi3aaxilqRW+HPDVjgM26JqxO4JiBY8dnnOM+YUj 049aDJKmG9sKQr13s87wrOJafwwND6z2c5XJ1t2he8o5Xsg+9xearRzFKQXNVCGHN/jA NkSx/xZBcuEydQBMi6IRu5LOQppaHw8uej20pL0oyf1zLxo9hm1CF45bKXYulc9h8Awq +IQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691592179; x=1692196979; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JCdcWU2yLzt0Outfc1yOdk0gUhmOfcE+so0qMrQejf4=; b=lkplRelIu/+XUzsnxtLzPXXsyYRAf19bt9OkL5/xknzkOsYD1tTe6LjHZB0HwKru2Q yVFPmn/ohTsc/HuEwe1+uPto3eMiTm1+lShw0oZp6mPyzbj7AMqfes2so8WdQodrlGP+ CjBGc+2H9ZXOpcCQZBSxr3447w8sc4EJG3gcG0DuR9zX/JQqPkZFIjJNzhYgqRSWyWhX v6tpViYwbDPLB+mDNVrHlOdkuwJjcGO86GQqba+D0C1FLRDAsmMaNlswfwjL0j3PE/+M eZyyru26Hvm1TGJiLUHRwUAQoN9iAinOdVJ4NFGYoyxGCqFNV2zlNNOLjiCyGqXjH17K /w3A== X-Gm-Message-State: AOJu0YzC7OXkhFjur1OWQjQruLDCdBtOdMGvX2OeZ29fvg2ByEpV921L v2h3qwvGxwU++zmogS5Fn/rmbjPY8Ehxs8cULR8nhSXtbBIrEOJjUsM= X-Google-Smtp-Source: AGHT+IGGxKePUHNuj/uuSgrUzKUJA2zp1ap1AxRnZFG3UbblW6Tdq44XpcdsBuaFCUJQZH/x6RKFvoJP+Z1YwAaG+4w= X-Received: by 2002:a17:90a:d384:b0:263:9661:a35c with SMTP id q4-20020a17090ad38400b002639661a35cmr2057647pju.8.1691592179130; Wed, 09 Aug 2023 07:42:59 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: =?UTF-8?Q?Cl=C3=A9ment_Chigot?= Date: Wed, 9 Aug 2023 16:42:48 +0200 Message-ID: Subject: Re: PR30724, cygwin ld performance regression since 014a602b86 To: Alan Modra Cc: binutils@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: On Wed, Aug 9, 2023 at 1:21=E2=80=AFAM Alan Modra wrote: > > According to the reporter of this bug the newlib fseek implementation > is likely slowed down by locking and fflush, only attempting to > optimise seeks when the file is opened read-only. Thus when writing > the output we get a dramatic slowdown due to commit 014a602b86. > > Cl=C3=A9ment would you please check that this doesn't regress anything on > mingw. I'm not seeing any and the issue fixed by 014a602b86 is still fixed so it looks good. However, I have a question regarding the purpose of bfd_io_force. Unless bfd_seek fails, it would be overridden right away. So are there cases where bfd_seek fails and bfd doesn't report an error, continuing to perform inputs/outputs as if nothing happens ? > PR 30724 > * bfd.c (enum bfd_last_io): New. > (struct bfd): Add last_io field. > * bfd-in2.h: Regenerate. > * bfd-io.c (bfd_bread, bfd_bwrite): Force seek if last_io is > opposite direction. > (bfd_seek): Reinstate optimisation for seek to same position. > > diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h > index 5f49807d7f2..cc62ab19617 100644 > --- a/bfd/bfd-in2.h > +++ b/bfd/bfd-in2.h > @@ -1916,6 +1916,14 @@ enum bfd_direction > both_direction =3D 3 > }; > > +enum bfd_last_io > + { > + bfd_io_seek =3D 0, > + bfd_io_read =3D 1, > + bfd_io_write =3D 2, > + bfd_io_force =3D 3 > + }; > + > enum bfd_plugin_format > { > bfd_plugin_unknown =3D 0, > @@ -2068,6 +2076,20 @@ struct bfd > /* The direction with which the BFD was opened. */ > ENUM_BITFIELD (bfd_direction) direction : 2; > > + /* POSIX.1-2017 (IEEE Std 1003.1) says of fopen : "When a file is > + opened with update mode ('+' as the second or third character in > + the mode argument), both input and output may be performed on > + the associated stream. However, the application shall ensure > + that output is not directly followed by input without an > + intervening call to fflush() or to a file positioning function > + (fseek(), fsetpos(), or rewind()), and input is not directly > + followed by output without an intervening call to a file > + positioning function, unless the input operation encounters > + end-of-file." > + This field tracks the last IO operation, so that bfd can insert > + a seek when IO direction changes. */ > + ENUM_BITFIELD (bfd_last_io) last_io : 2; > + > /* Is the file descriptor being cached? That is, can it be closed as > needed, and re-opened when accessed later? */ > unsigned int cacheable : 1; > diff --git a/bfd/bfd.c b/bfd/bfd.c > index e43a388ac72..88943a042d6 100644 > --- a/bfd/bfd.c > +++ b/bfd/bfd.c > @@ -53,6 +53,14 @@ EXTERNAL > . both_direction =3D 3 > . }; > . > +.enum bfd_last_io > +. { > +. bfd_io_seek =3D 0, > +. bfd_io_read =3D 1, > +. bfd_io_write =3D 2, > +. bfd_io_force =3D 3 > +. }; > +. > .enum bfd_plugin_format > . { > . bfd_plugin_unknown =3D 0, > @@ -208,6 +216,20 @@ CODE_FRAGMENT > . {* The direction with which the BFD was opened. *} > . ENUM_BITFIELD (bfd_direction) direction : 2; > . > +. {* POSIX.1-2017 (IEEE Std 1003.1) says of fopen : "When a file is > +. opened with update mode ('+' as the second or third character in > +. the mode argument), both input and output may be performed on > +. the associated stream. However, the application shall ensure > +. that output is not directly followed by input without an > +. intervening call to fflush() or to a file positioning function > +. (fseek(), fsetpos(), or rewind()), and input is not directly > +. followed by output without an intervening call to a file > +. positioning function, unless the input operation encounters > +. end-of-file." > +. This field tracks the last IO operation, so that bfd can insert > +. a seek when IO direction changes. *} > +. ENUM_BITFIELD (bfd_last_io) last_io : 2; > +. > . {* Is the file descriptor being cached? That is, can it be closed as > . needed, and re-opened when accessed later? *} > . unsigned int cacheable : 1; > diff --git a/bfd/bfdio.c b/bfd/bfdio.c > index 22c39a7b0cc..e0d47b3ee1c 100644 > --- a/bfd/bfdio.c > +++ b/bfd/bfdio.c > @@ -279,6 +279,14 @@ bfd_bread (void *ptr, bfd_size_type size, bfd *abfd) > return -1; > } > > + if (abfd->last_io =3D=3D bfd_io_write) > + { > + abfd->last_io =3D bfd_io_force; > + if (bfd_seek (abfd, 0, SEEK_CUR) !=3D 0) > + return -1; > + } > + abfd->last_io =3D bfd_io_read; > + > nread =3D abfd->iovec->bread (abfd, ptr, size); > if (nread !=3D -1) > abfd->where +=3D nread; > @@ -313,6 +321,14 @@ bfd_bwrite (const void *ptr, bfd_size_type size, bfd= *abfd) > return -1; > } > > + if (abfd->last_io =3D=3D bfd_io_read) > + { > + abfd->last_io =3D bfd_io_force; > + if (bfd_seek (abfd, 0, SEEK_CUR) !=3D 0) > + return -1; > + } > + abfd->last_io =3D bfd_io_write; > + > nwrote =3D abfd->iovec->bwrite (abfd, ptr, size); > if (nwrote !=3D -1) > abfd->where +=3D nwrote; > @@ -456,6 +472,13 @@ bfd_seek (bfd *abfd, file_ptr position, int directio= n) > if (direction !=3D SEEK_CUR) > position +=3D offset; > > + if (((direction =3D=3D SEEK_CUR && position =3D=3D 0) > + || (direction =3D=3D SEEK_SET && (ufile_ptr) position =3D=3D abfd= ->where)) > + && abfd->last_io !=3D bfd_io_force) > + return 0; > + > + abfd->last_io =3D bfd_io_seek; > + > result =3D abfd->iovec->bseek (abfd, position, direction); > if (result !=3D 0) > { > > -- > Alan Modra > Australia Development Lab, IBM