Skip to content

Commit

Permalink
[Java] refine Collection util data structure (#1287) (#1288)
Browse files Browse the repository at this point in the history
1. use local reference when possible
2. make tuple value field final

---------

Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
mof-dev-3 and [email protected] authored Jan 1, 2024
1 parent 14d9dbe commit 6a06d9f
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.fury.codegen.CodegenContext;
import org.apache.fury.codegen.Expression;
import org.apache.fury.codegen.ExpressionOptimizer;
import org.apache.fury.collection.Tuple3;
import org.apache.fury.collection.MutableTuple3;
import org.apache.fury.type.Descriptor;
import org.apache.fury.type.DescriptorGrouper;
import org.apache.fury.util.function.SerializableSupplier;
Expand Down Expand Up @@ -106,23 +106,25 @@ private void buildGroups() {
if (boxedRefTracking) {
boxedReadWeight = 4;
}
List<Tuple3<List<Descriptor>, Integer, List<List<Descriptor>>>> groups =
List<MutableTuple3<List<Descriptor>, Integer, List<List<Descriptor>>>> groups =
Arrays.asList(
Tuple3.of(
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getBoxedDescriptors()),
boxedWriteWeight,
boxedWriteGroups),
Tuple3.of(
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getBoxedDescriptors()),
boxedReadWeight,
boxedReadGroups),
Tuple3.of(
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getFinalDescriptors()), 9, finalWriteGroups),
Tuple3.of(new ArrayList<>(descriptorGrouper.getFinalDescriptors()), 5, finalReadGroups),
Tuple3.of(new ArrayList<>(descriptorGrouper.getOtherDescriptors()), 5, otherReadGroups),
Tuple3.of(
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getFinalDescriptors()), 5, finalReadGroups),
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getOtherDescriptors()), 5, otherReadGroups),
MutableTuple3.of(
new ArrayList<>(descriptorGrouper.getOtherDescriptors()), 9, otherWriteGroups));
for (Tuple3<List<Descriptor>, Integer, List<List<Descriptor>>> decs : groups) {
for (MutableTuple3<List<Descriptor>, Integer, List<List<Descriptor>>> decs : groups) {
while (decs.f0.size() > 0) {
int endIndex = Math.min(decs.f1, decs.f0.size());
decs.f2.add(decs.f0.subList(0, endIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private class MapIterator implements Iterator<Map.Entry<K, V>> {

@Override
public boolean hasNext() {
K[] keyTable = FuryObjectMap.this.keyTable;
for (int i = nextIndex; i < keyTable.length; i++) {
if (keyTable[i] != null) {
nextIndex = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,27 @@ public LazyMap(List<Entry<K, V>> entries) {

@Override
public Map<K, V> delegate() {
if (map == null) {
map = new HashMap<>(entries.size());
for (Entry<K, V> entry : entries) {
map.put(entry.getKey(), entry.getValue());
Map<K, V> m = this.map;
if (m == null) {
List<Entry<K, V>> e = this.entries;
m = new HashMap<>(e.size());
for (Entry<K, V> entry : e) {
m.put(entry.getKey(), entry.getValue());
}
this.map = m;
}
return map;
return m;
}

@Override
public V put(K key, V value) {
if (map == null) {
Map<K, V> m = map;
if (m == null) {
// avoid map put cost when deserialization this map.
entries.add(new MapEntry<>(key, value));
return null;
} else {
return map.put(key, value);
return m.put(key, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.collection;

import java.io.Serializable;
import java.util.Objects;

public class MutableTuple2<T0, T1> implements Serializable {
/** Field 0 of the tuple. */
public T0 f0;

/** Field 1 of the tuple. */
public T1 f1;

/**
* Creates a new tuple and assigns the given values to the tuple's fields, with field value
* nonFinal. Recommend use {@link Tuple2} if value do not need to change.
*
* @param value0 The value for field 0
* @param value1 The value for field 1
*/
public MutableTuple2(T0 value0, T1 value1) {
this.f0 = value0;
this.f1 = value1;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MutableTuple2<?, ?> tuple2 = (MutableTuple2<?, ?>) o;
return Objects.equals(f0, tuple2.f0) && Objects.equals(f1, tuple2.f1);
}

@Override
public int hashCode() {
return Objects.hash(f0, f1);
}

public static <T0, T1> MutableTuple2<T0, T1> of(T0 value0, T1 value1) {
return new MutableTuple2<>(value0, value1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.collection;

import java.io.Serializable;
import java.util.Objects;

public class MutableTuple3<T0, T1, T2> implements Serializable {
/** Field 0 of the tuple. */
public T0 f0;

/** Field 1 of the tuple. */
public T1 f1;

/** Field 2 of the tuple. */
public T2 f2;

/**
* Creates a new tuple and assigns the given values to the tuple's fields, with field value
* nonFinal. Recommend use {@link Tuple3} if value do not need to change
*
* @param value0 The value for field 0
* @param value1 The value for field 1
* @param value2 The value for field 2
*/
public MutableTuple3(T0 value0, T1 value1, T2 value2) {
this.f0 = value0;
this.f1 = value1;
this.f2 = value2;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MutableTuple3<?, ?, ?> tuple3 = (MutableTuple3<?, ?, ?>) o;
return Objects.equals(f0, tuple3.f0)
&& Objects.equals(f1, tuple3.f1)
&& Objects.equals(f2, tuple3.f2);
}

@Override
public int hashCode() {
return Objects.hash(f0, f1, f2);
}

public static <T0, T1, T2> MutableTuple3<T0, T1, T2> of(T0 value0, T1 value1, T2 value2) {
return new MutableTuple3<>(value0, value1, value2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public class Tuple2<T0, T1> implements Serializable {
private static final long serialVersionUID = 1L;

/** Field 0 of the tuple. */
public T0 f0;
public final T0 f0;

/** Field 1 of the tuple. */
public T1 f1;
public final T1 f1;

/** Creates a new tuple where all fields are null. */
public Tuple2() {}
public Tuple2() {
this(null, null);
}

/**
* Creates a new tuple and assigns the given values to the tuple's fields.
* Creates a new tuple and assigns the given values to the tuple's fields, with field value final.
* In case field value is nonFinal, use {@link MutableTuple2}
*
* @param value0 The value for field 0
* @param value1 The value for field 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ public class Tuple3<T0, T1, T2> implements Serializable {
private static final long serialVersionUID = 1L;

/** Field 0 of the tuple. */
public T0 f0;
public final T0 f0;

/** Field 1 of the tuple. */
public T1 f1;
public final T1 f1;

/** Field 1 of the tuple. */
public T2 f2;
/** Field 2 of the tuple. */
public final T2 f2;

/** Creates a new tuple where all fields are null. */
public Tuple3() {}
public Tuple3() {
this(null, null, null);
}

/**
* Creates a new tuple and assigns the given values to the tuple's fields.
* Creates a new tuple and assigns the given values to the tuple's fields, with field value final.
* In case field value is nonFinal, use {@link MutableTuple3}
*
* @param value0 The value for field 0
* @param value1 The value for field 1
Expand Down

0 comments on commit 6a06d9f

Please sign in to comment.