git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Vasyl Vavrychuk <vvavrychuk@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: jgit, MutableInteger vs AtomicInteger
Date: Tue, 25 Nov 2008 11:19:47 -0800	[thread overview]
Message-ID: <20081125191947.GI2932@spearce.org> (raw)
In-Reply-To: <loom.20081125T134459-650@post.gmane.org>

Vasyl Vavrychuk <vvavrychuk@gmail.com> wrote:
> I've just started browsing jgit sources and an obvious question arise.
> I didn't found such question in mail list and decided to ask the community.
> 
> I don't see reason behind creating own mutable integer container because we 
> have java.util.concurrent.atomic.AtomicInteger with methods
>     public final int get()
>     public final void set(int i)
> 
> And that is what we want.

Eh, its sort-of what we want.

The MutableInteger used in jgit is used instead of this C construct:

	int x;

	call_something(&x);
	call_something_else(&x);
	... now use x ...

In other words it is stack allocated, passed into methods for them
to read and/or update, and then later used in the caller.

AtomicInteger does in fact have get and set methods, providing what
would seem to be the same operation.  But if you read the source
code you'll see that the get and set methods operate against a
volatile int.  IIRC the volatile tag requires that the JRE must
not cache the value in a register.

If you look again at how MutableInteger is used, we never touch the
value outside of a single thread.  The volatile tag on the field
isn't necessary.  It may actually make it harder for the JIT to
produce tight machine code.  Where we use MutableInteger is the
depths of revision traversal and commit parsing, it *must* be fast.


Also, there's the simple fact that I forgot about AtomicInteger,
and it took me less time to code MutableInteger than it did to look
to see if the JRE had something already.  ;-)

-- 
Shawn.

      reply	other threads:[~2008-11-25 19:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-25 13:54 jgit, MutableInteger vs AtomicInteger Vasyl Vavrychuk
2008-11-25 19:19 ` Shawn O. Pearce [this message]

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=20081125191947.GI2932@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=vvavrychuk@gmail.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).