Skip to content
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

chore: reduce the object creation within readFlat #704

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import com.netflix.hollow.core.write.HollowListWriteRecord;
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteRecord;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordOrdinalReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordWriter;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalListNode;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalNode;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -126,14 +125,15 @@ protected Object parseHollowRecord(HollowRecord record) {
}

@Override
protected Object parseFlatRecord(FlatRecordTraversalNode node) {
List<Object> collection = new ArrayList<>();

for (FlatRecordTraversalNode elementNode : (FlatRecordTraversalListNode) node) {
Object element = elementMapper.parseFlatRecord(elementNode);
protected Object parseFlatRecord(FlatRecordOrdinalReader reader, int ordinal) {
FlatRecordOrdinalReader.Offset offset = reader.getOffsetAtDataStartOf(ordinal);
int size = reader.readSize(offset);
List<Object> collection = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
int elementOrdinal = reader.readListElementOrdinal(offset);
Object element = elementMapper.parseFlatRecord(reader, elementOrdinal);
collection.add(element);
}

return collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
import com.netflix.hollow.core.write.HollowTypeWriteState;
import com.netflix.hollow.core.write.HollowWriteRecord;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordOrdinalReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordWriter;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalMapNode;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalNode;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -140,13 +139,18 @@ protected Object parseHollowRecord(HollowRecord record) {
}

@Override
protected Object parseFlatRecord(FlatRecordTraversalNode node) {
FlatRecordTraversalMapNode mapNode = (FlatRecordTraversalMapNode) node;
Map<Object, Object> collection = new HashMap<>();

for (Map.Entry<FlatRecordTraversalNode, FlatRecordTraversalNode> entry : mapNode.entrySet()) {
Object key = keyMapper.parseFlatRecord(entry.getKey());
Object value = valueMapper.parseFlatRecord(entry.getValue());
protected Object parseFlatRecord(FlatRecordOrdinalReader reader, int ordinal) {
FlatRecordOrdinalReader.Offset offset = reader.getOffsetAtDataStartOf(ordinal);
int size = reader.readSize(offset);
Map<Object, Object> collection = new HashMap<>(size);
int previousKeyOrdinal = 0;
for (int i = 0; i < size; i++) {
long keyAndValueOrdinals = reader.readMapKeyAndValueOrdinals(offset, previousKeyOrdinal);
previousKeyOrdinal = (int) (keyAndValueOrdinals >>> 32);
int valueOrdinal = (int) keyAndValueOrdinals;

Object key = keyMapper.parseFlatRecord(reader, previousKeyOrdinal);
Object value = valueMapper.parseFlatRecord(reader, valueOrdinal);
collection.put(key, value);
}
return collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecord;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordOrdinalReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordReader;
import com.netflix.hollow.core.write.objectmapper.flatrecords.FlatRecordWriter;
import com.netflix.hollow.core.write.objectmapper.flatrecords.traversal.FlatRecordTraversalNode;
Expand Down Expand Up @@ -100,13 +101,20 @@ public <T> T readFlat(FlatRecordTraversalNode node) {
if (typeMapper == null) {
throw new IllegalArgumentException("No type mapper found for schema " + schemaName);
}
Object obj = typeMapper.parseFlatRecord(node);
Object obj = typeMapper.parseFlatRecord(node.getReader(), node.getOrdinal());
return (T) obj;
}

public <T> T readFlat(FlatRecord record) {
FlatRecordTraversalNode node = new FlatRecordTraversalObjectNode(record);
return readFlat(node);
FlatRecordOrdinalReader reader = new FlatRecordOrdinalReader(record);
int ordinal = reader.getOrdinalCount() - 1;
HollowSchema schema = reader.readSchema(ordinal);
HollowTypeMapper typeMapper = typeMappers.get(schema.getName());
if (typeMapper == null) {
throw new IllegalArgumentException("No type mapper found for schema " + schema.getName());
}
Object obj = typeMapper.parseFlatRecord(reader, ordinal);
return (T) obj;
}

/**
Expand Down
Loading
Loading