HBASE-16612 Use array to cache Types for KeyValue.Type.codeToType (Phil Yang)
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/981200bf Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/981200bf Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/981200bf Branch: refs/heads/hbase-12439 Commit: 981200bf1344e2c58559874cb7a66132f703efd6 Parents: a602aaf Author: tedyu <yuzhih...@gmail.com> Authored: Tue Sep 13 09:54:27 2016 -0700 Committer: tedyu <yuzhih...@gmail.com> Committed: Tue Sep 13 09:54:27 2016 -0700 ---------------------------------------------------------------------- .../main/java/org/apache/hadoop/hbase/KeyValue.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/981200bf/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java ---------------------------------------------------------------------- diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java index c1734cc..0c33a96 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java @@ -252,6 +252,14 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, return this.code; } + private static Type[] codeArray = new Type[256]; + + static { + for (Type t : Type.values()) { + codeArray[t.code & 0xff] = t; + } + } + /** * Cannot rely on enum ordinals . They change if item is removed or moved. * Do our own codes. @@ -259,10 +267,9 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, * @return Type associated with passed code. */ public static Type codeToType(final byte b) { - for (Type t : Type.values()) { - if (t.getCode() == b) { - return t; - } + Type t = codeArray[b & 0xff]; + if (t != null) { + return t; } throw new RuntimeException("Unknown code " + b); }