Skip to content

Commit

Permalink
Replace loopForTime with flat loops in hot functions. Seems to slig…
Browse files Browse the repository at this point in the history
…htly ease GC pressure.
  • Loading branch information
fenomas committed Apr 6, 2023
1 parent 3f123fc commit e2182da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
15 changes: 0 additions & 15 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ export function removeUnorderedListItem(list, item) {



// loop over a function for a few ms, or until it returns true
export function loopForTime(maxTimeInMS, callback, startTime) {
var t0 = startTime || performance.now()
var res = callback()
if (res) return
var t1 = performance.now(), dt = t1 - startTime
// tweak time to make the average delay equal to the desired amt
var cutoff = t0 + maxTimeInMS - dt / 2
if (t1 > cutoff) return
var maxIter = 1000 // sanity check
for (var i = 0; i < maxIter; i++) {
if (callback() || performance.now() > cutoff) return
}
}




Expand Down
37 changes: 21 additions & 16 deletions src/lib/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import EventEmitter from 'events'
import { Chunk } from './chunk'
import { LocationQueue, ChunkStorage, locationHasher, loopForTime } from './util'
import { LocationQueue, ChunkStorage, locationHasher } from './util'

var PROFILE_EVERY = 0 // ticks
var PROFILE_QUEUES_EVERY = 0 // ticks
Expand Down Expand Up @@ -404,26 +404,28 @@ World.prototype.tick = function () {
findChunksToMesh(this)

// process (create or mesh) some chunks, up to max iteration time
var ptime = Math.max(1, this.maxProcessingPerTick || 0)
var t = performance.now()
var t1 = t + Math.max(1, this.maxProcessingPerTick || 0)
var done1 = false
var done2 = false
var done3 = false
loopForTime(ptime, () => {
while (t < t1) {
if (!done1) {
done1 = processRequestQueue(this)
profile_hook('requests')
done1 = processRemoveQueue(this)
|| processRemoveQueue(this)
profile_hook('removes')
}
if (!done2) {
done2 = processMeshingQueue(this, false)
profile_hook('meshes')
done2 = processRequestQueue(this)
profile_hook('requests')
}
if (!done3) {
done3 = processRemoveQueue(this)
|| processRemoveQueue(this)
profile_hook('removes')
done3 = processMeshingQueue(this, false)
profile_hook('meshes')
}
return (done1 && done2 && done3)
}, tickStartTime)
if (done1 && done2 && done3) break
t = performance.now()
}

// track whether the player's local chunk is loaded and ready or not
var pChunk = this._storage.getChunkByIndexes(ci, cj, ck)
Expand All @@ -438,10 +440,13 @@ World.prototype.tick = function () {
World.prototype.render = function () {
// on render, quickly process the high-priority meshing queue
// to help avoid flashes of background while neighboring chunks update
var mpr = this.maxProcessingPerRender
if (mpr > 0) loopForTime(mpr, () => {
return processMeshingQueue(this, true)
})
var t = performance.now()
var t1 = t + this.maxProcessingPerRender
while (t < t1) {
var done = processMeshingQueue(this, true)
if (done) break
t = performance.now()
}
}


Expand Down

0 comments on commit e2182da

Please sign in to comment.