From nobody Mon Jul 13 06:45:50 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gzCct6Frfz6kT1N for ; Mon, 13 Jul 2026 06:45:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gzCct3BQKz3kks for ; Mon, 13 Jul 2026 06:45:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1783925150; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fdimtwYr2yLrZd3STCjTqL78Ys0oVOCtJZoIcq06u0U=; b=j/h5GJMx540Rk8kQQF81bNbycqFrTiUoC0Uv7/hiAU9MiSXGMfj3cb8pOudQ1oNn+JSelX 7N2s5QgGXx2fE9/RtyAJs/cGo99x5fZwW5sa09ByYHS+20+EM69gZqZL3ngn2rV3cXSjsS mANczQO/vw7vZ08luXrNOYPr2cl7arR3aegjEud1VLHku2lSWQkNV8YFszGgKXE09tQuHt gMtZ9z/ACkXqdN1rF90WOuzuCZFUqhotZPL7mw6vVJA301vQy8i+mZHiz593cF1ahKGNLb jgYxD2rqy8B5cxc3L4WNScvuPGsHdH9WXaWd3SN7IIlkRLHIE3bsCb0ceyTRjw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1783925150; a=rsa-sha256; cv=none; b=gbIFdOR8y5M9ZvsR320sJxT7iyNjmFQcEwxLSEMi6wQFm65uW7nfeUfFffqhaYF22/Ku67 aXsno0S27v+1t0WC+x3nvOI8KtKybOlRVBMFeyhBFeacMYk0EK6aCQNyOPM9DtOKuonIZ6 c01vVOCNL6gZhND3Eb0qtTOzoDOKU8OG2oLXUAv4kzaQZcu13Gxkuw62QXSjQAfb9zo9p/ QDC3pxor3PuUDfn5rnIZN29LtTDdY3oAJ4UD8xvY/amJKg5AA4lZfFhJ7Sv8fBRKuHTJpM EpFrR/7HSUr9bMTqb0G0BBwM2W5GNLPdxGY37hbQ/c4JZM+sZMDD6DbdyEzDxQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1783925150; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fdimtwYr2yLrZd3STCjTqL78Ys0oVOCtJZoIcq06u0U=; b=gNP0tzMNUIGuC7WCv/JGL9m1AQdVZjTdOzp+RiZ5y4WNHVTiVuN3adKAJJsE2Umby67qsx vbHG+PhwXQAmwYn6f7jR4MIJ9OySMGy5dud0ouSuoEQAlCMpQtuKPeJmj6BvSGiebZv7jp f1rs8tc86hKzZCrMELpOv0uCNk/BOpoReIWj2gwbGnNfe2XBJ903Ff6YqpnKHIT5bs5v/f wa+8FQRsTr3CR1p0jxbxQTiFl3FPJwE6fixa+TKmorP8mJchvFmN1zao0Qcdzq5VmqUJ3N Q0r8FtxrcVKDU4ABEOwGy67Li95oeGaZ3GSk6h52YZnPg6fgHptpHMLk4Pax2A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gzCct1hnpznqp for ; Mon, 13 Jul 2026 06:45:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1f73e by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 13 Jul 2026 06:45:50 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: 60382b4a04fa - main - libfetch: Reduce copying List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 60382b4a04fa39e9bf65b964b1b7b4bed6eaa56a Auto-Submitted: auto-generated Date: Mon, 13 Jul 2026 06:45:50 +0000 Message-Id: <6a54899e.1f73e.51001c06@gitrepo.freebsd.org> The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=60382b4a04fa39e9bf65b964b1b7b4bed6eaa56a commit 60382b4a04fa39e9bf65b964b1b7b4bed6eaa56a Author: Dag-Erling Smørgrav AuthorDate: 2026-07-13 06:43:56 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-07-13 06:43:56 +0000 libfetch: Reduce copying Reduce the amount of copying we do when performing buffered reads. MFC after: 1 week Reviewed by: op Differential Revision: https://reviews.freebsd.org/D58113 --- lib/libfetch/common.c | 45 +++++++++++++++++++++++++++++---------------- lib/libfetch/common.h | 2 ++ lib/libfetch/ftp.c | 23 ++++++++++++----------- lib/libfetch/http.c | 41 ++++++++++++++++++----------------------- 4 files changed, 61 insertions(+), 50 deletions(-) diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index 74748e5b7d17..dabc8c4b16b3 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -1437,12 +1437,23 @@ fetch_getln(conn_t *conn) /* allocate initial buffer */ if (conn->buf == NULL) { if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) - return (-1); + goto fail; + conn->line = conn->buf; conn->bufsize = MIN_BUF_SIZE; conn->buflen = 0; conn->pos = 0; } + /* look at the data we already have */ + if (conn->pos < conn->buflen) { + conn->line = conn->buf + conn->pos; + while (conn->pos < conn->buflen) + if (conn->buf[conn->pos++] == '\n') + goto found; + /* reset for the upcoming memmove() */ + conn->pos = conn->line - conn->buf; + } + /* move residual data up */ if (conn->buflen > 0) { if (conn->pos < conn->buflen) { @@ -1451,20 +1462,16 @@ fetch_getln(conn_t *conn) } conn->buflen -= conn->pos; conn->pos -= conn->pos; + conn->line = conn->buf; } - /* do we have a complete line? */ - while (conn->pos < conn->buflen) - if (conn->buf[conn->pos++] == '\n') - goto found; - for (;;) { /* read as much as we can right now */ rlen = fetch_read(conn, conn->buf + conn->buflen, conn->bufsize - conn->buflen - 1); /* error */ if (rlen < 0) - return (-1); + goto fail; /* advance and terminate */ conn->buflen += rlen; conn->buf[conn->buflen] = '\0'; @@ -1480,21 +1487,25 @@ fetch_getln(conn_t *conn) tmp = conn->buf; tmpsize = conn->bufsize * 2; if ((tmp = realloc(tmp, tmpsize)) == NULL) - return (-1); - conn->buf = tmp; + goto fail; + conn->line = conn->buf = tmp; conn->bufsize = tmpsize; } } /* connection closed, return what's left */ conn->pos = conn->buflen; found: - rlen = conn->pos; - if (rlen > 0 && conn->buf[rlen - 1] == '\n') - conn->buf[--rlen] = '\0'; - if (rlen > 0 && conn->buf[rlen - 1] == '\r') - conn->buf[--rlen] = '\0'; - DEBUGF("<<< %.*s\n", (int)rlen, conn->buf); - return (rlen); + conn->linelen = (conn->buf + conn->pos) - conn->line; + if (conn->linelen > 0 && conn->line[conn->linelen - 1] == '\n') + conn->line[--conn->linelen] = '\0'; + if (conn->linelen > 0 && conn->line[conn->linelen - 1] == '\r') + conn->line[--conn->linelen] = '\0'; + DEBUGF("<<< %.*s\n", (int)conn->linelen, conn->line); + return (conn->linelen); +fail: + conn->line = NULL; + conn->linelen = 0; + return (-1); } @@ -1520,6 +1531,8 @@ fetch_bufread(conn_t *conn, void *buf, size_t len) conn->buflen = 0; conn->pos = 0; } + conn->line = NULL; + conn->linelen = 0; /* return residual data first */ if (conn->buflen > conn->pos) { diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h index 143d4fe82945..ac6ec923050d 100644 --- a/lib/libfetch/common.h +++ b/lib/libfetch/common.h @@ -49,8 +49,10 @@ typedef struct fetchconn conn_t; struct fetchconn { int sd; /* socket descriptor */ char *buf; /* buffer */ + char *line; /* line (within buffer) */ size_t bufsize; /* buffer size */ size_t buflen; /* length of buffer contents */ + size_t linelen; /* length of line */ size_t pos; /* current position in buffer */ int err; /* last protocol reply code */ #ifdef WITH_SSL diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index 5e4148e0fa2c..4c1a63d3da82 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -148,8 +148,9 @@ ftp_chkerr(conn_t *conn) fetch_syserr(); return (-1); } - if (isftpinfo(conn->buf)) { - while (conn->buflen && !isftpreply(conn->buf)) { + + if (isftpinfo(conn->line)) { + while (rlen && !isftpreply(conn->line)) { if ((rlen = fetch_getln(conn)) < 0) { fetch_syserr(); return (-1); @@ -157,14 +158,14 @@ ftp_chkerr(conn_t *conn) } } - if (!isftpreply(conn->buf)) { + if (!isftpreply(conn->line)) { ftp_seterr(FTP_PROTOCOL_ERROR); return (-1); } - conn->err = (conn->buf[0] - '0') * 100 - + (conn->buf[1] - '0') * 10 - + (conn->buf[2] - '0'); + conn->err = (conn->line[0] - '0') * 100 + + (conn->line[1] - '0') * 10 + + (conn->line[2] - '0'); return (conn->err); } @@ -236,8 +237,8 @@ ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen) if (conn->err != FTP_WORKING_DIRECTORY && conn->err != FTP_FILE_ACTION_OK) return (FTP_PROTOCOL_ERROR); - end = conn->buf + conn->buflen; - src = conn->buf + 4; + end = conn->line + conn->linelen; + src = conn->line + 4; if (src >= end || *src++ != '"') return (FTP_PROTOCOL_ERROR); for (q = 0, dst = pwd; src < end && pwdlen--; ++src) { @@ -417,7 +418,7 @@ ftp_stat(conn_t *conn, const char *file, struct url_stat *us) ftp_seterr(e); return (-1); } - for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++) + for (ln = conn->line + 4; *ln && isspace((unsigned char)*ln); ln++) /* nothing */ ; for (us->size = 0; *ln && isdigit((unsigned char)*ln); ln++) us->size = us->size * 10 + *ln - '0'; @@ -435,7 +436,7 @@ ftp_stat(conn_t *conn, const char *file, struct url_stat *us) ftp_seterr(e); return (-1); } - for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++) + for (ln = conn->line + 4; *ln && isspace((unsigned char)*ln); ln++) /* nothing */ ; switch (strspn(ln, "0123456789")) { case 14: @@ -688,7 +689,7 @@ ftp_transfer(conn_t *conn, const char *oper, const char *file, * Find address and port number. The reply to the PASV command * is IMHO the one and only weak point in the FTP protocol. */ - ln = conn->buf; + ln = conn->line; switch (e) { case FTP_PASSIVE_MODE: case FTP_LPASSIVE_MODE: diff --git a/lib/libfetch/http.c b/lib/libfetch/http.c index 89f731c896dc..cefa8adbb8bd 100644 --- a/lib/libfetch/http.c +++ b/lib/libfetch/http.c @@ -126,8 +126,7 @@ * I/O functions for decoding chunked streams */ -struct httpio -{ +struct httpio { conn_t *conn; /* connection */ int chunked; /* chunked mode */ char *buf; /* chunk buffer */ @@ -148,17 +147,14 @@ struct httpio static int http_new_chunk(struct httpio *io) { - unsigned char *p, *eol; + char *p; if (fetch_getln(io->conn) == -1) return (-1); - p = (unsigned char *)io->conn->buf; - if (io->conn->pos < 2 || !isxdigit(*p)) - return (-1); - - eol = (unsigned char *)io->conn->buf + io->conn->pos; - while (p < eol && *p && !isspace(*p) && *p != ';') { + for (p = io->conn->line; + *p != '\0' && !isspace((unsigned char)*p) && *p != ';'; + p++) { io->chunksize <<= 4; if (*p >= '0' && *p <= '9') io->chunksize += *p - '0'; @@ -168,7 +164,6 @@ http_new_chunk(struct httpio *io) io->chunksize += 10 + *p - 'a'; else return (-1); - p++; } #ifndef NDEBUG @@ -435,9 +430,9 @@ http_get_reply(conn_t *conn) * on finding one, but if we do, insist on it being 1.0 or 1.1. * We don't care about the reason phrase. */ - if (strncmp(conn->buf, "HTTP", 4) != 0) + if (conn->linelen < 4 || strncmp(conn->line, "HTTP", 4) != 0) return (HTTP_PROTOCOL_ERROR); - p = conn->buf + 4; + p = conn->line + 4; if (*p == '/') { if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1')) return (HTTP_PROTOCOL_ERROR); @@ -521,17 +516,17 @@ http_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p) { unsigned int i, len; - if (conn->pos == 0 || conn->buf[0] == '\0') + if (conn->linelen == 0) return (hdr_end); /* Copy the line to the headerbuf */ - if (hbuf->bufsize < conn->pos + 1) { - if ((hbuf->buf = realloc(hbuf->buf, conn->pos + 1)) == NULL) + if (hbuf->bufsize <= conn->linelen) { + if ((hbuf->buf = realloc(hbuf->buf, conn->linelen + 1)) == NULL) return (hdr_syserror); - hbuf->bufsize = conn->pos + 1; + hbuf->bufsize = conn->linelen + 1; } - strcpy(hbuf->buf, conn->buf); - hbuf->buflen = conn->buflen; + memcpy(hbuf->buf, conn->line, conn->linelen + 1); + hbuf->buflen = conn->linelen; /* * Fetch possible continuation lines. Stop at 1st non-continuation @@ -545,19 +540,19 @@ http_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p) * Note: we previously considered a pure whitespace line * equivalent to an empty one. This was incorrect. */ - if (conn->buf[0] != ' ' && conn->buf[0] != "\t"[0]) + if (conn->line[0] != ' ' && conn->line[0] != '\t') break; /* Got a continuation line. Concatenate to previous */ - len = hbuf->buflen + conn->buflen; - if (hbuf->bufsize < len + 1) { + len = hbuf->buflen + conn->linelen; + if (hbuf->bufsize <= len) { len *= 2; if ((hbuf->buf = realloc(hbuf->buf, len + 1)) == NULL) return (hdr_syserror); hbuf->bufsize = len + 1; } - strcpy(hbuf->buf + hbuf->buflen, conn->buf); - hbuf->buflen += conn->buflen; + memcpy(hbuf->buf + hbuf->buflen, conn->line, conn->linelen + 1); + hbuf->buflen += conn->linelen; } /*