Re: Boolean valueOf instead of new Boolean

2014-05-27 Thread roger riggs

Hi Otávio,

I can sponsor these two  (Boolean and single char strings) for you.

Because they cross over different repositories and require different reviews
it would be more convenient to process each of them in two batches 
(client vs core)


Please can you break out the 'client' changes into a separate webrev?
The client packages are:  (java and javax...)
  java2d, awt, swing, font, print, beans, media, imageio, applet, sound 
and accessibility.


The client webrevs should be posted to awt-dev and the other two to 
core-libs, net-dev and servicability-dev.


Thanks, Roger


On 5/27/2014 1:16 PM, Otávio Gonçalves de Santana wrote:

Can anyone help me as sponsor?
On May 25, 2014 2:08 AM, "Otávio Gonçalves de Santana" 
wrote:






Re: Boolean valueOf instead of new Boolean

2014-05-27 Thread Otávio Gonçalves de Santana
Can anyone help me as sponsor?
On May 25, 2014 2:08 AM, "Otávio Gonçalves de Santana" 
wrote:

> Really Happy to hear that.
> Done.
>
>
> On Sat, May 24, 2014 at 5:10 PM, Andrej Golovnin <
> andrej.golov...@gmail.com> wrote:
>
>> Hi Otávio,
>>
>> it would be nice, if you would not modify the classes
>>  sun.reflect.UnsafeXXXFieldAccessorImpl.
>> This classes should be changed as a part of the fix for the issue
>> JDK-5043030.
>> The patch for this issue is already in work.
>>
>> Best regards,
>> Andrej Golovnin
>>
>> On 24.05.2014, at 16:34, Otávio Gonçalves de Santana 
>> wrote:
>>
>> > The Boolean class has cache for true and false and using it, will save
>> > memory and will faster than using create new instance of boolean.
>> > Using JMH[1] with a code test[2] the result was:
>> > Benchmark   Mode
>> Samples
>> >Mean  Mean errorUnits
>> > m.BooleanBenchmark.newInstanceBooleanthrpt20
>>  49801.326
>> > 369.897  ops/s
>> > m.BooleanBenchmark.newInstanceString   thrpt20
>> > 365.080   27.537ops/s
>> > m.BooleanBenchmark.valueOfBoolean   thrpt20
>>  764906233.316
>> > 9623009.653  ops/s
>> > m.BooleanBenchmark.valueOfString  thrpt20
>> > 371.174   28.216  ops/s
>> >
>> >
>> >
>> > The diff is on attachment or can is downloading the webdrev here:
>> >
>> https://dl.dropboxusercontent.com/u/16109193/open_jdk/boolean_instance_of.zip
>> >
>> > [1] http://openjdk.java.net/projects/code-tools/jmh/
>> >
>> > [2]
>> >
>> > @State(Scope.Thread)
>> >
>> > @OutputTimeUnit(TimeUnit.SECONDS)
>> >
>> > public class BooleanBenchmark {
>> >
>> > private static final int SIZE = 1_000_000;
>> >
>> > private List booleanString;
>> >
>> > private boolean[] booleans;
>> >
>> > {
>> >
>> > booleans = new boolean[SIZE];
>> >
>> > booleanString = new ArrayList<>(SIZE);
>> >
>> > for (int index = 0; index < SIZE; index++) {
>> >
>> > if (index % 2 == 0) {
>> >
>> > booleans[index] = true;
>> >
>> > booleanString.add(Boolean.TRUE.toString());
>> >
>> > } else {
>> >
>> > booleans[index] = false;
>> >
>> > booleanString.add(Boolean.FALSE.toString());
>> >
>> > }
>> >
>> > }
>> >
>> > }
>> >
>> > @GenerateMicroBenchmark
>> >
>> > public void valueOfBoolean() {
>> >
>> >
>> > for(boolean b: booleans) {
>> >
>> > Boolean result = b;
>> >
>> > }
>> >
>> > }
>> >
>> > @GenerateMicroBenchmark
>> >
>> > public void valueOfString() {
>> >
>> > for(String b: booleanString) {
>> >
>> > Boolean result = Boolean.valueOf(b);
>> >
>> > }
>> >
>> > }
>> >
>> > @GenerateMicroBenchmark
>> >
>> > public void newInstanceBoolean() {
>> >
>> >
>> > for(boolean b: booleans) {
>> >
>> > Boolean result = new Boolean(b);
>> >
>> > }
>> >
>> > }
>> >
>> > @GenerateMicroBenchmark
>> >
>> > public void newInstanceString() {
>> >
>> > for(String b: booleanString) {
>> >
>> > Boolean result = new Boolean(b);
>> >
>> > }
>> >
>> > }
>> >
>> > }
>> >
>> > --
>> > Atenciosamente.
>> >
>> > Otávio Gonçalves de Santana
>> >
>> > blog: http://otaviosantana.blogspot.com.br/
>> > twitter: http://twitter.com/otaviojava
>> > site: http://www.otaviojava.com.br
>> > (11) 98255-3513
>> > 
>>
>>
>
>
> --
> Atenciosamente.
>
> Otávio Gonçalves de Santana
>
> blog: http://otaviosantana.blogspot.com.br/
> twitter: http://twitter.com/otaviojava
> site: http://www.otaviojava.com.br
> (11) 98255-3513
>
>


