{"thread":{"id":"16463","subject":"jgit, MutableInteger vs AtomicInteger","startedAt":"2008-11-25T13:54:35Z","lastAt":"2008-11-25T19:19:47Z","messageCount":2,"participants":["Vasyl Vavrychuk","Shawn O. Pearce"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"96474","messageId":"loom.20081125T134459-650@post.gmane.org","threadId":"16463","inReplyTo":null,"subject":"jgit, MutableInteger vs AtomicInteger","fromName":"Vasyl Vavrychuk","fromEmail":"vvavrychuk@gmail.com","sentAt":"2008-11-25T13:54:35Z","receivedAt":"2008-11-25T13:54:35Z","isPatch":false,"sender":{"key":"vvavrychuk@gmail.com","avatar":"https://avatars.githubusercontent.com/u/152563?v=4"},"body":"Hi,\n\nI've just started browsing jgit sources and an obvious question arise.\nI didn't found such question in mail list and decided to ask the community.\n\nI don't see reason behind creating own mutable integer container because we \nhave java.util.concurrent.atomic.AtomicInteger with methods\n    public final int get()\n    public final void set(int i)\n\nAnd that is what we want.\n\nRegards,\n  V. V.\n"},{"id":"96488","messageId":"20081125191947.GI2932@spearce.org","threadId":"16463","inReplyTo":"loom.20081125T134459-650@post.gmane.org","subject":"Re: jgit, MutableInteger vs AtomicInteger","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-11-25T19:19:47Z","receivedAt":"2008-11-25T19:19:47Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Vasyl Vavrychuk <vvavrychuk@gmail.com> wrote:\n> I've just started browsing jgit sources and an obvious question arise.\n> I didn't found such question in mail list and decided to ask the community.\n> \n> I don't see reason behind creating own mutable integer container because we \n> have java.util.concurrent.atomic.AtomicInteger with methods\n>     public final int get()\n>     public final void set(int i)\n> \n> And that is what we want.\n\nEh, its sort-of what we want.\n\nThe MutableInteger used in jgit is used instead of this C construct:\n\n\tint x;\n\n\tcall_something(&x);\n\tcall_something_else(&x);\n\t... now use x ...\n\nIn other words it is stack allocated, passed into methods for them\nto read and/or update, and then later used in the caller.\n\nAtomicInteger does in fact have get and set methods, providing what\nwould seem to be the same operation.  But if you read the source\ncode you'll see that the get and set methods operate against a\nvolatile int.  IIRC the volatile tag requires that the JRE must\nnot cache the value in a register.\n\nIf you look again at how MutableInteger is used, we never touch the\nvalue outside of a single thread.  The volatile tag on the field\nisn't necessary.  It may actually make it harder for the JIT to\nproduce tight machine code.  Where we use MutableInteger is the\ndepths of revision traversal and commit parsing, it *must* be fast.\n\n\nAlso, there's the simple fact that I forgot about AtomicInteger,\nand it took me less time to code MutableInteger than it did to look\nto see if the JRE had something already.  ;-)\n\n-- \nShawn.\n"}]}