This is an automated email from the ASF dual-hosted git repository.

william pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/orc.git


The following commit(s) were added to refs/heads/main by this push:
     new 08aaebc37 ORC-1710: [Java] Reduce enum array allocation
08aaebc37 is described below

commit 08aaebc371927e6bb9a0f19c7cc90478200e3b6f
Author: sullis <[email protected]>
AuthorDate: Thu May 9 23:45:44 2024 -0700

    ORC-1710: [Java] Reduce enum array allocation
    
    ### What changes were proposed in this pull request?
    
    reduce Java enum array allocation by caching the result of .values() method.
    
    ### Why are the changes needed?
    
    This change reduces the number of object allocations.
    
    Reference:
    
https://www.gamlor.info/wordpress/2017/08/javas-enum-values-hidden-allocations/
    
    ### How was this patch tested?
    
    GitHub Actions.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No.
    
    ### Similar work
    
    A similar change has already been merged in the Apache Iceberg project
    https://github.com/apache/iceberg/pull/10126
    
    A similar change has already been merged in the Apache Pinot project
    https://github.com/apache/pinot/pull/12579
    
    A similar change has already been merged in the Apache NiFi project
    https://github.com/apache/nifi/pull/8679
    
    Closes #1926 from sullis/ORC-1710.
    
    Authored-by: sullis <[email protected]>
    Signed-off-by: William Hyun <[email protected]>
---
 java/core/src/java/org/apache/orc/impl/ParserUtils.java | 4 +++-
 java/core/src/java/org/apache/orc/impl/ReaderImpl.java  | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/java/core/src/java/org/apache/orc/impl/ParserUtils.java 
b/java/core/src/java/org/apache/orc/impl/ParserUtils.java
index df2f8b5e1..c864465bd 100644
--- a/java/core/src/java/org/apache/orc/impl/ParserUtils.java
+++ b/java/core/src/java/org/apache/orc/impl/ParserUtils.java
@@ -31,6 +31,8 @@ import java.util.List;
 import java.util.regex.Pattern;
 
 public class ParserUtils {
+  private static final TypeDescription.Category[] 
TYPE_DESCRIPTION_CATEGORY_VALUES
+      = TypeDescription.Category.values();
 
   static TypeDescription.Category parseCategory(ParserUtils.StringPosition 
source) {
     StringBuilder word = new StringBuilder();
@@ -56,7 +58,7 @@ public class ParserUtils {
       catString = catString.trim();
     }
     if (!catString.isEmpty()) {
-      for (TypeDescription.Category cat : TypeDescription.Category.values()) {
+      for (TypeDescription.Category cat : TYPE_DESCRIPTION_CATEGORY_VALUES) {
         if (cat.getName().equals(catString)) {
           return cat;
         }
diff --git a/java/core/src/java/org/apache/orc/impl/ReaderImpl.java 
b/java/core/src/java/org/apache/orc/impl/ReaderImpl.java
index 3afbff5fc..9e018157f 100644
--- a/java/core/src/java/org/apache/orc/impl/ReaderImpl.java
+++ b/java/core/src/java/org/apache/orc/impl/ReaderImpl.java
@@ -65,6 +65,10 @@ public class ReaderImpl implements Reader {
 
   private static final Logger LOG = LoggerFactory.getLogger(ReaderImpl.class);
 
+  private static final OrcFile.Version[] ORC_FILE_VERSION_VALUES = 
OrcFile.Version.values();
+  private static final OrcFile.WriterVersion[] ORC_FILE_WRITER_VERSION_VALUES
+      = OrcFile.WriterVersion.values();
+
   private static final int DIRECTORY_SIZE_GUESS = 16 * 1024;
   public static final int DEFAULT_COMPRESSION_BLOCK_SIZE = 256 * 1024;
 
@@ -268,7 +272,7 @@ public class ReaderImpl implements Reader {
     if (versionList == null || versionList.isEmpty()) {
       return OrcFile.Version.V_0_11;
     }
-    for (OrcFile.Version version: OrcFile.Version.values()) {
+    for (OrcFile.Version version: ORC_FILE_VERSION_VALUES) {
       if (version.getMajor() == versionList.get(0) &&
           version.getMinor() == versionList.get(1)) {
         return version;
@@ -620,7 +624,7 @@ public class ReaderImpl implements Reader {
    * @return the version of the software that produced the file
    */
   public static OrcFile.WriterVersion getWriterVersion(int writerVersion) {
-    for(OrcFile.WriterVersion version: OrcFile.WriterVersion.values()) {
+    for(OrcFile.WriterVersion version: ORC_FILE_WRITER_VERSION_VALUES) {
       if (version.getId() == writerVersion) {
         return version;
       }

Reply via email to