From patchwork Tue Aug 8 23:21:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 132966 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2452974vqr; Tue, 8 Aug 2023 16:21:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHd9ApmBC+5v7IoYWujh9gE1aQ5HMXffONFCFlRKG8Rb/4PAmobSaRgvkc1DLsGF99JgotS X-Received: by 2002:a17:906:2d4:b0:99b:56f1:3002 with SMTP id 20-20020a17090602d400b0099b56f13002mr968619ejk.61.1691536888636; Tue, 08 Aug 2023 16:21:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691536888; cv=none; d=google.com; s=arc-20160816; b=lZPIg1I5yGmQSWPhN0raThMG0eSJYZbGBZimpz6sltYcYOg248z1GRJR+HusuA6t9x bBDgooaFhmmdySAdWHns3QC09+td8RrBItCi29B2nlEp/7Kc9RamGBWSqKwUhzpZllo5 D4CMA+nlB6gqMlLlTTBlibTaj7qkLx3ShGlSyyO1bNgVcVKTI7Ir4si5hiOtyuUXXDE9 9lbKcv+BAVXmp/9FdUfnGFEXJHVl8PLg/nQ3kULlmKIVY3cJqnfTjUH5btmEWLG5ILvM TT9EfLtGh3SR52rtIqinT/BblmAtd8rZ1DOloJgOqLCDdQcg25pZrKqZSCCMjL3siwUC vilQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:content-disposition:mime-version :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=uq3TeJ3zIrsuo+tVZDnh+BkH+lERx0GbDn1nDlAQjN4=; fh=7b4X05PxNb/Y3Z/lOfdbWZaEvVS46d8d81rkLXorINs=; b=uN9fYiUh/o7KRpEVR0q2OidQc2O9cCdVB/OL/trXDar0q+24AL33PEG+Aohk58cfsG ERnMvsVtVHQFKdd0DpF84emq7l7ioSpPVbMQc7/BoSMkvwt0fkMw9P/hNrI9rQoqIDBp G1D8W4KEdV7xTyi3QsecIunzQ7uerB1WqIFqlPA6sqDta9KYBSQdwu1+U/DQIthKxmKv hc4MXc2SeoW/D4JIp/w5e5OhBUuQ5CFwRcO+CQQKvZmN6mIwq8AZ13xkb4A0SiONSD/J NVp7uodgSzsdOFLKw9sUJ/k0lPR9D28TVOPP8uo4NZ9N01xKS5EFo2YQAhBzjX9ZN1Jk Kg/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=rWnWAWqn; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dt20-20020a170906b79400b00988da9cd05csi7237779ejb.23.2023.08.08.16.21.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 16:21:28 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=rWnWAWqn; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55E64385842D for ; Tue, 8 Aug 2023 23:21:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 55E64385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691536887; bh=uq3TeJ3zIrsuo+tVZDnh+BkH+lERx0GbDn1nDlAQjN4=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=rWnWAWqnvn50OGHAvHrYALMH6UdYYzRpLvldUW+w6SWr39ReerP8A14/xoVraY2jX ag3rwRAMJIZzgMpRLVOkkYnULRNKBUO5+pHpuHkJqNGzQOrafMgpslHmR/7yAxrxdJ aqvqpDFBz1ZmXrMH7Watw4KqktjSqIdNzZndb6+8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org 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 Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6bd0a0a675dso817878a34.2 for ; Tue, 08 Aug 2023 16:21:17 -0700 (PDT) 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-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 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-Disposition: inline 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773704984731959848 X-GMAIL-MSGID: 1773704984731959848 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) {