-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize enum field deserialization (#499)
BUG=FI-13182 Closes #302 ClassValue lazily caches result of Enum#values for the JVM to avoid unnecessary memory overhead. odp download --event-id 1094224 * com.linkedin.followfeed.partitioner.graph.ContentQualityClassification[].values() Total Allocation: 20.5G (3.58%)
- Loading branch information
Showing
4 changed files
with
88 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
helper/helper/src/main/java/com/linkedin/avroutil1/Enums.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2023 LinkedIn Corp. | ||
* Licensed under the BSD 2-Clause License (the "License"). | ||
* See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.avroutil1; | ||
|
||
/** | ||
* Consists exclusively of static methods that operate on or return {@link Enum}s. | ||
*/ | ||
public class Enums { | ||
private static final ClassValue<Enum<?>[]> ENUM_CONSTANTS = new ClassValue<Enum<?>[]>() { | ||
@Override | ||
protected Enum<?>[] computeValue(Class<?> type) { | ||
// JVM creates array everytime invoked. | ||
return (Enum<?>[]) type.getEnumConstants(); | ||
} | ||
}; | ||
|
||
private Enums() { | ||
} // utility | ||
|
||
/** | ||
* Returns shared enum constant for specified ordinal. | ||
* | ||
* Lazily caches result of Enum#values to avoid unnecessary memory overhead. | ||
*/ | ||
public static <E extends Enum<?>> E getConstant(Class<E> type, int ordinal) { | ||
return (E) ENUM_CONSTANTS.get(type)[ordinal]; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
helper/helper/src/test/java/com/linkedin/avroutil1/EnumsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2023 LinkedIn Corp. | ||
* Licensed under the BSD 2-Clause License (the "License"). | ||
* See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.avroutil1; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class EnumsTest { | ||
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) | ||
public void testGetEnumConstantWithNegativeOrdinal() { | ||
Enums.getConstant(Thread.State.class, -1); | ||
} | ||
|
||
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class) | ||
public void testGetEnumConstantWithLargeOrdinal() { | ||
Enums.getConstant(Thread.State.class, Thread.State.values().length); | ||
} | ||
|
||
@Test | ||
public void testGetEnumConstant() { | ||
for (Thread.State state : Thread.State.values()) { | ||
Assert.assertEquals(Enums.getConstant(Thread.State.class, state.ordinal()), state); | ||
} | ||
} | ||
} |