瀏覽代碼

HADOOP-1094. Optimize the generated Writable implementations for records to not allocate a new BinaryOutputArchive or BinaryInputArchive per call. Contributed by Milind.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@521913 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 年之前
父節點
當前提交
dd1b795008

+ 4 - 0
CHANGES.txt

@@ -27,6 +27,10 @@ Trunk (unreleased changes)
  8. HADOOP-1137.  Fix StatusHttpServer to work correctly when
     resources are in a jar file.  (Benjamin Reed via cutting)
 
+ 9. HADOOP-1094.  Optimize generated Writable implementations for
+    records to not allocate a new BinaryOutputArchive or
+    BinaryInputArchive per call.  (Milind Bhandarkar via cutting)
+
 
 Release 0.12.2 - 2007-23-17
 

+ 24 - 1
src/java/org/apache/hadoop/record/BinaryRecordInput.java

@@ -29,7 +29,7 @@ import java.io.InputStream;
  */
 public class BinaryRecordInput implements RecordInput {
     
-    final private DataInput in;
+    private DataInput in;
     
     static private class BinaryIndex implements Index {
         private int nelems;
@@ -44,6 +44,29 @@ public class BinaryRecordInput implements RecordInput {
         }
     }
     
+    private BinaryRecordInput() {}
+    
+    private void setDataInput(DataInput inp) {
+      this.in = inp;
+    }
+    
+    private static ThreadLocal bIn = new ThreadLocal() {
+      protected synchronized Object initialValue() {
+        return new BinaryRecordInput();
+      }
+    };
+    
+    /**
+     * Get a thread-local record input for the supplied DataInput.
+     * @param inp data input stream
+     * @return binary record input corresponding to the supplied DataInput.
+     */
+    public static BinaryRecordInput get(DataInput inp) {
+      BinaryRecordInput bin = (BinaryRecordInput) bIn.get();
+      bin.setDataInput(inp);
+      return bin;
+    }
+    
     /** Creates a new instance of BinaryRecordInput */
     public BinaryRecordInput(InputStream strm) {
         this.in = new DataInputStream(strm);

+ 23 - 0
src/java/org/apache/hadoop/record/BinaryRecordOutput.java

@@ -33,6 +33,29 @@ public class BinaryRecordOutput implements RecordOutput {
     
     private DataOutput out;
     
+    private BinaryRecordOutput() {}
+    
+    private void setDataOutput(DataOutput out) {
+      this.out = out;
+    }
+    
+    private static ThreadLocal bOut = new ThreadLocal() {
+      protected synchronized Object initialValue() {
+        return new BinaryRecordOutput();
+      }
+    };
+    
+    /**
+     * Get a thread-local record output for the supplied DataOutput.
+     * @param out data output stream
+     * @return binary record output corresponding to the supplied DataOutput.
+     */
+    public static BinaryRecordOutput get(DataOutput out) {
+      BinaryRecordOutput bout = (BinaryRecordOutput) bOut.get();
+      bout.setDataOutput(out);
+      return bout;
+    }
+    
     /** Creates a new instance of BinaryRecordOutput */
     public BinaryRecordOutput(OutputStream out) {
         this.out = new DataOutputStream(out);

+ 0 - 44
src/java/org/apache/hadoop/record/InputArchive.java

@@ -1,44 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.record;
-
-import java.io.IOException;
-
-/**
- * Interface that all the Deserializers have to implement.
- *
- * @author Milind Bhandarkar
- */
-public interface InputArchive {
-    public byte readByte(String tag) throws IOException;
-    public boolean readBool(String tag) throws IOException;
-    public int readInt(String tag) throws IOException;
-    public long readLong(String tag) throws IOException;
-    public float readFloat(String tag) throws IOException;
-    public double readDouble(String tag) throws IOException;
-    public String readString(String tag) throws IOException;
-    public Buffer readBuffer(String tag) throws IOException;
-    public void readRecord(Record r, String tag) throws IOException;
-    public void startRecord(String tag) throws IOException;
-    public void endRecord(String tag) throws IOException;
-    public Index startVector(String tag) throws IOException;
-    public void endVector(String tag) throws IOException;
-    public Index startMap(String tag) throws IOException;
-    public void endMap(String tag) throws IOException;
-}

+ 0 - 48
src/java/org/apache/hadoop/record/OutputArchive.java

@@ -1,48 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.record;
-
-import java.io.IOException;
-import java.util.TreeMap;
-import java.util.ArrayList;
-
-/**
- * Interface that alll the serializers have to implement.
- *
- * @author Milind Bhandarkar
- */
-public interface OutputArchive {
-    public void writeByte(byte b, String tag) throws IOException;
-    public void writeBool(boolean b, String tag) throws IOException;
-    public void writeInt(int i, String tag) throws IOException;
-    public void writeLong(long l, String tag) throws IOException;
-    public void writeFloat(float f, String tag) throws IOException;
-    public void writeDouble(double d, String tag) throws IOException;
-    public void writeString(String s, String tag) throws IOException;
-    public void writeBuffer(Buffer buf, String tag)
-        throws IOException;
-    public void writeRecord(Record r, String tag) throws IOException;
-    public void startRecord(Record r, String tag) throws IOException;
-    public void endRecord(Record r, String tag) throws IOException;
-    public void startVector(ArrayList v, String tag) throws IOException;
-    public void endVector(ArrayList v, String tag) throws IOException;
-    public void startMap(TreeMap v, String tag) throws IOException;
-    public void endMap(TreeMap v, String tag) throws IOException;
-
-}

+ 2 - 2
src/java/org/apache/hadoop/record/Record.java

@@ -67,13 +67,13 @@ public abstract class Record implements WritableComparable, Cloneable {
   
   // inherit javadoc
   public void write(final DataOutput out) throws java.io.IOException {
-    BinaryRecordOutput bout = new BinaryRecordOutput(out);
+    BinaryRecordOutput bout = BinaryRecordOutput.get(out);
     this.serialize(bout);
   }
   
   // inherit javadoc
   public void readFields(final DataInput din) throws java.io.IOException {
-    BinaryRecordInput rin = new BinaryRecordInput(din);
+    BinaryRecordInput rin = BinaryRecordInput.get(din);
     this.deserialize(rin);
   }
 }