{"id":383,"date":"2009-07-08T22:17:13","date_gmt":"2009-07-09T02:17:13","guid":{"rendered":"https:\/\/lowtek.ca\/roo\/?p=383"},"modified":"2009-07-08T22:17:13","modified_gmt":"2009-07-09T02:17:13","slug":"writing-better-jni-code","status":"publish","type":"post","link":"https:\/\/lowtek.ca\/roo\/2009\/writing-better-jni-code\/","title":{"rendered":"Writing better JNI code"},"content":{"rendered":"<p>Well, it seems Ron beat me to the punch to <a href=\"http:\/\/ronald-servant.blogspot.com\/2009\/07\/avoiding-jni-pitfalls.html\">blog about this<\/a> &#8211; but allow me to promote the recently published <a href=\"http:\/\/www.ibm.com\/developerworks\">developerWorks<\/a> article that I had a hand in bringing to life:\u00a0 <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-jni\/index.html?ca=dgr-twtrJNI-Bestdth-j&amp;S_TACT=105AGY83&amp;S_CMP=TWDW\">Best practices for using the Java Native Interface<\/a><\/p>\n<p>Here is the summary:<\/p>\n<blockquote><p>The Java\u2122 Native Interface (JNI) is a standard Java API that enables Java code to integrate with code written in other programming languages. JNI can be a key element in your toolkit if you want to leverage existing code assets \u2014 for example, in a service-oriented architecture (SOA) or a cloud-based system. But when used without due care, JNI can quickly lead to poorly performing and unstable applications. This article identifies the top 10 JNI programming pitfalls, provides best practices for avoiding them, and introduces the tools available for implementing these practices.<\/p><\/blockquote>\n<p>Many <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kudos\">kudos<\/a> to Michael Dawson for his wordsmithing and persistence to make this article happen, he really deserves the lion&#8217;s share of the credit.<\/p>\n<p><a href=\"http:\/\/ronald-servant.blogspot.com\">Ron<\/a> calls out his favorite pitfall as <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-jni\/index.html?ca=dgr-twtrJNI-Bestdth-j&amp;S_TACT=105AGY83&amp;S_CMP=TWDW#wrong\">using the wrong JNIEnv<\/a>.\u00a0 I&#8217;ll pick the one that makes me laugh because it is sad but true:\u00a0 <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-jni\/index.html?ca=dgr-twtrJNI-Bestdth-j&amp;S_TACT=105AGY83&amp;S_CMP=TWDW#choosing\">Choosing the wrong boundary between native and Java code<\/a> &#8211; believe it or not there was a real world example that motivated the sample code.\u00a0 The customer had decided to go for a &#8220;pure java&#8221; solution for their embedded application, so the minimal amount of native code was written &#8211; two functions that could read or write individual bits (not bytes).\u00a0 While the example is outlandish, many times the integration of legacy code via JNI is done without thinking of the cost of the boundary.<\/p>\n<p>Let&#8217;s discuss the picking the right side of the JNI boundary for a minute.\u00a0 The <a href=\"http:\/\/en.wikipedia.org\/wiki\/New_I\/O\">NIO implementation<\/a> is a good example of trying to fix file performance by keeping the data on the &#8220;right&#8221; side of the JNI boundary.\u00a0 While NIO provides other functions allowing for tighter OS integration, one key performance win is due to the file data is kept in native buffers.\u00a0\u00a0 If you&#8217;re trying to get the data into java objects to be manipulated, you are likely defeating the performance win of NIO by forcing all that file data across the JNI boundary.\u00a0 Where NIO will excel is needing to pull a bunch of data from a file and put it in another file &#8211; if you don&#8217;t need to hoist that data up into the java object space, you&#8217;ll get close to pure native performance.<\/p>\n<p>Please <a href=\"http:\/\/www.ibm.com\/developerworks\/java\/library\/j-jni\/index.html?ca=dgr-twtrJNI-Bestdth-j&amp;S_TACT=105AGY83&amp;S_CMP=TWDW\">check out the article<\/a>, or at least bookmark it and send it to people who write JNI code.\u00a0\u00a0 Maybe we&#8217;ll see fewer false JVM bugs that turn out to be JNI related problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, it seems Ron beat me to the punch to blog about this &#8211; but allow me to promote the recently published developerWorks article that I had a hand in bringing to life:\u00a0 Best practices for using the Java Native Interface Here is the summary: The Java\u2122 Native Interface (JNI) is a standard Java API &hellip; <a href=\"https:\/\/lowtek.ca\/roo\/2009\/writing-better-jni-code\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Writing better JNI code&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-work"],"_links":{"self":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":1,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/posts\/383\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lowtek.ca\/roo\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}