git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: Stefan Zager <szager@google.com>
Cc: git@vger.kernel.org
Subject: Re: static variables
Date: Tue, 10 Dec 2013 17:45:01 -0800	[thread overview]
Message-ID: <20131211014501.GI2311@google.com> (raw)
In-Reply-To: <CAHOQ7J-rO-KjHyYk1Gw6Wv+iH_M7DPr76t3G7YN_sUv3YqcJcg@mail.gmail.com>

Stefan Zager wrote:

> This is probably a naive question, but: there are quite a lot of static
> variables in the git code where it's really unnecessary.  Is that just a
> historical artifact, or is there some reason to prefer them?

Sometimes it's for convenience.  Other times it's to work around C89's
requirement that initializers can't include pointers to automatic
variables, so when using parse_options, old commands tend to use
statics for the variables initialized by options.  (Since then, git
has stopped following that so rigidly, which is probably a good
thing.)

Worse, some functions have static buffers when they need a large
buffer and want to avoid too much allocation churn.  As a general
rule, historically very little of git's code (mostly pack related)
needed to be usable with threads, though of course it would be
excellent to fix more code to be thread-safe.

> As an example, here's an excerpt from symlnks.c.  In addition to being
> static, if I'm reading this right, it appears that the 'removal' variable
> is used before it's initialized:

statics are allocated from the .bss section, where they are zeroed
automatically.

> static struct removal_def {
>   char path[PATH_MAX];
>   int len;
> } removal;

Plumbing this through the call stack instead of using a static sounds
like a good idea.  That would mean allocating the removal_def in
unpack-trees.c::check_updates, I think (see v1.6.3-rc0~147^2~16,
"unlink_entry(): introduce schedule_dir_for_removal()", 2009-02-09 for
context).  Then the loop could be divided into chunks that each use
their own removal_def or something.

Sometimes when git needs parallelism and threads don't work, it uses
fork + exec (aka run_command).  Making the relevant functionality
thread-safe is generally much nicer, though.

Thanks and hope that helps,
Jonathan

  reply	other threads:[~2013-12-11  1:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-11  1:25 static variables Stefan Zager
2013-12-11  1:45 ` Jonathan Nieder [this message]
2013-12-11  1:53   ` Jonathan Nieder

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=20131211014501.GI2311@google.com \
    --to=jrnieder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=szager@google.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).