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

 
 

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

Submitter:  Ernst Reissner <ernstreissner>
Submitted:  Wed 10 Aug 2016 02:45:37 PM UTC
   
 
Category:  Interpreter Severity:  2 - Minor
Priority:  3 - Low Item Group:  Performance
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

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: 
Mike Miller <mtmiller>
Group Member
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>
Group Member
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 Files:
   
   
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
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by ernstreissner (Submitted the item)
  • -email is unavailable- added by ernstreissner
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-08-12 ernstreissner Attached File- Added patchBug48750.patch, #38192
    2016-08-10 mtmiller StatusNeed Info Confirmed
    2016-08-10 mtmiller CategoryNone 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
    2016-08-10 ernstreissner Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code