Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Extended-Precision Native Integers for Java


Dr. Dobb's Journal November 1997: Sheng's Optimization Trick

Dr. Dobb's Journal November 1997

Sheng's Optimization Trick


Accessing an object's field via the JNI typically requires a sequence of four API calls. In my first attempt to access the array of integers that hold the value of a mInt, the last call, GetIntArrayElements(), failed. After some debugging, I knew the first three calls were succeeding, so why was GetIntArrayElements() failing with a "JNI scalar array element type mismatch" error? Sun's Sheng Liang answered the question, showing me an error in the third call after all. That call, GetObjectField(), requires the object's handle as its second parameter, and I was accidentally passing a class handle. Instead of detecting that an incorrect type of handle was being passed, the JVM used it and returned a wildly wrong value, which I used as input to the fourth call, making it fail.

While JNI is an improvement over NMI, it still has some problems: It doesn't typecheck handles on JNI calls, so the JVM is prone to the same kind of API errors that plagued Windows programs in the past.

The performance trick Sheng showed me is simple: You can do the first two calls of this infamous four-call sequence once (when the native library is loaded), and cache the output of the second call, which is the fieldID of the field in question (see the cacheFID() call in Listing One). Then, perform calls three and four of the sequence in each method. (You really only need one call in cacheFID(): A static code member gets the class's handle as a parameter, so you don't need an explicit call to retrieve it.) You can safely cache and reuse this value because the fieldID for a given class and field never changes for any instance of that class. The second call in the sequence, GetFieldID(), is expensive, so caching it more than doubles the performance of the Add() method from 25,000 to 55,555 operations per second on a 133-MHz Pentium under Windows 95 using the JVM from Sun's JDK 1.1.1. If you want to run your own tests, you can run AddTime.java (also available electronically), which compares calling the methods Add() and testAdd(). testAdd() is simply a noncached version of Add().

-- L.G.


Copyright © 1997, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.