Author: edwardyoon
Date: Thu Sep 27 08:20:32 2012
New Revision: 1390883

URL: http://svn.apache.org/viewvc?rev=1390883&view=rev
Log:
Minor optimization

Modified:
    
hama/trunk/core/src/main/java/org/apache/hama/bsp/message/HadoopMessageManagerImpl.java
    hama/trunk/core/src/main/java/org/apache/hama/util/CompressionUtil.java

Modified: 
hama/trunk/core/src/main/java/org/apache/hama/bsp/message/HadoopMessageManagerImpl.java
URL: 
http://svn.apache.org/viewvc/hama/trunk/core/src/main/java/org/apache/hama/bsp/message/HadoopMessageManagerImpl.java?rev=1390883&r1=1390882&r2=1390883&view=diff
==============================================================================
--- 
hama/trunk/core/src/main/java/org/apache/hama/bsp/message/HadoopMessageManagerImpl.java
 (original)
+++ 
hama/trunk/core/src/main/java/org/apache/hama/bsp/message/HadoopMessageManagerImpl.java
 Thu Sep 27 08:20:32 2012
@@ -101,14 +101,17 @@ public final class HadoopMessageManagerI
       throw new IllegalArgumentException("Can not find " + addr.toString()
           + " to transfer messages to!");
     } else {
-      if (compressor != null
-          && CompressionUtil.getBundleSize(bundle) > conf.getLong(
-              "hama.messenger.compression.threshold", 1048576)) {
-        BSPCompressedBundle compMsgBundle = compressor.compressBundle(bundle);
-        if (CompressionUtil.getCompressionRatio(compMsgBundle, bundle) < 1.0) {
-          bspPeerConnection.put(compMsgBundle);
-        } else {
-          bspPeerConnection.put(bundle);
+      if (compressor != null) {
+        float bundleSize = CompressionUtil.getBundleSize(bundle);
+        if (CompressionUtil.getBundleSize(bundle) > conf.getLong(
+            "hama.messenger.compression.threshold", 1048576)) {
+          BSPCompressedBundle compMsgBundle = 
compressor.compressBundle(bundle);
+          if (CompressionUtil.getCompressionRatio(
+              (float) compMsgBundle.getData().length, bundleSize) < 1.0) {
+            bspPeerConnection.put(compMsgBundle);
+          } else {
+            bspPeerConnection.put(bundle);
+          }
         }
       } else {
         bspPeerConnection.put(bundle);

Modified: 
hama/trunk/core/src/main/java/org/apache/hama/util/CompressionUtil.java
URL: 
http://svn.apache.org/viewvc/hama/trunk/core/src/main/java/org/apache/hama/util/CompressionUtil.java?rev=1390883&r1=1390882&r2=1390883&view=diff
==============================================================================
--- hama/trunk/core/src/main/java/org/apache/hama/util/CompressionUtil.java 
(original)
+++ hama/trunk/core/src/main/java/org/apache/hama/util/CompressionUtil.java Thu 
Sep 27 08:20:32 2012
@@ -22,7 +22,6 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 
 import org.apache.hama.bsp.BSPMessageBundle;
-import org.apache.hama.bsp.message.compress.BSPCompressedBundle;
 
 public class CompressionUtil {
 
@@ -30,35 +29,22 @@ public class CompressionUtil {
    * Calculates the compression ratio. A compression ratio of less than 1 is
    * desirable.
    * 
-   * @param compMsgBundle
-   * @param bundle
-   * @return
+   * @param compressedSize
+   * @param bundleSize
+   * @return the compression ratio
    * @throws IOException
    */
-  public static float getCompressionRatio(BSPCompressedBundle compMsgBundle,
-      BSPMessageBundle<?> bundle) throws IOException {
-
-    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    DataOutputStream dos = new DataOutputStream(bos);
-    bundle.write(dos);
-
-    dos.close();
-    bos.close();
-
-    float compLen = compMsgBundle.getData().length;
-
-    return (compLen / bos.toByteArray().length);
+  public static float getCompressionRatio(float compressedSize, float 
bundleSize)
+      throws IOException {
+    return (compressedSize / bundleSize);
   }
 
-  public static long getBundleSize(BSPMessageBundle<?> bundle)
+  public static float getBundleSize(BSPMessageBundle<?> bundle)
       throws IOException {
-    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    DataOutputStream dos = new DataOutputStream(bos);
+    DataOutputStream dos = new DataOutputStream(new ByteArrayOutputStream());
     bundle.write(dos);
-
     dos.close();
-    bos.close();
 
-    return bos.toByteArray().length;
+    return dos.size();
   }
 }


Reply via email to