Skip to content

Commit

Permalink
fix(journal-parse): Improve idle CPU performance ZMS-109 (#597)
Browse files Browse the repository at this point in the history
* optimize CPU performance, reduce amount of calls to JSON.parse

* default to needFullParse, otherwise do not do full parse
  • Loading branch information
NickOvt authored Jan 5, 2024
1 parent 8b7f6c9 commit 5721047
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions lib/imap-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,39 @@ class ImapNotifier extends EventEmitter {
this.subscriber.on('message', (channel, message) => {
if (channel === 'wd_events') {
let data;
try {
data = JSON.parse(message);
} catch (E) {
return;
// if e present at beginning, check if p also is present
// if no p -> no json parse
// if p -> json parse ONLY p
// if e not in beginning but p is -> json parse whole

let needFullParse = true;

if (message.length === 32 && message[2] === 'e' && message[5] === '"' && message[6 + 24] === '"') {
// there is only e, no p -> no need for full parse
needFullParse = false;
}

if (!needFullParse) {
// get e and continue
data = { e: message.slice(6, 6 + 24) };
} else {
// full parse
try {
data = JSON.parse(message);
} catch (E) {
return;
}
}
if (data.e && !data.p) {
// events without payload are scheduled, these are notifications about changes in journal
scheduleDataEvent(data.e);
} else if (data.e) {
// events with payload are triggered immediatelly, these are actions for doing something
this._listeners.emit(data.e, data.p);

if (this._listeners._events[data.e]?.length > 0) {
// do not schedule or fire/emit empty events
if (data.e && !data.p) {
// events without payload are scheduled, these are notifications about changes in journal
scheduleDataEvent(data.e);
} else if (data.e) {
// events with payload are triggered immediatelly, these are actions for doing something
this._listeners.emit(data.e, data.p);
}
}
}
});
Expand Down

0 comments on commit 5721047

Please sign in to comment.