From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] test-lib-functions: make packetize() more efficient
Date: Fri, 27 Mar 2020 12:18:34 -0700 [thread overview]
Message-ID: <xmqq1rpdhaid.fsf@gitster.c.googlers.com> (raw)
In-Reply-To: <20200327080300.GA605934@coredump.intra.peff.net> (Jeff King's message of "Fri, 27 Mar 2020 04:03:00 -0400")
Jeff King <peff@peff.net> writes:
> The packetize() function takes its input on stdin, and requires 4
> separate sub-processes to format a simple string. We can do much better
> by getting the length via the shell's "${#packet}" construct. The one
> caveat is that the shell can't put a NUL into a variable, so we'll have
> to continue to provide the stdin form for a few calls.
Yuck. Binary protocol and shell variables do not mix well.
Documentation/CodingGuidelines forbids ${#parameter} in the first
place and we seem to use it only when we know we are using bash.
Perhaps we should start considering to lift it. I dunno.
> +# convert function arguments or stdin (if not arguments given) to pktline
> +# representation. If multiple arguments are given, they are separated by
> +# whitespace and put in a single packet. Note that data containing NULs must be
> +# given on stdin, and that empty input becomes an empty packet, not a flush
> +# packet (for that you can just print 0000 yourself).
> packetize() {
> + if test $# -gt 0
> + then
> + packet="$*"
> + printf '%04x%s' "$((4 + ${#packet}))" "$packet"
This allows
packetize "want $hash_head"
to be written like so:
packetize want "$hash_head"
which maybe is a handy thing to do.
> + else
> + cat >packetize.tmp &&
> + len=$(wc -c <packetize.tmp) &&
> + printf '%04x' "$(($len + 4))" &&
> + cat packetize.tmp &&
> + rm -f packetize.tmp
perl -e '
my $data = do { local $?; <STDIN> };
printf "%04x%s", length($data), $data;
'
That's one process but much heavier than cat/wc/printf/cat, I guess.
> + fi
> }
>
> # Parse the input as a series of pktlines, writing the result to stdout.
next prev parent reply other threads:[~2020-03-27 19:18 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-27 8:02 [PATCH 0/2] upload-pack: handle unexpected v2 delim packets Jeff King
2020-03-27 8:03 ` [PATCH 1/2] test-lib-functions: make packetize() more efficient Jeff King
2020-03-27 15:16 ` Taylor Blau
2020-03-28 12:25 ` Jeff King
2020-03-27 19:18 ` Junio C Hamano [this message]
2020-03-28 11:20 ` Jeff King
2020-03-29 0:11 ` Junio C Hamano
2020-03-29 3:05 ` Junio C Hamano
2020-03-29 14:53 ` Jeff King
2020-03-29 15:44 ` Junio C Hamano
2020-03-29 14:52 ` Jeff King
2020-03-29 15:02 ` [PATCH] test-lib-functions: simplify packetize() stdin code Jeff King
2020-03-29 15:49 ` Junio C Hamano
2020-03-27 8:03 ` [PATCH 2/2] upload-pack: handle unexpected delim packets Jeff King
2020-03-27 15:17 ` Taylor Blau
2020-03-27 15:18 ` [PATCH 0/2] upload-pack: handle unexpected v2 " Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqq1rpdhaid.fsf@gitster.c.googlers.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).