1

It seems as though double-brace initialization increases overhead.

Does using braces inside of a method also reduce performance?

eg.

public class DoIReducePerformanceToo {

    public void aMethod() {

        {
           // Is it a bad idea to use these?
        }

    }

}

I've taken a look at Java's grammar and it seems that this is classified as a block:

Block: 
    { BlockStatements }

BlockStatements: 
    { BlockStatement }

BlockStatement:
    LocalVariableDeclarationStatement
    ClassOrInterfaceDeclaration
    [Identifier :] Statement

but I'm not sure where in the grammar double-brace initialization falls.

My question: does using block statements in methods reduce performance in Java? And are these blocks of the same nature as double-brace initialization?

EDIT:

Inner class instantation is:

ClassCreatorRest: Arguments [ClassBody]

ClassBody: 
    { { ClassBodyDeclaration } }
Community
  • 1
  • 1
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 1
    It's not syntactically the braces that affect performance but what that means. In that situation it means create a new class and instantiate it. In this situation, it doesn't mean anything and it will be removed as dead code. – Esailija Jul 09 '13 at 21:58

3 Answers3

6

The double-brace initialization trick has nothing to do with normal scopes.

Instead, it creates an anonymous class that inherits the type your initializing, and runs your code in an initialization block (which is syntactic sugar for a constructor).

This extra class has overhead.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks! Would you be able to point me to the location in the grammar where double-brace initialization is defined? – sdasdadas Jul 09 '13 at 21:58
  • @sdasdadas: `ClassCreatorRest: Arguments [ClassBody]` – SLaks Jul 09 '13 at 21:59
  • 1
    It isn't, really; the double-brace initialization technique is just a normal initializer block inside a normal anonymous class. – Louis Wasserman Jul 09 '13 at 21:59
  • @LouisWasserman I found the regular class declaration but couldn't quite find the inner class declaration. :) – sdasdadas Jul 09 '13 at 22:02
  • @SLaks this isn't answering the OP's question, is it? I thought it was about a block statement, not about inline initializer blocks... – feralin Jul 09 '13 at 22:06
  • @feralin You answered why a block statement is the same speed as no block statement (which answers the question too). I was mainly concerned, however, that block statements were somehow tied to instantiating a class (which, in retrospect, seems silly). – sdasdadas Jul 09 '13 at 22:11
3

The block syntax is a part of the grammar, but also changes things such as variable scope. However, after compilation, variables, syntax, scope, and all is just converted into a plain bytecode format. The bytecode does not care about scoping rules, and the like, so there should be no overhead to using extra blocks in your code.

For example, the code

void something()
{
    int x = 5;
    randomStuffWithInt(x);
    {
        int x = 10;
        somethingWithInt(x);
    }
}

could be converted (alpha conversion) to

void something()
{
    int x = 5;
    randomStuffWithInt(x);
    int y = 10;
    somethingWithInt(y);
}

At runtime, it should be exactly the same speed.

feralin
  • 3,268
  • 3
  • 21
  • 37
1

Late answer, but... I believe there will actually be an execution impact (though not necessarily a large one) given that the run-time engine will create a local variable symbol table on the stack to house variables within the block. If this is done in a looping context or in code otherwise requiring high speed, it may be advisable to avoid its usage.