bugGNU Octave - Bugs: bug #48750, java interface: performance of...

 
 

bug #48750: java interface: performance of wrapping fundamental scalar types

Submitted by:  Ernst Reissner <ernstreissner>
Submitted on:  Wed 10 Aug 2016 02:45:37 PM UTC  
 
Category: InterpreterSeverity: 2 - Minor
Priority: 3 - LowItem Group: Performance
Status: ConfirmedAssigned to: None
Originator Name: Open/Closed: Open
Release: devOperating System: Any

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Fri 12 Aug 2016 09:48:54 PM UTC, comment #5:

fixed.

(file #38192)

Ernst Reissner <ernstreissner>
Wed 10 Aug 2016 03:44:06 PM UTC, comment #4:

Thank you, got it.

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(int)

Mike Miller <mtmiller>
Project Administrator
Wed 10 Aug 2016 03:18:22 PM UTC, comment #3:

see also
http://pmd.sourceforge.net/pmd-4.3.0/rules/migrating.html

e.g.

IntegerInstantiation

Since: PMD 3.5

In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly.

This rule is defined by the following XPath expression:

//PrimaryPrefix
/AllocationExpression
[not (ArrayDimsAndInits)
and (ClassOrInterfaceType/@Image='Integer'
or ClassOrInterfaceType/@Image='java.lang.Integer')]

Example:

public class Foo {
private Integer i = new Integer(0); // change to Integer i = Integer.valueOf(0);
}

Ernst Reissner <ernstreissner>
Wed 10 Aug 2016 03:15:45 PM UTC, comment #2:

Yes, first sentence is correct.
Second one I wonder because I read:

ublic static Integer valueOf(int i)

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Parameters:
i - an int value.
Returns:
an Integer instance representing i.
Since:
1.5

meaning that numbers in range -127,128 are cached always,
i.e. never lead to object creation when invoked in that range.
The should sentence means that normally,
a new object is created only if an equivalent one is not yet cached.

See also

http://stackoverflow.com/questions/2974561/new-integer-vs-valueof

on boxing with reference to jls specification.
There explicitly equality of two integers means that they are the same.
Normally, valueOf is what is used for boxing/unboxing.

By the way, here we see, that we shall not talk about boxing
when we just mean conversion java-octave.

Ernst Reissner <ernstreissner>
Wed 10 Aug 2016 02:57:47 PM UTC, comment #1:

Thanks, so this is a memory optimization issue, correct? I don't see anything obvious in the Java API docs about object caching or such, but maybe it's somewhere else. Would be nice to point to a reference showing the justification for this change.

Mike Miller <mtmiller>
Project Administrator
Wed 10 Aug 2016 02:45:37 PM UTC, original submission:

In ov-java.cc method
static bool
unbox (JNIEnv *jni_env, const octave_value& val, jobject_ref& jobj,
jclass_ref& jcls)

There is a macro for conversion of primitive scalars.
This is in itself not so good.
Also, the JVM is forced to create objects.
If I have values 0 then each conversion creates a new one,
putting load on the gc.
It is bad style to do so.
Instead of new Integer(0), write Integer.valueOf(0)
and let the JVM decide whether to invoke a constructor or not.

Ernst Reissner <ernstreissner>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

Attached Files
file #38192:  patchBug48750.patch added by ernstreissner (6KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by ernstreissner (Submitted the item)
  • -unavailable- added by ernstreissner
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 9 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 12 Aug 2016 09:48:54 PM UTCernstreissnerAttached File-=>Added patchBug48750.patch, #38192
    Wed 10 Aug 2016 03:44:06 PM UTCmtmillerStatusNeed Info=>Confirmed
    Wed 10 Aug 2016 02:57:47 PM UTCmtmillerCategoryNone=>Interpreter
      Severity3 - Normal=>2 - Minor
      Priority5 - Normal=>3 - Low
      Item GroupNone=>Performance
      StatusNone=>Need Info
      Summaryjava interface: unboxing scalars: macros and object creation=>java interface: performance of wrapping fundamental scalar types
    Wed 10 Aug 2016 02:45:37 PM UTCernstreissnerCarbon-Copy-=>Added -unavailable-

    Back to the top


    Powered by Savane 3.1-cleanup1