From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id BE6E13858D20 for ; Tue, 8 Aug 2023 23:21:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BE6E13858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6bd0a0a675dso817878a34.2 for ; Tue, 08 Aug 2023 16:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691536877; x=1692141677; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=uq3TeJ3zIrsuo+tVZDnh+BkH+lERx0GbDn1nDlAQjN4=; b=AnNIyOHvnZ7PRXs4aQzEgr7qTp5jfxLdbWXp0FiEpfMrO0WR0aib+kZzMQGESIZnQu wxLlKYs6YblqPy5EAW1Onl56AxLRvOvweYuE26IrlZ6wKi7AppLEUPGfkoqFmzFIdJ66 ICB80v+dxugZEyZizWUS+UeC3ExZ+U6hB1treCuJGe/LY4RhQxgo36a4Y3dwCE69kkzn k6AWuYz7TXs8OBFZFixH3e/ywc4pPdLVgyZk0DuPt+j/MzDPydfiCdU6Q4y3FhP3czd+ lY6yMWhPiUx8+alj1SdERTE2WJukFkitlkmfqWoJXzatK71qdZkL1d9X9tHDECDjRJxP 5mZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691536877; x=1692141677; h=content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uq3TeJ3zIrsuo+tVZDnh+BkH+lERx0GbDn1nDlAQjN4=; b=L7jxzFiEapLDS7sIiC0Rkxxfayd0SKwj22540ViyG7PpHI1Gc89tzeMeYQStyMNiYZ nrGh+n0+VzL7S1lObYGThN2vJL1H5Q+qKWL6LvFff2Dk40Y7IOVU/UWXQ2VOGZZjvN17 RDehv3mGanLRgiOH1xLH6E4IDcJCbSlqhhw5ObrCK8mdrjUNNpEQgwcTEbll06ECxLi3 4QJEwA2euWqbfiyIBEAsl/8HjD6/TkdDh/Zx8V/2BrKAqnRgsFc9PI5dOs8JMLm17RZN 1fLng20M6y0zDPiuug6QCgaC+gZgSBGYDGGlpOfDc4dV98ir93FofqfDFcxyUVuoIuD6 dJ+g== X-Gm-Message-State: AOJu0Yw7mwgU7qnyNwBVA9GRYDs5jE9X/Y5Ewq9AzXdY6FFiWZfOLcJA S2RiAuhkp4W4Awsa+LeK56p4Hwd5pxs= X-Google-Smtp-Source: AGHT+IFEtg1D7nb+XrOAMUEmfY8azy6uSUPcUI+sQhbfuaEZ4Re2uU2dhVWh9ziujeg8AzhG7XREdw== X-Received: by 2002:a05:6870:46a0:b0:1be:c586:31ae with SMTP id a32-20020a05687046a000b001bec58631aemr1168413oap.40.1691536876922; Tue, 08 Aug 2023 16:21:16 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id d17-20020aa78151000000b0064398fe3451sm8551541pfn.217.2023.08.08.16.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 16:21:15 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 561901142572; Wed, 9 Aug 2023 08:51:13 +0930 (ACST) Date: Wed, 9 Aug 2023 08:51:13 +0930 From: Alan Modra To: binutils@sourceware.org Cc: =?iso-8859-1?Q?Cl=E9ment?= Chigot Subject: PR30724, cygwin ld performance regression since 014a602b86 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3034.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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: 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ément would you please check that this doesn't regress anything on mingw. 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 = 3 }; +enum bfd_last_io + { + bfd_io_seek = 0, + bfd_io_read = 1, + bfd_io_write = 2, + bfd_io_force = 3 + }; + enum bfd_plugin_format { bfd_plugin_unknown = 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 = 3 . }; . +.enum bfd_last_io +. { +. bfd_io_seek = 0, +. bfd_io_read = 1, +. bfd_io_write = 2, +. bfd_io_force = 3 +. }; +. .enum bfd_plugin_format . { . bfd_plugin_unknown = 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 == bfd_io_write) + { + abfd->last_io = bfd_io_force; + if (bfd_seek (abfd, 0, SEEK_CUR) != 0) + return -1; + } + abfd->last_io = bfd_io_read; + nread = abfd->iovec->bread (abfd, ptr, size); if (nread != -1) abfd->where += nread; @@ -313,6 +321,14 @@ bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd) return -1; } + if (abfd->last_io == bfd_io_read) + { + abfd->last_io = bfd_io_force; + if (bfd_seek (abfd, 0, SEEK_CUR) != 0) + return -1; + } + abfd->last_io = bfd_io_write; + nwrote = abfd->iovec->bwrite (abfd, ptr, size); if (nwrote != -1) abfd->where += nwrote; @@ -456,6 +472,13 @@ bfd_seek (bfd *abfd, file_ptr position, int direction) if (direction != SEEK_CUR) position += offset; + if (((direction == SEEK_CUR && position == 0) + || (direction == SEEK_SET && (ufile_ptr) position == abfd->where)) + && abfd->last_io != bfd_io_force) + return 0; + + abfd->last_io = bfd_io_seek; + result = abfd->iovec->bseek (abfd, position, direction); if (result != 0) { -- Alan Modra Australia Development Lab, IBM