diff --git a/src/GlobalConfig.ts b/src/GlobalConfig.ts index e743abd397..bfd34253e9 100644 --- a/src/GlobalConfig.ts +++ b/src/GlobalConfig.ts @@ -8,19 +8,14 @@ const GlobalConfig = { //test env isTest: false, - idleEnable: true, //idle logging idleLog: false, - //idle 时间阈值 - idleTimeRemaining: 4, //idle 申请不到idle时,强制执行时间阈值 - idleForceTimeThreshold: 100, - //idle 超时阈值 - idleTimeout: 1000, + idleForceTimeThreshold: 48, //worker 数量 workerCount: (getGlobalThis().MAPTALKS_WORKER_COUNT) as number || 0, //每个Worker Message中封装的task message数量 - taskCountPerWorkerMessage: 5, + messagePostRatioPerWorker: 0.3, //当前运行环境的最大FPS,用户可以手动配置,否则将自动检测并赋值,为地图锁帧渲染准备 maxFPS: 0 }; diff --git a/src/core/MicroTask.ts b/src/core/MicroTask.ts index 6792f079d6..0b717ecaee 100644 --- a/src/core/MicroTask.ts +++ b/src/core/MicroTask.ts @@ -88,34 +88,20 @@ function executeMicroTasks() { } } -let broadcastIdleMessage = true; -function loop() { - if (broadcastIdleMessage) { - getGlobalWorkerPool().broadcastIdleMessage(); - } else { - getGlobalWorkerPool().commit(); - } +function loop(isBusy?: boolean) { + const messageRatio = GlobalConfig.messagePostRatioPerWorker * (isBusy ? 0.5 : 1); + getGlobalWorkerPool().commit(); + getGlobalWorkerPool().broadcastIdleMessage(messageRatio); executeMicroTasks(); - broadcastIdleMessage = !broadcastIdleMessage; loopHooks.forEach(func => { func(); }); } let idleCallTime = now(); -function idleFrameLoop(deadline) { - const { idleTimeRemaining, idleLog } = GlobalConfig; - if (deadline && deadline.timeRemaining) { - const t = deadline.timeRemaining(); - if (t >= idleTimeRemaining) { - loop(); - idleCallTime = now(); - } else { - if (t < idleTimeRemaining && idleLog) { - console.warn('currrent page is busy,the timeRemaining is', t); - } - } - } +function idleFrameLoop() { + loop(); + idleCallTime = now(); requestIdleCallback(idleFrameLoop); } @@ -124,7 +110,7 @@ function animFrameLoop() { const { idleForceTimeThreshold, idleLog } = GlobalConfig; const time = now(); if (time - idleCallTime > idleForceTimeThreshold) { - loop(); + loop(true); idleCallTime = now(); if (idleLog) { console.warn(`did not apply for availability, forced run idle`); diff --git a/src/core/worker/Worker.ts b/src/core/worker/Worker.ts index c77a9d0549..75b65db25a 100644 --- a/src/core/worker/Worker.ts +++ b/src/core/worker/Worker.ts @@ -51,7 +51,7 @@ const header = ` func(workerExports,self); adapters[key]=workerExports; workerExports.initialize && workerExports.initialize(self); - + } onmessage = function (msg) { msg = msg.data; @@ -65,8 +65,8 @@ const header = ` } // postMessage when main thread idle if(msg.messageType==='idle'){ - var messageCount = msg.messageCount||5; - handleMessageQueue(messageCount); + var messageRatio = msg.messageRatio; + handleMessageQueue(messageRatio); return; } if (msg.messageType === 'batch') { @@ -98,16 +98,17 @@ const header = ` } var messageResultQueue = []; - - function handleMessageQueue(messageCount){ + + function handleMessageQueue(messageRatio){ if(messageResultQueue.length===0){ return; } - var queues = messageResultQueue.slice(0,messageCount); + var count = Math.ceil((messageRatio || 1) * messageResultQueue.length); + var queues = messageResultQueue.slice(0, count); queues.forEach(function(queue){ post(queue.callback,queue.err,queue.data,queue.buffers); }); - messageResultQueue=messageResultQueue.slice(messageCount,Infinity); + messageResultQueue=messageResultQueue.slice(count, Infinity); } function post(callback, err, data, buffers) { diff --git a/src/core/worker/WorkerPool.ts b/src/core/worker/WorkerPool.ts index 0569338768..cfa0ac796c 100644 --- a/src/core/worker/WorkerPool.ts +++ b/src/core/worker/WorkerPool.ts @@ -120,10 +120,10 @@ export default class WorkerPool { return this.workers || []; } - broadcastIdleMessage() { + broadcastIdleMessage(messageRatio: number) { const workers = this.getWorkers(); workers.forEach(worker => { - worker.postMessage({ messageType: 'idle', messageCount: GlobalConfig.taskCountPerWorkerMessage }); + worker.postMessage({ messageType: 'idle', messageRatio }); }); return this; }