-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache #4
base: master
Are you sure you want to change the base?
Cache #4
Changes from all commits
e96eeff
84e2166
cfdff11
29c4942
311526c
42e39d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var utils = require("./utils") | |
var arrayCreate = utils.arrayCreate | ||
var arrayExpand = utils.arrayExpand | ||
var typedArrayExpand = utils.typedArrayExpand | ||
var FastMap = utils.FastMap | ||
|
||
var INITIAL_CAPACITY = 1024 | ||
var ENTITY_DEAD = 0 | ||
|
@@ -22,6 +23,10 @@ function EntityManager(components, storage) { | |
|
||
this._entityPool = [] | ||
this._entityCounter = 0 | ||
|
||
this._entityCacheIndices = new Uint32Array(INITIAL_CAPACITY) | ||
this._entityCache = FastMap() | ||
this._entityCache[0] = [] | ||
} | ||
|
||
EntityManager.prototype.create = function() { | ||
|
@@ -37,6 +42,8 @@ EntityManager.prototype.create = function() { | |
var entity = this._entityInst[id] | ||
this._entityFlag[id] = ENTITY_ALIVE | ||
|
||
this._entityCacheIndices[entity._id] = this._entityCache[0].push(entity) - 1 | ||
|
||
return entity | ||
} | ||
|
||
|
@@ -54,10 +61,26 @@ EntityManager.prototype.query = function() { | |
return [] | ||
} | ||
|
||
var result = [] | ||
for (var id = 0; id < this._entityCounter; id++) { | ||
if (this._entityFlag[id] === ENTITY_ALIVE && (this._entityMask[id] & mask) === mask) { | ||
result.push(this._entityInst[id]) | ||
var allocLen = 0 | ||
var entityMasks = Object.keys(this._entityCache) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a real performance killer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will add something that removes the empty ones. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Want me to fix this before the merge? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! But I won't merge it immediately. I want to add more tests and benchmarks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried two approaches so far, changing the property descriptor to enumerable: false so it doesn't appear in Object.keys(). This resulted in quite bad performance when adding/removing components. Also tried simply deleting the property which I know is a bad thing to do on objects. But the performance is much better. 10x faster when adding components, 100x faster when removing them. Overall it increased the query performance by roughly 1.5-2x on 1000 entities. Any other ideas on how to do this? |
||
for (var index = 0; index < entityMasks.length; index++) { | ||
var entityMask = entityMasks[index] | ||
if((entityMask & mask) === mask) { | ||
allocLen += this._entityCache[entityMask].length | ||
} | ||
} | ||
|
||
var result = new Array(allocLen) | ||
var resultIndex = 0 | ||
|
||
for (var index = 0; index < entityMasks.length; index++) { | ||
var entityMask = entityMasks[index] | ||
if((entityMask & mask) === mask) { | ||
var cache = this._entityCache[entityMask] | ||
for (var i = 0; i < cache.length; i++) { | ||
result[resultIndex] = cache[i] | ||
resultIndex++ | ||
} | ||
} | ||
} | ||
|
||
|
@@ -76,6 +99,7 @@ EntityManager.prototype._accomodate = function(id) { | |
|
||
this._entityFlag = typedArrayExpand(this._entityFlag, capacity) | ||
this._entityMask = typedArrayExpand(this._entityMask, capacity) | ||
this._entityCacheIndices = typedArrayExpand(this._entityCacheIndices, capacity) | ||
this._entityInst = arrayExpand(this._entityInst, capacity, null) | ||
this._storage.resize(capacity) | ||
} | ||
|
@@ -84,13 +108,20 @@ EntityManager.prototype._accomodate = function(id) { | |
EntityManager.prototype._add = function(id, component) { | ||
var ctor = component.constructor | ||
var index = this._components.index(ctor) | ||
|
||
this._cacheRemove(id) | ||
this._entityMask[id] |= 1 << index | ||
this._cacheAdd(id) | ||
this._storage.set(id, index, component) | ||
} | ||
|
||
EntityManager.prototype._remove = function(id, C) { | ||
var index = this._components.index(C) | ||
|
||
this._cacheRemove(id) | ||
this._entityMask[id] &= ~(1 << index) | ||
this._cacheAdd(id) | ||
|
||
this._storage.delete(id, index) | ||
} | ||
|
||
|
@@ -104,7 +135,31 @@ EntityManager.prototype._get = function(id, C) { | |
return this._storage.get(id, index) | ||
} | ||
|
||
EntityManager.prototype._cacheAdd = function(id) { | ||
var entity = this._entityInst[id] | ||
var cache = this._entityCache[this._entityMask[id]] | ||
if (cache === undefined) { | ||
this._entityCache[this._entityMask[id]] = [] | ||
} | ||
this._entityCacheIndices[id] = this._entityCache[this._entityMask[id]].push(entity) - 1 | ||
} | ||
|
||
EntityManager.prototype._cacheRemove = function(id) { | ||
var entity = this._entityInst[id] | ||
var cache = this._entityCache[this._entityMask[id]] | ||
if (cache.length === 1 || this._entityCacheIndices[id] === cache.length - 1) { | ||
cache.pop() | ||
} else { | ||
var lastCacheElement = cache.pop() | ||
this._entityCacheIndices[lastCacheElement._id] = this._entityCacheIndices[entity._id] | ||
cache[this._entityCacheIndices[lastCacheElement._id]] = lastCacheElement | ||
} | ||
} | ||
|
||
EntityManager.prototype._destroy = function(id) { | ||
if (this._entityFlag[id] !== ENTITY_DEAD) { | ||
this._cacheRemove(id) | ||
} | ||
this._entityFlag[id] = ENTITY_DEAD | ||
this._entityMask[id] = 0 | ||
this._entityPool.push(id) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be optimized:
See https://gist.github.com/ooflorent/c58c9a0f08923973a5e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So do you suggest I add fast.js to the project and require it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Next to
utils.js
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed on the gist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wow didn't notice the missing brackets