git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: "René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>
Subject: Re: [PATCH] pass constants as first argument to st_mult()
Date: Mon, 01 Aug 2016 14:00:46 -0700	[thread overview]
Message-ID: <xmqq8twgi4qp.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <20160801164723.mober7em6znt56w4@sigill.intra.peff.net> (Jeff King's message of "Mon, 1 Aug 2016 12:47:24 -0400")

Jeff King <peff@peff.net> writes:

>> -	mx = xcalloc(st_mult(num_create, NUM_CANDIDATE_PER_DST), sizeof(*mx));
>> +	mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_create), sizeof(*mx));
>
> I didn't look at all of the calls, but I wonder if it is a natural
> pattern to put the constant second.  

Between

    st_mult(GIT_SHA1_RAWSZ, i)
    st_mult(i, GIT_SHA1_RAWSZ)

the former is more intuitive at least to me [*1*], but calloc(3) disagrees
with me.

> Since multiplication is
> commutative, it would be correct for st_mult() to just flip the order of
> arguments it feeds to unsigned_mult_overflows().
>
> That may introduce the same inefficiency in other callsites, but I
> wonder if it would be fewer.

"git grep -A3 st_mult \*.c" seems to tell me that the callsites with
a constant in their first parameter are the majority (many are
sizeof(something)).  The three places in the patch under discussion
are the only places that got them in the different order.


[Footnote]

*1* I have a slight suspicion that this is cultural, i.e. how
arithmetic is taught in grade schools.  When an apple costs 30 yen
and I have 5 of them, I was taught to multiply 30x5 to arrive at
150, not 5x30=150, and I am guessing that is because the former
matches the natural order of these two numbers (cost, quantity) in
the language I was taught.

  reply	other threads:[~2016-08-01 21:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-30 18:18 [PATCH] pass constants as first argument to st_mult() René Scharfe
2016-08-01 16:47 ` Jeff King
2016-08-01 21:00   ` Junio C Hamano [this message]
2016-08-01 21:11     ` Jeff King
2016-08-01 22:31       ` Junio C Hamano
2016-08-03 19:13         ` Jeff King
2016-08-03 19:41           ` Junio C Hamano
2016-08-03 19:49             ` Jeff King
2016-08-03 19:58               ` Junio C Hamano
2016-08-03 19:59               ` Stefan Beller
2016-08-03 20:04                 ` Jeff King
2016-08-03 19:56           ` Christian Couder

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=xmqq8twgi4qp.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --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).