Re: Boolean valueOf instead of new Boolean

2014-05-24 Thread Otávio Gonçalves de Santana
Really Happy to hear that.
Done.


On Sat, May 24, 2014 at 5:10 PM, Andrej Golovnin
wrote:

> Hi Otávio,
>
> it would be nice, if you would not modify the classes
>  sun.reflect.UnsafeXXXFieldAccessorImpl.
> This classes should be changed as a part of the fix for the issue
> JDK-5043030.
> The patch for this issue is already in work.
>
> Best regards,
> Andrej Golovnin
>
> On 24.05.2014, at 16:34, Otávio Gonçalves de Santana 
> wrote:
>
> > The Boolean class has cache for true and false and using it, will save
> > memory and will faster than using create new instance of boolean.
> > Using JMH[1] with a code test[2] the result was:
> > Benchmark   Mode
> Samples
> >Mean  Mean errorUnits
> > m.BooleanBenchmark.newInstanceBooleanthrpt20
>  49801.326
> > 369.897  ops/s
> > m.BooleanBenchmark.newInstanceString   thrpt20
> > 365.080   27.537ops/s
> > m.BooleanBenchmark.valueOfBoolean   thrpt20
>  764906233.316
> > 9623009.653  ops/s
> > m.BooleanBenchmark.valueOfString  thrpt20
> > 371.174   28.216  ops/s
> >
> >
> >
> > The diff is on attachment or can is downloading the webdrev here:
> >
> https://dl.dropboxusercontent.com/u/16109193/open_jdk/boolean_instance_of.zip
> >
> > [1] http://openjdk.java.net/projects/code-tools/jmh/
> >
> > [2]
> >
> > @State(Scope.Thread)
> >
> > @OutputTimeUnit(TimeUnit.SECONDS)
> >
> > public class BooleanBenchmark {
> >
> > private static final int SIZE = 1_000_000;
> >
> > private List booleanString;
> >
> > private boolean[] booleans;
> >
> > {
> >
> > booleans = new boolean[SIZE];
> >
> > booleanString = new ArrayList<>(SIZE);
> >
> > for (int index = 0; index < SIZE; index++) {
> >
> > if (index % 2 == 0) {
> >
> > booleans[index] = true;
> >
> > booleanString.add(Boolean.TRUE.toString());
> >
> > } else {
> >
> > booleans[index] = false;
> >
> > booleanString.add(Boolean.FALSE.toString());
> >
> > }
> >
> > }
> >
> > }
> >
> > @GenerateMicroBenchmark
> >
> > public void valueOfBoolean() {
> >
> >
> > for(boolean b: booleans) {
> >
> > Boolean result = b;
> >
> > }
> >
> > }
> >
> > @GenerateMicroBenchmark
> >
> > public void valueOfString() {
> >
> > for(String b: booleanString) {
> >
> > Boolean result = Boolean.valueOf(b);
> >
> > }
> >
> > }
> >
> > @GenerateMicroBenchmark
> >
> > public void newInstanceBoolean() {
> >
> >
> > for(boolean b: booleans) {
> >
> > Boolean result = new Boolean(b);
> >
> > }
> >
> > }
> >
> > @GenerateMicroBenchmark
> >
> > public void newInstanceString() {
> >
> > for(String b: booleanString) {
> >
> > Boolean result = new Boolean(b);
> >
> > }
> >
> > }
> >
> > }
> >
> > --
> > Atenciosamente.
> >
> > Otávio Gonçalves de Santana
> >
> > blog: http://otaviosantana.blogspot.com.br/
> > twitter: http://twitter.com/otaviojava
> > site: http://www.otaviojava.com.br
> > (11) 98255-3513
> > 
>
>


-- 
Atenciosamente.

Otávio Gonçalves de Santana

