From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,HK_RANDOM_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: (qmail 12596 invoked from network); 3 May 2020 09:33:55 -0000 Received-SPF: pass (primenet.com.au: domain of zsh.org designates 203.24.36.2 as permitted sender) receiver=inbox.vuxu.org; client-ip=203.24.36.2 envelope-from= Received: from ns1.primenet.com.au (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTPUTF8; 3 May 2020 09:33:55 -0000 Received: (qmail 868 invoked by alias); 3 May 2020 09:33:46 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 45768 Received: (qmail 7881 invoked by uid 1010); 3 May 2020 09:33:46 -0000 X-Qmail-Scanner-Diagnostics: from mail-wm1-f42.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.2/25793. spamassassin: 3.4.4. Clear:RC:0(209.85.128.42):SA:0(-1.0/5.0):. Processed in 2.06072 secs); 03 May 2020 09:33:46 -0000 X-Envelope-From: xzfcpw@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.128.42 as permitted sender) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:user-agent :mime-version:content-transfer-encoding; bh=EK5Ub7akB4cf0DX+imdMMdWeyHltRLQY3k1jgi3kRFg=; b=h11WnGN+WPx2YWdtOoxLqqEihl0lDi1N4BMHkoaGfB5Mmma4DpYZJkNPfmDR0UzB+2 pXfjfGSgoXvGVK44m34qNHJMwsBrJ/HBuzLGv1lVwFLdDuBlR7z5eAC3yFKazHBhAnOk WMvzJk5ynDBka9/drSpkhZR2tsABnJMVwUeU6o1tJD3STj4CADd4ABEaLmqWFJ0LULR6 JDEyHeTecLH7fLmcBEjzQxbh73A9mdW44sr9z3M9AbGvCeOXeqX+9D95MJi+m5jSHz9o ecTDtGz8iJAzVJfxp1WY7pTAb0NyJ8HLEKM3oeVnkFSmdJ73CLZgQAu0nNl2yxFfVDz1 Saxw== X-Gm-Message-State: AGi0PuZ6AoXMxf6HUM65O4pyedShLuFjH/JK0sj23wmY2qcfhY1JwUw3 6AGLAL+nJ4noZVEsrRz0MPdzFw0gKkw= X-Google-Smtp-Source: APiQypKhCgMN0BGwRpWvkA67Xv2WT5Yz2HpWr9v9KtvS+gWv3G7j2mtZIkXTolNhcDT3NBCNOhz4lA== X-Received: by 2002:a1c:f615:: with SMTP id w21mr8270584wmc.183.1588498388905; Sun, 03 May 2020 02:33:08 -0700 (PDT) Message-ID: Subject: PATCH: readhistfile(): do not call ftell() in loop From: xzfcpw@gmail.com To: zsh-workers@zsh.org Date: Sun, 03 May 2020 09:33:05 +0000 Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Prior to this patch, readhistfile() performed one lseek() syscall (inside ftell()) per each history entry. This is time-consuming on large histfiles. This patch replaces this call to ftell() with manual tracking of bytes read. Benchmark on my histfile (5.5M bytes, 138103 lines): hyperfine --warmup 5 './zsh -is < /dev/null' old: 708.8 ms new: 498.9 ms Also, minor code style fix: `fseek(in, 0, 0)` replaced with `fseek(in, 0, SEEK_SET)`. diff --git a/Src/hist.c b/Src/hist.c index 5281e8718..3a8eb9a16 100644 --- a/Src/hist.c +++ b/Src/hist.c @@ -2575,10 +2575,11 @@ resizehistents(void) } static int -readhistline(int start, char **bufp, int *bufsiz, FILE *in) +readhistline(int start, char **bufp, int *bufsiz, off_t *fpos, FILE *in) { char *buf = *bufp; if (fgets(buf + start, *bufsiz - start, in)) { + fpos += strlen(buf + start); int len = start + strlen(buf + start); if (len == start) return -1; @@ -2588,7 +2589,7 @@ readhistline(int start, char **bufp, int *bufsiz, FILE *in) return -1; *bufp = zrealloc(buf, 2 * (*bufsiz)); *bufsiz = 2 * (*bufsiz); - return readhistline(len, bufp, bufsiz, in); + return readhistline(len, bufp, bufsiz, fpos, in); } } else { @@ -2596,7 +2597,7 @@ readhistline(int start, char **bufp, int *bufsiz, FILE *in) if (len > 1 && buf[len - 2] == '\\') { buf[--len - 1] = '\n'; if (!feof(in)) - return readhistline(len, bufp, bufsiz, in); + return readhistline(len, bufp, bufsiz, fpos, in); } } return len; @@ -2612,7 +2613,7 @@ readhistfile(char *fn, int err, int readflags) FILE *in; Histent he; time_t stim, ftim, tim = time(NULL); - off_t fpos; + off_t fpos, lfpos; short *words; struct stat sb; int nwordpos, nwords, bufsiz; @@ -2664,7 +2665,8 @@ readhistfile(char *fn, int err, int readflags) if (readflags & HFILE_SKIPOLD || (hist_ignore_all_dups && newflags & hist_skip_flags)) newflags |= HIST_MAKEUNIQUE; - while (fpos = ftell(in), (l = readhistline(0, &buf, &bufsiz, in))) { + fpos = ftell(in); + while (lfpos = fpos, l = readhistline(0, &buf, &bufsiz, &fpos, in)) { char *pt; int remeta = 0; @@ -2723,7 +2725,8 @@ readhistfile(char *fn, int err, int readflags) && histstrcmp(pt, lasthist.text) == 0) searching = 0; else { - fseek(in, 0, 0); + fseek(in, 0, SEEK_SET); + fpos = 0; histfile_linect = 0; searching = -1; } @@ -2738,7 +2741,7 @@ readhistfile(char *fn, int err, int readflags) if (readflags & HFILE_USE_OPTIONS) { histfile_linect++; - lasthist.fpos = fpos; + lasthist.fpos = lfpos; lasthist.stim = stim; }