Don't rely on IDWeakMap for bookkeeping remote objects
It frees us from using C++ to track JS objects, thus improves the performance of collecting memory.
This commit is contained in:
parent
bd64f5ced2
commit
64e8ce0c07
4 changed files with 59 additions and 70 deletions
|
@ -1,82 +1,65 @@
|
||||||
EventEmitter = require('events').EventEmitter
|
EventEmitter = require('events').EventEmitter
|
||||||
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
|
|
||||||
v8Util = process.atomBinding 'v8_util'
|
v8Util = process.atomBinding 'v8_util'
|
||||||
|
|
||||||
# Class to reference all objects.
|
|
||||||
class ObjectsStore
|
|
||||||
@stores = {}
|
|
||||||
|
|
||||||
constructor: ->
|
|
||||||
@nextId = 0
|
|
||||||
@objects = []
|
|
||||||
|
|
||||||
getNextId: ->
|
|
||||||
++@nextId
|
|
||||||
|
|
||||||
add: (obj) ->
|
|
||||||
id = @getNextId()
|
|
||||||
@objects[id] = obj
|
|
||||||
id
|
|
||||||
|
|
||||||
has: (id) ->
|
|
||||||
@objects[id]?
|
|
||||||
|
|
||||||
remove: (id) ->
|
|
||||||
throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
|
|
||||||
delete @objects[id]
|
|
||||||
|
|
||||||
get: (id) ->
|
|
||||||
throw new Error("Invalid key #{id} for ObjectsStore") unless @has id
|
|
||||||
@objects[id]
|
|
||||||
|
|
||||||
@forRenderView: (key) ->
|
|
||||||
@stores[key] = new ObjectsStore unless @stores[key]?
|
|
||||||
@stores[key]
|
|
||||||
|
|
||||||
@releaseForRenderView: (key) ->
|
|
||||||
delete @stores[key]
|
|
||||||
|
|
||||||
class ObjectsRegistry extends EventEmitter
|
class ObjectsRegistry extends EventEmitter
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@setMaxListeners Number.MAX_VALUE
|
@setMaxListeners Number.MAX_VALUE
|
||||||
|
@nextId = 0
|
||||||
|
|
||||||
# Objects in weak map will be not referenced (so we won't leak memory), and
|
# Stores all objects by ref-counting.
|
||||||
# every object created in browser will have a unique id in weak map.
|
# (id) => {object, count}
|
||||||
@objectsWeakMap = new IDWeakMap
|
@storage = {}
|
||||||
@objectsWeakMap.add = (obj) ->
|
|
||||||
id = IDWeakMap::add.call this, obj
|
# Stores the IDs of objects referenced by WebContents.
|
||||||
v8Util.setHiddenValue obj, 'atomId', id
|
# (webContentsId) => {(id) => (count)}
|
||||||
id
|
@owners = {}
|
||||||
|
|
||||||
# Register a new object, the object would be kept referenced until you release
|
# Register a new object, the object would be kept referenced until you release
|
||||||
# it explicitly.
|
# it explicitly.
|
||||||
add: (key, obj) ->
|
add: (webContentsId, obj) ->
|
||||||
# Some native objects may already been added to objectsWeakMap, be care not
|
id = @saveToStorage obj
|
||||||
# to add it twice.
|
# Remember the owner.
|
||||||
@objectsWeakMap.add obj unless v8Util.getHiddenValue obj, 'atomId'
|
@owners[webContentsId] ?= {}
|
||||||
id = v8Util.getHiddenValue obj, 'atomId'
|
@owners[webContentsId][id] ?= 0
|
||||||
|
@owners[webContentsId][id]++
|
||||||
|
# Returns object's id
|
||||||
|
id
|
||||||
|
|
||||||
# Store and reference the object, then return the storeId which points to
|
# Get an object according to its ID.
|
||||||
# where the object is stored. The caller can later dereference the object
|
|
||||||
# with the storeId.
|
|
||||||
# We use a difference key because the same object can be referenced for
|
|
||||||
# multiple times by the same renderer view.
|
|
||||||
store = ObjectsStore.forRenderView key
|
|
||||||
storeId = store.add obj
|
|
||||||
|
|
||||||
[id, storeId]
|
|
||||||
|
|
||||||
# Get an object according to its id.
|
|
||||||
get: (id) ->
|
get: (id) ->
|
||||||
@objectsWeakMap.get id
|
@storage[id]?.object
|
||||||
|
|
||||||
# Remove an object according to its storeId.
|
# Dereference an object according to its ID.
|
||||||
remove: (key, storeId) ->
|
remove: (webContentsId, id) ->
|
||||||
ObjectsStore.forRenderView(key).remove storeId
|
@dereference id, 1
|
||||||
|
# Also reduce the count in owner.
|
||||||
|
pointer = @owners[webContentsId]
|
||||||
|
--pointer[id]
|
||||||
|
delete pointer[id] if pointer[id] is 0
|
||||||
|
|
||||||
# Clear all references to objects from renderer view.
|
# Clear all references to objects refrenced by the WebContents.
|
||||||
clear: (key) ->
|
clear: (webContentsId) ->
|
||||||
@emit "clear-#{key}"
|
@emit "clear-#{webContentsId}"
|
||||||
ObjectsStore.releaseForRenderView key
|
return unless @owners[webContentsId]?
|
||||||
|
@dereference id, count for id, count of @owners[webContentsId]
|
||||||
|
delete @owners[webContentsId]
|
||||||
|
|
||||||
|
# Private: Saves the object into storage and assigns an ID for it.
|
||||||
|
saveToStorage: (object) ->
|
||||||
|
id = v8Util.getHiddenValue object, 'atomId'
|
||||||
|
unless id
|
||||||
|
id = ++@nextId
|
||||||
|
@storage[id] = {count: 0, object}
|
||||||
|
v8Util.setHiddenValue object, 'atomId', id
|
||||||
|
++@storage[id].count
|
||||||
|
id
|
||||||
|
|
||||||
|
# Private: Dereference the object from store.
|
||||||
|
dereference: (id, count) ->
|
||||||
|
pointer = @storage[id]
|
||||||
|
pointer.count -= count
|
||||||
|
if pointer.count is 0
|
||||||
|
v8Util.deleteHiddenValue pointer.object, 'atomId'
|
||||||
|
delete @storage[id]
|
||||||
|
|
||||||
module.exports = new ObjectsRegistry
|
module.exports = new ObjectsRegistry
|
||||||
|
|
|
@ -28,7 +28,7 @@ valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
||||||
# Reference the original value if it's an object, because when it's
|
# Reference the original value if it's an object, because when it's
|
||||||
# passed to renderer we would assume the renderer keeps a reference of
|
# passed to renderer we would assume the renderer keeps a reference of
|
||||||
# it.
|
# it.
|
||||||
[meta.id, meta.storeId] = objectsRegistry.add sender.getId(), value
|
meta.id = objectsRegistry.add sender.getId(), value
|
||||||
|
|
||||||
meta.members = []
|
meta.members = []
|
||||||
meta.members.push {name: prop, type: typeof field} for prop, field of value
|
meta.members.push {name: prop, type: typeof field} for prop, field of value
|
||||||
|
@ -174,8 +174,8 @@ ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, id, name) ->
|
||||||
catch e
|
catch e
|
||||||
event.returnValue = errorToMeta e
|
event.returnValue = errorToMeta e
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, storeId) ->
|
ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, id) ->
|
||||||
objectsRegistry.remove event.sender.getId(), storeId
|
objectsRegistry.remove event.sender.getId(), id
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_GUEST_WEB_CONTENTS', (event, guestInstanceId) ->
|
ipc.on 'ATOM_BROWSER_GUEST_WEB_CONTENTS', (event, guestInstanceId) ->
|
||||||
try
|
try
|
||||||
|
|
|
@ -28,6 +28,11 @@ void SetHiddenValue(v8::Local<v8::Object> object,
|
||||||
object->SetHiddenValue(key, value);
|
object->SetHiddenValue(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DeleteHiddenValue(v8::Local<v8::Object> object,
|
||||||
|
v8::Local<v8::String> key) {
|
||||||
|
object->DeleteHiddenValue(key);
|
||||||
|
}
|
||||||
|
|
||||||
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
||||||
return object->GetIdentityHash();
|
return object->GetIdentityHash();
|
||||||
}
|
}
|
||||||
|
@ -48,6 +53,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
dict.SetMethod("createObjectWithName", &CreateObjectWithName);
|
dict.SetMethod("createObjectWithName", &CreateObjectWithName);
|
||||||
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
||||||
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
||||||
|
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
||||||
dict.SetMethod("getObjectHash", &GetObjectHash);
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
||||||
dict.SetMethod("setDestructor", &SetDestructor);
|
dict.SetMethod("setDestructor", &SetDestructor);
|
||||||
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
||||||
|
|
|
@ -102,7 +102,7 @@ metaToValue = (meta) ->
|
||||||
# Track delegate object's life time, and tell the browser to clean up
|
# Track delegate object's life time, and tell the browser to clean up
|
||||||
# when the object is GCed.
|
# when the object is GCed.
|
||||||
v8Util.setDestructor ret, ->
|
v8Util.setDestructor ret, ->
|
||||||
ipc.send 'ATOM_BROWSER_DEREFERENCE', meta.storeId
|
ipc.send 'ATOM_BROWSER_DEREFERENCE', meta.id
|
||||||
|
|
||||||
# Remember object's id.
|
# Remember object's id.
|
||||||
v8Util.setHiddenValue ret, 'atomId', meta.id
|
v8Util.setHiddenValue ret, 'atomId', meta.id
|
||||||
|
|
Loading…
Reference in a new issue