Skip to content

Commit

Permalink
fix: use expected size to initialize hash map instead of capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 13, 2022
1 parent 39b857f commit 8590e25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.alibaba.ttl3.internal.util.Utils.newHashMap;

/**
* {@link CompositeCrrTransmit} transmit all {@link CrrTransmit}
* registered by {@link #registerCrrTransmit(CrrTransmit)}.
Expand Down Expand Up @@ -41,7 +43,7 @@ public CompositeCrrTransmit(CompositeCrrTransmitCallback callback) {
*/
@NonNull
public Capture capture() {
final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = new HashMap<>(registeredCrrTransmitSet.size());
final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = newHashMap(registeredCrrTransmitSet.size());
for (CrrTransmit<Object, Object> crrTransmit : registeredCrrTransmitSet) {
try {
crrTransmit2Value.put(crrTransmit, crrTransmit.capture());
Expand All @@ -68,7 +70,7 @@ public Backup replay(@NonNull Capture captured) {
final Object data = callback.beforeReplay();

final Snapshot capturedSnapshot = (Snapshot) captured;
final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = new HashMap<>(capturedSnapshot.crrTransmit2Value.size());
final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = newHashMap(capturedSnapshot.crrTransmit2Value.size());
for (Map.Entry<CrrTransmit<Object, Object>, Object> entry : capturedSnapshot.crrTransmit2Value.entrySet()) {
CrrTransmit<Object, Object> crrTransmit = entry.getKey();
try {
Expand Down Expand Up @@ -105,7 +107,7 @@ public Backup replay(@NonNull Capture captured) {
public Backup clear() {
final Object data = callback.beforeReplay();

final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = new HashMap<>(registeredCrrTransmitSet.size());
final HashMap<CrrTransmit<Object, Object>, Object> crrTransmit2Value = newHashMap(registeredCrrTransmitSet.size());
for (CrrTransmit<Object, Object> crrTransmit : registeredCrrTransmitSet) {
try {
crrTransmit2Value.put(crrTransmit, crrTransmit.clear());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.WeakHashMap;
import java.util.function.Supplier;

import static com.alibaba.ttl3.internal.util.Utils.newHashMap;

/**
* {@link TransmittableThreadLocal}({@code TTL}) can transmit the value from the thread of submitting task
* to the thread of executing task even using thread pooling components.
Expand Down Expand Up @@ -330,7 +332,7 @@ private static class TtlTransmittee implements Transmittee<HashMap<Transmittable
@NonNull
@Override
public HashMap<TransmittableThreadLocal<Object>, Object> capture() {
final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value = new HashMap<>(holder.get().size());
final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value = newHashMap(holder.get().size());
for (TransmittableThreadLocal<Object> threadLocal : holder.get().keySet()) {
ttl2Value.put(threadLocal, threadLocal.getTransmitteeValue());
}
Expand All @@ -340,7 +342,7 @@ public HashMap<TransmittableThreadLocal<Object>, Object> capture() {
@NonNull
@Override
public HashMap<TransmittableThreadLocal<Object>, Object> replay(@NonNull HashMap<TransmittableThreadLocal<Object>, Object> captured) {
final HashMap<TransmittableThreadLocal<Object>, Object> backup = new HashMap<>(holder.get().size());
final HashMap<TransmittableThreadLocal<Object>, Object> backup = newHashMap(holder.get().size());

for (final Iterator<TransmittableThreadLocal<Object>> iterator = holder.get().keySet().iterator(); iterator.hasNext(); ) {
TransmittableThreadLocal<Object> threadLocal = iterator.next();
Expand All @@ -365,7 +367,7 @@ public HashMap<TransmittableThreadLocal<Object>, Object> replay(@NonNull HashMap
@NonNull
@Override
public HashMap<TransmittableThreadLocal<Object>, Object> clear() {
return replay(new HashMap<>(0));
return replay(newHashMap(0));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.WeakHashMap;
import java.util.logging.Logger;

import static com.alibaba.ttl3.internal.util.Utils.newHashMap;

/**
* {@code ThreadLocalTransmitRegistry}, {@code ThreadLocal} transmit integration.
* <p>
Expand Down Expand Up @@ -142,7 +144,7 @@ private static class ThreadLocalTransmittee implements Transmittee<HashMap<Threa
@NonNull
@Override
public HashMap<ThreadLocal<Object>, Object> capture() {
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value = new HashMap<>(threadLocalHolder.size());
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value = newHashMap(threadLocalHolder.size());
for (Map.Entry<ThreadLocal<Object>, TtlCopier<Object>> entry : threadLocalHolder.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
final TtlCopier<Object> copier = entry.getValue();
Expand All @@ -155,7 +157,7 @@ public HashMap<ThreadLocal<Object>, Object> capture() {
@NonNull
@Override
public HashMap<ThreadLocal<Object>, Object> replay(@NonNull HashMap<ThreadLocal<Object>, Object> captured) {
final HashMap<ThreadLocal<Object>, Object> backup = new HashMap<>(captured.size());
final HashMap<ThreadLocal<Object>, Object> backup = newHashMap(captured.size());

for (Map.Entry<ThreadLocal<Object>, Object> entry : captured.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
Expand All @@ -172,7 +174,7 @@ public HashMap<ThreadLocal<Object>, Object> replay(@NonNull HashMap<ThreadLocal<
@NonNull
@Override
public HashMap<ThreadLocal<Object>, Object> clear() {
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value = new HashMap<>(threadLocalHolder.size());
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value = newHashMap(threadLocalHolder.size());

for (Map.Entry<ThreadLocal<Object>, TtlCopier<Object>> entry : threadLocalHolder.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
Expand Down

0 comments on commit 8590e25

Please sign in to comment.