Skip to content

Commit

Permalink
perf[orm]: avoid resize
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Aug 3, 2024
1 parent a455317 commit a844013
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions orm/src/main/java/com/zfoo/orm/cache/EntityCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ public void persist(PK pk) {
return;
}
pnode.resetTime(TimeUtils.currentTimeMillis());
var updateList = new ArrayList<E>();
updateList.add(pnode.getEntity());
doPersist(updateList);
doPersist(List.of(pnode.getEntity()));
}


Expand All @@ -239,13 +237,14 @@ public void persistAll() {
var currentTime = TimeUtils.currentTimeMillis();
// key为threadId
var updateMap = new HashMap<Long, List<E>>();
var initSize = cache.size() >> 2;
cache.forEach(new BiConsumer<PK, PNode<PK, E>>() {
@Override
public void accept(PK pk, PNode<PK, E> 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);
}
}
Expand All @@ -268,7 +267,7 @@ public void accept(PK pk, PNode<PK, E> pnode) {
@Override
public void persistAllBlock() {
var currentTime = TimeUtils.currentTimeMillis();
var updateList = new ArrayList<E>();
var updateList = new ArrayList<E>(cache.size());
cache.forEach(new BiConsumer<PK, PNode<PK, E>>() {
@Override
public void accept(PK pk, PNode<PK, E> pnode) {
Expand Down

0 comments on commit a844013

Please sign in to comment.