-
Notifications
You must be signed in to change notification settings - Fork 68
Always return PrimitiveFloatList, even when cold #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FelixGV
merged 1 commit into
linkedin:master
from
FelixGV:always_return_primitive_float_list_even_when_cold
May 29, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
avro-fastserde/src/main/java/com/linkedin/avro/api/PrimitiveFloatList.java
This file contains hidden or 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,20 @@ | ||
| package com.linkedin.avro.api; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A {@link List} implementation with additional functions to prevent boxing. | ||
| */ | ||
| public interface PrimitiveFloatList extends List<Float> { | ||
| /** | ||
| * @param index index of the element to return | ||
| * @return the element at the specified position in this list | ||
| */ | ||
| float getPrimitive(int index); | ||
|
|
||
| /** | ||
| * @param e element whose presence in this collection is to be ensured | ||
| * @return <tt>true</tt> if this collection changed as a result of the call | ||
| */ | ||
| boolean addPrimitive(float e); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
...fastserde/src/main/java/com/linkedin/avro/fastserde/coldstart/ColdPrimitiveFloatList.java
This file contains hidden or 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,31 @@ | ||
| package com.linkedin.avro.fastserde.coldstart; | ||
|
|
||
| import com.linkedin.avro.api.PrimitiveFloatList; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericData; | ||
|
|
||
| /** | ||
| * A {@link PrimitiveFloatList} implementation which is equivalent in all respect to the vanilla Avro | ||
| * implementation, both in terms of functionality and (lack of) performance. It provides the primitive | ||
| * API that the interface requires, but actually just returns an unboxed Float Object, thus providing | ||
| * no GC benefit. This should be possible to improve upon in the future, however. | ||
| * | ||
| * The main motivation for this class is merely to provide a guarantee that the extended API is always | ||
| * available, even when Fast-Avro isn't warmed up yet. | ||
| */ | ||
| public class ColdPrimitiveFloatList extends GenericData.Array<Float> implements PrimitiveFloatList { | ||
FelixGV marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final Schema SCHEMA = Schema.createArray(Schema.create(Schema.Type.FLOAT)); | ||
| public ColdPrimitiveFloatList(int capacity) { | ||
| super(capacity, SCHEMA); | ||
| } | ||
|
|
||
| @Override | ||
| public float getPrimitive(int index) { | ||
| return get(index); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean addPrimitive(float o) { | ||
| return add(o); | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
avro-fastserde/src/main/java/org/apache/avro/generic/ColdDatumReaderMixIn.java
This file contains hidden or 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,29 @@ | ||
| package org.apache.avro.generic; | ||
|
|
||
| import com.linkedin.avro.fastserde.coldstart.ColdPrimitiveFloatList; | ||
| import java.util.Collection; | ||
| import org.apache.avro.Schema; | ||
|
|
||
|
|
||
| /** | ||
| * An interface with default implementation in order to defeat the lack of multiple inheritance. | ||
| */ | ||
| public interface ColdDatumReaderMixIn { | ||
| default Object newArray(Object old, int size, Schema schema, NewArrayFunction fallBackFunction) { | ||
| switch (schema.getElementType().getType()) { | ||
| case FLOAT: | ||
| if (null == old || !(old instanceof ColdPrimitiveFloatList)) { | ||
| return new ColdPrimitiveFloatList(size); | ||
| } | ||
| ((Collection) old).clear(); | ||
| return old; | ||
| // TODO: Add more cases when we support more primitive array types | ||
| default: | ||
| return fallBackFunction.newArray(old, size, schema); | ||
| } | ||
| } | ||
|
|
||
| interface NewArrayFunction { | ||
| Object newArray(Object old, int size, Schema schema); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
avro-fastserde/src/main/java/org/apache/avro/generic/ColdGenericDatumReader.java
This file contains hidden or 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,21 @@ | ||
| package org.apache.avro.generic; | ||
|
|
||
| import org.apache.avro.Schema; | ||
|
|
||
|
|
||
| /** | ||
| * A light-weight extension of {@link GenericDatumReader} which merely ensures that the types of the | ||
| * extended API are always returned. | ||
| * | ||
| * This class needs to be in the org.apache.avro.generic package in order to access protected methods. | ||
| */ | ||
| public class ColdGenericDatumReader<T> extends GenericDatumReader<T> implements ColdDatumReaderMixIn { | ||
| public ColdGenericDatumReader(Schema writerSchema, Schema readerSchema) { | ||
| super(writerSchema, readerSchema); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object newArray(Object old, int size, Schema schema) { | ||
| return newArray(old, size, schema, super::newArray); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
avro-fastserde/src/main/java/org/apache/avro/generic/ColdSpecificDatumReader.java
This file contains hidden or 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,22 @@ | ||
| package org.apache.avro.generic; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.specific.SpecificDatumReader; | ||
|
|
||
|
|
||
| /** | ||
| * A light-weight extension of {@link SpecificDatumReader} which merely ensures that the types of | ||
| * the extended API are always returned. | ||
| * | ||
| * This class needs to be in the org.apache.avro.generic package in order to access protected methods. | ||
| */ | ||
| public class ColdSpecificDatumReader<T> extends SpecificDatumReader<T> implements ColdDatumReaderMixIn { | ||
| public ColdSpecificDatumReader(Schema writerSchema, Schema readerSchema) { | ||
| super(writerSchema, readerSchema); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object newArray(Object old, int size, Schema schema) { | ||
| return newArray(old, size, schema, super::newArray); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.