What is "cheaper"?

Valentin Micic v@REDACTED
Thu Mar 25 19:06:11 CET 2021

> On 25 Mar 2021, at 16:26, Maria Scott <maria-12648430@REDACTED> wrote:
> Hi,
> not sure about cheap, but...
>> But, Culpa Mea — I should have formulated my question a bit better.
>> Thus, let me take another stab at it: what would be more efficient, canceling the timer (and thus avoiding generation of the message), or letting the send_after/3 generating (and runtime delivering) the message?
>> The answer should be considered relative to the "event ordering" process — will it spend more time canceling the message, or removing generated message from its queue)?
> ... it may happen that the timer expires and sends the message just before you cancel it. The message will then pollute your message queue. As such, you should react on the return of cancel_timer/1, if it returns false you may want to flush out that message.
> But on another note, erlang:cancel_timer/1 is synonymous to erlang:cancel_timer/2 with options {async, false} and {info, true}, which results in a blocking operation that will wait for the cancellation to really "happen". You may look into option {async, true}, which will immediately return ok and not block the calling process. You will still have to take care of the possible message that may have been sent just before you cancelled the timer.
> Kind regards,
> Maria Scott

Thank you, Maria

All things considered, it appears to be “cheaper" not to cancel the timer (and thus generated event) when the timer values for are reasonably small (e.g. a few seconds), but desirable to do so (as Jasper suggested), when values for the timer are relatively high (in order to provide for a better memory utilisation used by erlang:send_after/3 scheduler). 

Kind regards


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210325/7546067d/attachment.htm>

More information about the erlang-questions mailing list