<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.E-MailFormatvorlage17
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.E-MailFormatvorlage18
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=DE link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Proposal: Optimizing Efficiency using Read-only Arrays<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>TL;DR: Read-only Arrays will improve speed, reduce memory and power consumption, provide security by default, and make programming and reviews easier and quicker.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Looking at the profile of any average real-world application, it is apparent that a lot of memory activity stems from allocating byte arrays.<o:p></o:p></p><p class=MsoNormal>Byte arrays are a core building block of several APIs in OpenJDK.<o:p></o:p></p><p class=MsoNormal>Just to name two of them: First and foremost Strings, as they are ubiquitous, but also I/O, as byte arrays are the buckets which carry all data through any InputStream/OutputStream.<o:p></o:p></p><p class=MsoNormal>While I was authoring several java.io optimizations in the past months, the latter became the driver for me write down this propsal.<o:p></o:p></p><p class=MsoNormal>Nevertheless, the proposal is focusing on a general solution, applicable to all Java APIs, beyond I/O.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>To perform any I/O in Java, all data MUST pass one or multiple byte arrays, each and every day.<o:p></o:p></p><p class=MsoNormal>As it is easy to imagine, we can easily talk about multiple Gigabytes per day for an average server product.<o:p></o:p></p><p class=MsoNormal>Once this array reference is passed to a custom method, it leaves the safe harbor of the JDK while entering possibly evil outside world - it becomes compromised.<o:p></o:p></p><p class=MsoNormal>The called custom method ("Mr Evil") could either read privata data sitting in the array beyond passed read lower and upper limits, or could write poisoned data into the passed array, picked up afterwards by the JDK code (hence is treated as "safe" data).<o:p></o:p></p><p class=MsoNormal>To mitigate these risks, typically byte arrays are duplicated (at least within limits) before forwarded to the outer world, so the "evil" receiver will only see a temporary / trimmed copy of the array.<o:p></o:p></p><p class=MsoNormal>Just due to that single safety means alone, each day tens of thousands of Java servers are squandering precious memory and power, producing considerable amounts of carbon dioxide in turn.<o:p></o:p></p><p class=MsoNormal>While copying buffers is effective, it also is inefficient.<o:p></o:p></p><p class=MsoNormal>"Inefficiency" is definitively not a term we want Java to be recognized as in the age of climate change.<o:p></o:p></p><p class=MsoNormal>N.B.: As soon as we omit explicit creation of an array copy, either due to a human programming fault, or due to an unexpected technical failure, security is ineffective! Hence relying on explicit copies is also a suboptimal ("flaky") safety means. Due to that risk, reviews of I/O code often become complex, lenghty and exhausting, making them rather expensive.<o:p></o:p></p><p class=MsoNormal>This is just one single example. You could easily find lots more in the JDK.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>If the Java language would have a means to mark arrays as "read-only" to the Compiler / JVM (just like it alrady has for final variables), then no more need for an explicit copy exists.<o:p></o:p></p><p class=MsoNormal>Several benefits would arise from the fact that no copy of the array is created (and removed) in turn:<o:p></o:p></p><p class=MsoNormal>* Speed is improved. While System.arraycopy() is quick, not calling it at all is quicker.<o:p></o:p></p><p class=MsoNormal>* GC pressure is reduced. While it might be low already, not creating a copy of an array makes it zero.<o:p></o:p></p><p class=MsoNormal>* Security by default. As the JVM cannot write "read-only" arrays, there is no harm when an explicit copy is omitted.<o:p></o:p></p><p class=MsoNormal>* Reduced memory consumption. No copy at all means literally zero additional memory.<o:p></o:p></p><p class=MsoNormal>* Reduced power consumption. No power to invest into squandered CPU cycles.<o:p></o:p></p><p class=MsoNormal>* Easier programming. No need to remind explicit creation of copies.<o:p></o:p></p><p class=MsoNormal>* Simpler code. No copies means no code to create them, making the reminder simpler to understand.<o:p></o:p></p><p class=MsoNormal>* Quicker reviews. Reviewer does not have to take care to check for compromised buffers, which is easily forgotten.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>While each single effect might be small, remind that all these effects will happen all together at once, and are massively applied each and every day, as arrays are building blocks of the JDK.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>To sum up, I'd like to propose to add a means to the Java language which turns arrays into "read-only" arrays.<o:p></o:p></p></div></body></html>