Skip to content

Commit 12b4606

Browse files
authored
perf: improve boot time by only traversing once
* refactor: only traverse once The traverse module already recursively walks every node, this includes nested objects and arrays. This makes the second traverse unnecessary and increases the execution time significantly. * refactor: skip enums and messages By skipping objects with the key type, this should skip enums and messages. Recursively iterating on enums and messages are unnecessary and expensive as they contain the file descriptor protos which is a buffer. * refactor: remove usage of _.keys * refactor: use single assignment to create object * refactor: remove pick and reduce assignments * docs: describe behaviour of call
1 parent 57b6276 commit 12b4606

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

lib/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Mali extends Emitter {
9999
const data = mu.getServiceDefintions(proto)
100100

101101
if (!name) {
102-
name = _.keys(data)
102+
name = Object.keys(data)
103103
} else if (_.isString(name)) {
104104
name = [name]
105105
}
@@ -190,7 +190,7 @@ class Mali extends Emitter {
190190
})
191191
} else if (_.isPlainObject(service)) {
192192
// we have object notation
193-
const testKey = _.keys(service)[0]
193+
const testKey = Object.keys(service)[0]
194194
if (_.isFunction(service[testKey]) || _.isArray(service[testKey])) {
195195
// first property of object is a function or array
196196
// that means we have service-level middleware of RPC handlers

lib/utils.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,44 @@ function getCallTypeFromDescriptor (descriptor) {
3737
function getServiceDefintions (proto) {
3838
const services = {}
3939

40-
traverse(proto).forEach(function (v0) {
41-
traverse(v0).forEach(function (v) {
42-
if (isService(v)) {
43-
const srviceObj = v.service || v
44-
const vKeys = _.keys(srviceObj)
45-
const name = getServiceNameFromPath(srviceObj[vKeys[0]].path)
46-
47-
if (services[name]) {
48-
return
49-
}
50-
51-
const shortServiceName = getShortServiceNameFromPath(srviceObj[vKeys[0]].path)
52-
53-
services[name] = {}
54-
services[name].shortServiceName = shortServiceName
55-
services[name].fullServiceName = name
56-
services[name].service = srviceObj
57-
58-
const methods = {}
59-
_.forOwn(srviceObj, (m, k) => {
60-
methods[m.path] = _.pick(m, METHOD_PROPS)
61-
methods[m.path].name = getMethodNameFromPath(m.path)
62-
methods[m.path].fullName = m.path
63-
methods[m.path].package = getPackageNameFromPath(m.path)
64-
methods[m.path].service = shortServiceName
65-
})
40+
traverse(proto).forEach(function (v) {
41+
if (isService(v)) {
42+
const srviceObj = v.service || v
43+
const vKeys = Object.keys(srviceObj)
44+
const name = getServiceNameFromPath(srviceObj[vKeys[0]].path)
45+
46+
if (services[name]) {
47+
return
48+
}
6649

67-
services[name].methods = methods
50+
const shortServiceName = getShortServiceNameFromPath(srviceObj[vKeys[0]].path)
51+
52+
services[name] = {
53+
shortServiceName: shortServiceName,
54+
fullServiceName: name,
55+
service: srviceObj
6856
}
69-
})
57+
58+
const methods = {}
59+
_.forOwn(srviceObj, (m, k) => {
60+
methods[m.path] = METHOD_PROPS.reduce((acc, method) => {
61+
if (method !== 'name') {
62+
acc[method] = m[method]
63+
}
64+
return acc
65+
}, {
66+
name: getMethodNameFromPath(m.path),
67+
fullName: m.path,
68+
package: getPackageNameFromPath(m.path),
69+
service: shortServiceName
70+
})
71+
})
72+
73+
services[name].methods = methods
74+
} else if (v && v.type) {
75+
// Skip children
76+
this.update(v, true)
77+
}
7078
})
7179

7280
return services
@@ -105,12 +113,12 @@ function getShortServiceNameFromPath (path) {
105113

106114
function isService (v) {
107115
if (v && v.service) {
108-
const vKeys = _.keys(v.service)
116+
const vKeys = Object.keys(v.service)
109117

110118
return vKeys && vKeys.length && v.service[vKeys[0]] && v.service[vKeys[0]].path &&
111119
_.isString(v.service[vKeys[0]].path) && v.service[vKeys[0]].path[0] === '/'
112120
} else if (v) {
113-
const vKeys = _.keys(v)
121+
const vKeys = Object.keys(v)
114122

115123
return vKeys && vKeys.length && v[vKeys[0]] && v[vKeys[0]].path &&
116124
_.isString(v[vKeys[0]].path) && v[vKeys[0]].path[0] === '/'

0 commit comments

Comments
 (0)