From a844013eb035caa623c8cdfadf9571b58471fdc1 Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 3 Aug 2024 14:24:15 +0800 Subject: [PATCH] perf[orm]: avoid resize --- orm/src/main/java/com/zfoo/orm/cache/EntityCache.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java index e910ec4e5..0dfdded38 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -222,9 +222,7 @@ public void persist(PK pk) { return; } pnode.resetTime(TimeUtils.currentTimeMillis()); - var updateList = new ArrayList(); - updateList.add(pnode.getEntity()); - doPersist(updateList); + doPersist(List.of(pnode.getEntity())); } @@ -239,13 +237,14 @@ public void persistAll() { var currentTime = TimeUtils.currentTimeMillis(); // key为threadId var updateMap = new HashMap>(); + var initSize = cache.size() >> 2; cache.forEach(new BiConsumer>() { @Override public void accept(PK pk, PNode pnode) { var entity = pnode.getEntity(); if (pnode.getModifiedTime() != pnode.getWriteToDbTime()) { pnode.resetTime(currentTime); - var updateList = updateMap.computeIfAbsent(pnode.getThreadId(), it -> new ArrayList<>()); + var updateList = updateMap.computeIfAbsent(pnode.getThreadId(), it -> new ArrayList<>(initSize)); updateList.add(entity); } } @@ -268,7 +267,7 @@ public void accept(PK pk, PNode pnode) { @Override public void persistAllBlock() { var currentTime = TimeUtils.currentTimeMillis(); - var updateList = new ArrayList(); + var updateList = new ArrayList(cache.size()); cache.forEach(new BiConsumer>() { @Override public void accept(PK pk, PNode pnode) {