Stupid performance trick in Java
The High Performance & Scalability Zone is supported by Joyent, the birthplace of node.js and the cloud providers that helped LinkedIn scale to 1B pages a month. Learn from their DTrace experts or see how Joyent's SmartMachines scale so rapidly and efficiently.
ByteBuffer has a compact() method which is useful for re-using a buffer
if it is not full consumed. However, in the likely case it is consumed,
it can perform surprising badly.
while (in.read(buf) >= 0 || buf.position != 0) {
buf.flip();
out.write(buf);
buf.compact(); // In case of partial write
}
In my application, the throughput increased by 6% by replacing compact() with this method.
public static void compact(ByteBuffer bb) {
if (bb.remaining() == 0)
bb.clear();
else
bb.compact();
}
Note: this is not the increase in a micro-benchmark, but across the entire application!
Your Mileage May Vary of course
From http://vanillajava.blogspot.com/2011/11/stupid-performance-trick-in-java.html
Tags:
Published at DZone with permission of Peter Lawrey, author and DZone MVB.(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Scalability and better performance are constant concerns for the developer and operations manager. Joyent is a company dedicated to mitigating these concerns and they are the supporters of the High Performance & Scalability Zone. Their key product is the SmartDataCenter—a cloud IaaS built for real-time production with 100% data resiliency, superb security, and excellent monitoring and tuning capabilities. Learn from their DTrace experts and see how Joyent's SmartMachines scale so rapidly and efficiently





Comments
John David replied on Wed, 2012/01/25 - 7:09pm
Also another way to improve your java performance is to specify you buffer size at the start and hard code it in your code according to your requirements.
also keep in mind the memory size of your deployment server or machine.
new java