From: Kjetil Barvik <barvik@broadpark.no>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: ??? Re: [PATCH/RFC v1 1/6] symlinks.c: small cleanup and optimisation
Date: Thu, 29 Jan 2009 15:19:05 +0100 [thread overview]
Message-ID: <86iqnygppi.fsf@broadpark.no> (raw)
In-Reply-To: <7vskn3np6u.fsf@gitster.siamese.dyndns.org>
* Kjetil Barvik <barvik@broadpark.no> writes:
> + /*
> + * Is the cached path string a substring of 'name', is 'name'
> + * a substring of the cached path string, or is 'name' and the
> + * cached path string the exact same string?
> + */
> + if (i >= max_len && ((i < len && name[i] == '/') ||
> + (i < cache.len && cache.path[i] == '/') ||
> + (len == cache.len))) {
* Junio C Hamano <gitster@pobox.com> writes:
> As you described in your commit log message, what this really wants to
> check is (i == max_len). By saying (i >= max_len) you are losing
> readability, optimizing for compilers (that do not notice this)
When the compiler see the source line:
'while (i < max_len && name[i] == cache.path[i])'
it will, from what I know about compilers and machine instructions for
intel cpu's, generate the following pseudo instructions for this line
(before more compiler optimisation is done):
1 test !(i < max_len) /* which is (i >= max_len) */
2 jump-if-not-zero first_instruction_address_outside_the_loop
3 "some instructions to retrieve some memory locations and test
name[i] == cache.path[i]"
4 jump-if-not-zero instruction-address-to-continue-to-loop
So, I thought that if I could use the exact same test as the compiler
have to generate for line 1, then I would maybe saved a test, and
maybe also a jump instruction.
Compiling with 'gcc -Os' (optimise for text segment size) I was able to
save 4 bytes for this file, which I think shows that gcc is able to
take advantage of this trick.
> and pessimizing for human readers (like me, who had to spend a few
> dozens of seconds to realize what you are doing, to speculate why you
> might have thought this would be a good idea, and writing this
> paragraph). I do not know if it is a good trade-off.
But, it was not easy to come up with a real test which shows a
noticeable time difference from this trick, so, I guess this is only a
trick for the kernel people (at most), so I revert it and will use
'==' for version 2, such that we keep a little better readability.
-- kjetil
ps! Compiling with '-march=core2 -O2 -g0 -s -fomit-frame-pointer', the
difference in text segment was 64 bytes when using this trick.
next prev parent reply other threads:[~2009-01-29 14:20 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-01-26 21:17 [PATCH/RFC v1 0/6] git checkout: more cleanups, optimisation, less lstat() calls Kjetil Barvik
2009-01-26 21:17 ` [PATCH/RFC v1 1/6] symlinks.c: small cleanup and optimisation Kjetil Barvik
2009-01-28 20:36 ` ??? " Junio C Hamano
2009-01-29 14:19 ` Kjetil Barvik [this message]
2009-01-26 21:17 ` [PATCH/RFC v1 2/6] remove some memcpy() and strchr() calls inside create_directories() Kjetil Barvik
2009-01-28 20:36 ` Junio C Hamano
2009-01-26 21:17 ` [PATCH/RFC v1 3/6] cleanup of write_entry() in entry.c Kjetil Barvik
2009-01-28 21:31 ` Junio C Hamano
2009-01-26 21:17 ` [PATCH/RFC v1 4/6] use fstat() instead of lstat() when we have an opened file Kjetil Barvik
2009-01-26 21:17 ` [PATCH/RFC v1 5/6] combine-diff.c: remove a call to fstat() inside show_patch_diff() Kjetil Barvik
2009-01-27 9:35 ` Mike Ralphson
2009-01-27 12:03 ` Kjetil Barvik
2009-01-27 12:06 ` Mike Ralphson
2009-01-28 20:37 ` Junio C Hamano
2009-01-29 1:16 ` Junio C Hamano
2009-01-29 8:20 ` Kjetil Barvik
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86iqnygppi.fsf@broadpark.no \
--to=barvik@broadpark.no \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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.
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 NNTP newsgroup(s).