Skip to content

Commit

Permalink
refactor(java): Remove Guava's Collection usages (#1611)
Browse files Browse the repository at this point in the history
## What does this PR do?

Remove Guava's Collection usages

## Related issues

<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->
#1113

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

Lazy map become slightly more lazy =)

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
Munoon authored May 8, 2024
1 parent 8b3fe0e commit a87b902
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import static org.apache.fury.type.TypeUtils.isPrimitive;
import static org.apache.fury.util.Preconditions.checkArgument;

import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -450,11 +449,7 @@ protected Expression writeForNotNullNonFinalObject(
buffer,
inputObject));
return invokeGenerated(
ctx,
ImmutableSet.of(buffer, inputObject),
writeClassAndObject,
"writeClassAndObject",
false);
ctx, ofHashSet(buffer, inputObject), writeClassAndObject, "writeClassAndObject", false);
}

/**
Expand Down Expand Up @@ -666,7 +661,7 @@ protected Expression serializeForCollection(
serializer =
invokeGenerated(
ctx,
ImmutableSet.of(buffer, collection),
ofHashSet(buffer, collection),
writeClassAction,
"writeCollectionClassInfo",
false);
Expand All @@ -685,7 +680,7 @@ protected Expression serializeForCollection(
actions.add(write);
if (generateNewMethod) {
return invokeGenerated(
ctx, ImmutableSet.of(buffer, collection, serializer), actions, "writeCollection", false);
ctx, ofHashSet(buffer, collection, serializer), actions, "writeCollection", false);
}
return actions;
}
Expand Down Expand Up @@ -970,7 +965,7 @@ protected Expression serializeForMap(
// Spit this into a separate method to avoid method too big to inline.
serializer =
invokeGenerated(
ctx, ImmutableSet.of(buffer, map), writeClassAction, "writeMapClassInfo", false);
ctx, ofHashSet(buffer, map), writeClassAction, "writeMapClassInfo", false);
}
} else if (!AbstractMapSerializer.class.isAssignableFrom(serializer.type().getRawType())) {
serializer = new Cast(serializer, TypeRef.of(AbstractMapSerializer.class), "mapSerializer");
Expand All @@ -981,7 +976,7 @@ protected Expression serializeForMap(
jitWriteMap(buffer, map, serializer, typeRef),
new Invoke(serializer, "write", buffer, map));
if (generateNewMethod) {
return invokeGenerated(ctx, ImmutableSet.of(buffer, map), write, "writeMap", false);
return invokeGenerated(ctx, ofHashSet(buffer, map), write, "writeMap", false);
}
return write;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.fury.codegen.Code.LiteralValue.FalseLiteral;
import static org.apache.fury.codegen.Expression.Invoke.inlineInvoke;
import static org.apache.fury.codegen.ExpressionUtils.add;
import static org.apache.fury.collection.Collections.ofHashSet;
import static org.apache.fury.type.TypeUtils.OBJECT_ARRAY_TYPE;
import static org.apache.fury.type.TypeUtils.OBJECT_TYPE;
import static org.apache.fury.type.TypeUtils.PRIMITIVE_BYTE_ARRAY_TYPE;
Expand All @@ -32,8 +33,6 @@
import static org.apache.fury.type.TypeUtils.getSizeOfPrimitiveType;
import static org.apache.fury.type.TypeUtils.isPrimitive;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -162,17 +161,12 @@ public Expression buildEncodeExpression() {
}
expressions.addAll(serializePrimitives(bean, buffer, objectCodecOptimizer.primitiveGroups));
int numGroups = getNumGroups(objectCodecOptimizer);
for (List<Descriptor> group :
Iterables.concat(
objectCodecOptimizer.boxedWriteGroups,
objectCodecOptimizer.finalWriteGroups,
objectCodecOptimizer.otherWriteGroups)) {
if (group.isEmpty()) {
continue;
}
boolean inline = group.size() == 1 && numGroups < 10;
expressions.add(serializeGroup(group, bean, buffer, inline));
}
addGroupExpressions(
objectCodecOptimizer.boxedWriteGroups, numGroups, expressions, bean, buffer);
addGroupExpressions(
objectCodecOptimizer.finalWriteGroups, numGroups, expressions, bean, buffer);
addGroupExpressions(
objectCodecOptimizer.otherWriteGroups, numGroups, expressions, bean, buffer);
for (Descriptor descriptor :
objectCodecOptimizer.descriptorGrouper.getCollectionDescriptors()) {
expressions.add(serializeGroup(Collections.singletonList(descriptor), bean, buffer, false));
Expand All @@ -183,6 +177,21 @@ public Expression buildEncodeExpression() {
return expressions;
}

private void addGroupExpressions(
List<List<Descriptor>> writeGroup,
int numGroups,
ListExpression expressions,
Expression bean,
Reference buffer) {
for (List<Descriptor> group : writeGroup) {
if (group.isEmpty()) {
continue;
}
boolean inline = group.size() == 1 && numGroups < 10;
expressions.add(serializeGroup(group, bean, buffer, inline));
}
}

private int getNumGroups(ObjectCodecOptimizer objectCodecOptimizer) {
return objectCodecOptimizer.boxedWriteGroups.size()
+ objectCodecOptimizer.finalWriteGroups.size()
Expand Down Expand Up @@ -291,7 +300,7 @@ private List<Expression> serializePrimitivesUnCompressed(
} else {
expressions.add(
objectCodecOptimizer.invokeGenerated(
ImmutableSet.of(bean, base, writerAddr), groupExpressions, "writeFields"));
ofHashSet(bean, base, writerAddr), groupExpressions, "writeFields"));
}
}
Expression increaseWriterIndex =
Expand Down Expand Up @@ -397,7 +406,7 @@ private List<Expression> serializePrimitivesCompressed(
} else {
expressions.add(
objectCodecOptimizer.invokeGenerated(
ImmutableSet.of(bean, buffer, base), groupExpressions, "writeFields"));
ofHashSet(bean, buffer, base), groupExpressions, "writeFields"));
}
}
return expressions;
Expand Down Expand Up @@ -445,17 +454,12 @@ public Expression buildDecodeExpression() {
}
expressions.addAll(deserializePrimitives(bean, buffer, objectCodecOptimizer.primitiveGroups));
int numGroups = getNumGroups(objectCodecOptimizer);
for (List<Descriptor> group :
Iterables.concat(
objectCodecOptimizer.boxedReadGroups,
objectCodecOptimizer.finalReadGroups,
objectCodecOptimizer.otherReadGroups)) {
if (group.isEmpty()) {
continue;
}
boolean inline = group.size() == 1 && numGroups < 10;
expressions.add(deserializeGroup(group, bean, buffer, inline));
}
deserializeReadGroup(
objectCodecOptimizer.boxedReadGroups, numGroups, expressions, bean, buffer);
deserializeReadGroup(
objectCodecOptimizer.finalReadGroups, numGroups, expressions, bean, buffer);
deserializeReadGroup(
objectCodecOptimizer.otherReadGroups, numGroups, expressions, bean, buffer);
for (Descriptor d : objectCodecOptimizer.descriptorGrouper.getCollectionDescriptors()) {
expressions.add(deserializeGroup(Collections.singletonList(d), bean, buffer, false));
}
Expand All @@ -481,6 +485,21 @@ public Expression buildDecodeExpression() {
return expressions;
}

private void deserializeReadGroup(
List<List<Descriptor>> readGroups,
int numGroups,
ListExpression expressions,
Expression bean,
Reference buffer) {
for (List<Descriptor> group : readGroups) {
if (group.isEmpty()) {
continue;
}
boolean inline = group.size() == 1 && numGroups < 10;
expressions.add(deserializeGroup(group, bean, buffer, inline));
}
}

protected Expression buildComponentsArray() {
return new StaticInvoke(
Platform.class, "copyObjectArray", OBJECT_ARRAY_TYPE, recordComponentDefaultValues);
Expand Down Expand Up @@ -653,7 +672,7 @@ private List<Expression> deserializeUnCompressedPrimitives(
} else {
expressions.add(
objectCodecOptimizer.invokeGenerated(
ImmutableSet.of(bean, heapBuffer, readerAddr), groupExpressions, "readFields"));
ofHashSet(bean, heapBuffer, readerAddr), groupExpressions, "readFields"));
}
}
Expression increaseReaderIndex =
Expand Down Expand Up @@ -742,7 +761,7 @@ private List<Expression> deserializeCompressedPrimitives(
} else {
expressions.add(
objectCodecOptimizer.invokeGenerated(
ImmutableSet.of(bean, buffer, heapBuffer), groupExpressions, "readFields"));
ofHashSet(bean, buffer, heapBuffer), groupExpressions, "readFields"));
}
}
return expressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

package org.apache.fury.codegen;

import static java.util.Collections.unmodifiableSet;
import static org.apache.fury.codegen.Code.ExprCode;
import static org.apache.fury.codegen.CodeGenerator.alignIndent;
import static org.apache.fury.codegen.CodeGenerator.indent;
import static org.apache.fury.type.TypeUtils.getArrayType;
import static org.apache.fury.type.TypeUtils.getRawType;

import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -114,7 +114,7 @@ public class CodegenContext {
"true",
"false",
"null"));
JAVA_RESERVED_WORDS = ImmutableSet.copyOf(JAVA_RESERVED_WORDS);
JAVA_RESERVED_WORDS = unmodifiableSet(JAVA_RESERVED_WORDS);
}

private static Map<String, Map<String, Boolean>> nameConflicts = new ConcurrentHashMap<>();
Expand Down
Loading

0 comments on commit a87b902

Please sign in to comment.