From 8705e28f60f767d4938223ec1b8b972374dad010 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 25 Jun 2024 14:50:23 +0800 Subject: [PATCH] perf[orm]: unsafe collection persist batch size --- .../java/com/zfoo/orm/cache/EntityCache.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 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 30fda047..30b527cb 100644 --- a/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java +++ b/orm/src/main/java/com/zfoo/orm/cache/EntityCache.java @@ -47,6 +47,7 @@ public class EntityCache, E extends IEntity> imple private static final Logger logger = LoggerFactory.getLogger(EntityCache.class); private static final int BATCH_SIZE = 512; + private static final int UNSAFE_COLLECTION_BATCH_SIZE = BATCH_SIZE / Runtime.getRuntime().availableProcessors(); private final EntityDef entityDef; @@ -230,15 +231,13 @@ public class EntityCache, E extends IEntity> imple if (pnode == null) { return; } - @SuppressWarnings("unchecked") - var entityClass = (Class) entityDef.getClazz(); if (pnode.getModifiedTime() == pnode.getWriteToDbTime()) { return; } pnode.resetTime(TimeUtils.currentTimeMillis()); var updateList = new ArrayList(); updateList.add(pnode.getEntity()); - doPersist(updateList, entityClass); + doPersist(updateList, BATCH_SIZE); } // 游戏中80%都是执行更新的操作,这样做会极大的提高更新速度 @@ -246,9 +245,6 @@ public class EntityCache, E extends IEntity> imple // 没有并发问题的entity还是在异步线程池Event慢慢更新,有并发问题的entity才放到原来的update线程去更新(第一次update会记录entity所在线程) @Override public void persistAll() { - @SuppressWarnings("unchecked") - var entityClass = (Class) entityDef.getClazz(); - var currentTime = TimeUtils.currentTimeMillis(); if (entityDef.hasUnsafeCollection()) { @@ -265,14 +261,14 @@ public class EntityCache, E extends IEntity> imple } } }); - for(var entry : updateMap.entrySet()) { + for (var entry : updateMap.entrySet()) { var threadId = entry.getKey(); var updateList = entry.getValue(); var executor = ThreadUtils.executorByThreadId(threadId); if (executor == null) { - EventBus.asyncExecute(entityClass.hashCode(), () -> doPersist(updateList, entityClass)); + EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList, BATCH_SIZE)); } else { - executor.execute(() -> doPersist(updateList, entityClass)); + executor.execute(() -> doPersist(updateList, UNSAFE_COLLECTION_BATCH_SIZE)); } } } else { @@ -287,17 +283,20 @@ public class EntityCache, E extends IEntity> imple } } }); - EventBus.asyncExecute(entityClass.hashCode(), () -> doPersist(updateList, entityClass)); + EventBus.asyncExecute(entityDef.getClazz().hashCode(), () -> doPersist(updateList, BATCH_SIZE)); } } - private void doPersist(List updateList, Class entityClass) { + private void doPersist(List updateList, int batchSize) { // 执行更新 if (updateList.isEmpty()) { return; } - var page = Page.valueOf(1, BATCH_SIZE, updateList.size()); + @SuppressWarnings("unchecked") + var entityClass = (Class) entityDef.getClazz(); + + var page = Page.valueOf(1, batchSize, updateList.size()); var maxPageSize = page.totalPage(); for (var currentPage = 1; currentPage <= maxPageSize; currentPage++) {