blog: http://otaviosantana.blogspot.com.br/
twitter: http://twitter.com/otaviojava
site: http://www.otaviojava.com.br
(11) 98255-3513
diff -r 28d1de89ff27 src/share/classes/java/net/Socket.java
--- a/src/share/classes/java/net/Socket.javaThu May 22 12:54:02 2014 -0700
+++ b/src/share/classes/java/net/Socket.javaSat May 24 10:55:43 2014 -0300
@@ -1017,7 +1017,7 @@
 if (isClosed())
 throw new SocketException("Socket is closed");
 if (!on) {
-getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
+getImpl().setOption(SocketOptions.SO_LINGER, on);
 } else {
 if (linger < 0) {
 throw new IllegalArgumentException("invalid value for 
SO_LINGER");
diff -r 28d1de89ff27 src/share/classes/sun/font/SunFontManager.java
--- a/src/share/classes/sun/font/SunFontManager.javaThu May 22 12:54:02 
2014 -0700
+++ b/src/share/classes/sun/font/SunFontManager.javaSat May 24 10:55:43 
2014 -0300
@@ -2871,8 +2871,7 @@
 new java.security.PrivilegedAction() {
 public Object run() {
 SecurityManager sm = System.getSecurityManager();
-return new Boolean
-(sm instanceof sun.applet.AppletSecurity);
+return sm instanceof sun.applet.AppletSecurity;
 }
 });
 return appletSM.booleanValue();
diff -r 28d1de89ff27 
src/share/classes/sun/management/StackTraceElementCompositeData.java
--- a/src/share/classes/sun/management/StackTraceElementCompositeData.java  
Thu May 22 12:54:02 2014 -0700
+++ b/src/share/classes/sun/management/StackTraceElementCompositeData.java  
Sat May 24 10:55:43 2014 -0300
@@ -68,7 +68,7 @@
 ste.getMethodName(),
 ste.getFileName(),
 new Integer(ste.getLineNumber()),
-  

Re: Boolean valueOf instead of new Boolean

2014-05-24 Thread Andrej Golovnin
Hi Otávio,

it would be nice, if you would not modify the classes  
sun.reflect.UnsafeXXXFieldAccessorImpl.
This classes should be changed as a part of the fix for the issue JDK-5043030.
The patch for this issue is already in work.

Best regards,
Andrej Golovnin

On 24.05.2014, at 16:34, Otávio Gonçalves de Santana  
wrote:

> The Boolean class has cache for true and false and using it, will save
> memory and will faster than using create new instance of boolean.
> Using JMH[1] with a code test[2] the result was:
> Benchmark   Mode   Samples
>Mean  Mean errorUnits
> m.BooleanBenchmark.newInstanceBooleanthrpt20  49801.326
> 369.897  ops/s
> m.BooleanBenchmark.newInstanceString   thrpt20
> 365.080   27.537ops/s
> m.BooleanBenchmark.valueOfBoolean   thrpt20764906233.316
> 9623009.653  ops/s
> m.BooleanBenchmark.valueOfString  thrpt20
> 371.174   28.216  ops/s
> 
> 
> 
> The diff is on attachment or can is downloading the webdrev here:
> https://dl.dropboxusercontent.com/u/16109193/open_jdk/boolean_instance_of.zip
> 
> [1] http://openjdk.java.net/projects/code-tools/jmh/
> 
> [2]
> 
> @State(Scope.Thread)
> 
> @OutputTimeUnit(TimeUnit.SECONDS)
> 
> public class BooleanBenchmark {
> 
> private static final int SIZE = 1_000_000;
> 
> private List booleanString;
> 
> private boolean[] booleans;
> 
> {
> 
> booleans = new boolean[SIZE];
> 
> booleanString = new ArrayList<>(SIZE);
> 
> for (int index = 0; index < SIZE; index++) {
> 
> if (index % 2 == 0) {
> 
> booleans[index] = true;
> 
> booleanString.add(Boolean.TRUE.toString());
> 
> } else {
> 
> booleans[index] = false;
> 
> booleanString.add(Boolean.FALSE.toString());
> 
> }
> 
> }
> 
> }
> 
> @GenerateMicroBenchmark
> 
> public void valueOfBoolean() {
> 
> 
> for(boolean b: booleans) {
> 
> Boolean result = b;
> 
> }
> 
> }
> 
> @GenerateMicroBenchmark
> 
> public void valueOfString() {
> 
> for(String b: booleanString) {
> 
> Boolean result = Boolean.valueOf(b);
> 
> }
> 
> }
> 
> @GenerateMicroBenchmark
> 
> public void newInstanceBoolean() {
> 
> 
> for(boolean b: booleans) {
> 
> Boolean result = new Boolean(b);
> 
> }
> 
> }
> 
> @GenerateMicroBenchmark
> 
> public void newInstanceString() {
> 
> for(String b: booleanString) {
> 
> Boolean result = new Boolean(b);
> 
> }
> 
> }
> 
> }
> 
> -- 
> Atenciosamente.
> 
> Otávio Gonçalves de Santana
> 
> blog: http://otaviosantana.blogspot.com.br/
> twitter: http://twitter.com/otaviojava
> site: http://www.otaviojava.com.br
> (11) 98255-3513
> 



Re: Boolean valueOf instead of new Boolean

2014-05-24 Thread Otávio Gonçalves de Santana
Hi Alexis.
Thank you for fixes the benchMarck.

About the class GSSManagerImpl, yes it is necessary because the
System.getProperty(USE_NATIVE_PROP) returns a String.


On Sat, May 24, 2014 at 12:19 PM, Aleksey Shipilev <
aleksey.shipi...@oracle.com> wrote:

> On 05/24/2014 06:34 PM, Otávio Gonçalves de Santana wrote:
> > The Boolean class has cache for true and false and using it, will save
> > memory and will faster than using create new instance of boolean.
> > Using JMH[1] with a code test[2] the result was:
>
> I agree Boolean.valueOf (whether explicit or implicit) should be used if
> identity is not required. Do you really need explicit Boolean.valueOf
> in, say, GSSManagerImpl, instead of relying on autoboxing?
>
> That said, your benchmark is not correct. At very least, you have to use
> explicit BlackHoles to avoid DCE [1][2]. This is how you do it:
>
> @State(Scope.Thread)
> @OutputTimeUnit(TimeUnit.SECONDS)
> public class BooleanBenchmark {
>
> @Param("100")
> private int size;
>
> private List booleanString;
> private boolean[] booleans;
>
> @Setup
> public void s() {
> booleans = new boolean[size];
> booleanString = new ArrayList(size);
>
> for (int index = 0; index < size; index++) {
> if (index % 2 == 0) {
> booleans[index] = true;
> booleanString.add(Boolean.TRUE.toString());
> } else {
> booleans[index] = false;
> booleanString.add(Boolean.FALSE.toString());
> }
> }
> }
>
> @GenerateMicroBenchmark
> public void valueOfBoolean(BlackHole bh) {
> for (boolean b : booleans) {
> Boolean result = b;
> bh.consume(result);
> }
> }
>
> @GenerateMicroBenchmark
> public void valueOfString(BlackHole bh) {
> for (String b : booleanString) {
> Boolean result = Boolean.valueOf(b);
> bh.consume(result);
> }
> }
>
> @GenerateMicroBenchmark
> public void newInstanceBoolean(BlackHole bh) {
> for (boolean b : booleans) {
> Boolean result = new Boolean(b);
> bh.consume(result);
> }
> }
>
> @GenerateMicroBenchmark
> public void newInstanceString(BlackHole bh) {
> for (String b : booleanString) {
> Boolean result = new Boolean(b);
> bh.consume(result);
> }
> }
> }
>
> Thanks,
> -Aleksey.
>
>
> [1]
>
> http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
> [2]
>
> http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_09_Blackholes.java
>
>


-- 
Atenciosamente.

Otávio Gonçalves de Santana

blog: http://otaviosantana.blogspot.com.br/
twitter: http://twitter.com/otaviojava
site: http://www.otaviojava.com.br
(11) 98255-3513


Re: Boolean valueOf instead of new Boolean

2014-05-24 Thread Aleksey Shipilev
On 05/24/2014 06:34 PM, Otávio Gonçalves de Santana wrote:
> The Boolean class has cache for true and false and using it, will save
> memory and will faster than using create new instance of boolean.
> Using JMH[1] with a code test[2] the result was:

I agree Boolean.valueOf (whether explicit or implicit) should be used if
identity is not required. Do you really need explicit Boolean.valueOf
in, say, GSSManagerImpl, instead of relying on autoboxing?

That said, your benchmark is not correct. At very least, you have to use
explicit BlackHoles to avoid DCE [1][2]. This is how you do it:

@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.SECONDS)
public class BooleanBenchmark {

@Param("100")
private int size;

private List booleanString;
private boolean[] booleans;

@Setup
public void s() {
booleans = new boolean[size];
booleanString = new ArrayList(size);

for (int index = 0; index < size; index++) {
if (index % 2 == 0) {
booleans[index] = true;
booleanString.add(Boolean.TRUE.toString());
} else {
booleans[index] = false;
booleanString.add(Boolean.FALSE.toString());
}
}
}

@GenerateMicroBenchmark
public void valueOfBoolean(BlackHole bh) {
for (boolean b : booleans) {
Boolean result = b;
bh.consume(result);
}
}

@GenerateMicroBenchmark
public void valueOfString(BlackHole bh) {
for (String b : booleanString) {
Boolean result = Boolean.valueOf(b);
bh.consume(result);
}
}

@GenerateMicroBenchmark
public void newInstanceBoolean(BlackHole bh) {
for (boolean b : booleans) {
Boolean result = new Boolean(b);
bh.consume(result);
}
}

@GenerateMicroBenchmark
public void newInstanceString(BlackHole bh) {
for (String b : booleanString) {
Boolean result = new Boolean(b);
bh.consume(result);
}
}
}

Thanks,
-Aleksey.


[1]
http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
[2]
http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_09_Blackholes.java