perf: avoid array allocation in _onMethod#82
Merged
Conversation
Contributor
Author
|
It will look like this: /** @internal */
_onMethod(methodFrame: MethodFrame): void {
if (this._state.incoming != null) {
throw new AMQPConnectionError('UNEXPECTED_FRAME',
'unexpected method frame, already awaiting header/body; this is a bug')
}
switch(methodFrame.methodId) {
case Cmd.BasicDeliver:
case Cmd.BasicReturn:
case Cmd.BasicGetOK:
this._state.incoming = {methodFrame, headerFrame: undefined, chunks: undefined, received: 0}
break;
case Cmd.BasicGetEmpty:
this._handleRPC(Cmd.BasicGetOK, undefined)
break;
case Cmd.BasicAck:
if (this._state.mode === CH_MODE.CONFIRM) {
const params = methodFrame.params as Required<MethodParams[Cmd.BasicAck]>
if (params.multiple) {
for (const [tag, dfd] of this._state.unconfirmed.entries()) {
if (tag > params.deliveryTag)
break
dfd.resolve()
this._state.unconfirmed.delete(tag)
}
} else {
const dfd = this._state.unconfirmed.get(params.deliveryTag)
if (dfd) {
dfd.resolve()
this._state.unconfirmed.delete(params.deliveryTag)
} else {
//TODO channel error; PRECONDITION_FAILED, unexpected ack
}
}
}
break;
case Cmd.BasicNack:
if (this._state.mode === CH_MODE.CONFIRM) {
const params = methodFrame.params as Required<MethodParams[Cmd.BasicNack]>
if (params.multiple) {
for (const [tag, dfd] of this._state.unconfirmed.entries()) {
if (tag > params.deliveryTag)
break
dfd.reject(new AMQPError('NACK', 'message rejected by server'))
this._state.unconfirmed.delete(tag)
}
} else {
const dfd = this._state.unconfirmed.get(params.deliveryTag)
if (dfd) {
dfd.reject(new AMQPError('NACK', 'message rejected by server'))
this._state.unconfirmed.delete(params.deliveryTag)
} else {
//TODO channel error; PRECONDITION_FAILED, unexpected nack
}
}
}
break;
case Cmd.BasicCancel:
const params = methodFrame.params as Required<MethodParams[Cmd.BasicCancel]>
this._state.consumers.delete(params.consumerTag)
setImmediate(() => {
this.emit('basic.cancel', params.consumerTag, new AMQPError('CANCEL_FORCED', 'cancelled by server'))
})
break;
default:
this._handleRPC(methodFrame.methodId, methodFrame.params)
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One more small optimization. This method may also benefit in using switch statement, I can give it a try if you want