Issue
Does using anonymous function whenever possible reduces the memory usage and improves the overall performance in Dart?
Solution
Short Answer
No. It doesn't improve overall performance and also doesn't decrease memory usage. However the exact opposite might happen depending on the scenario.
Long Answer
Anonymous functions are the same as closures in Dart, and so will have a pointer(memory address) to the parent environment associated with it. Which means an additional variable with size equivalent to the bit-width of a pointer (in the corresponding platform) will be accompanying the anonymous function.
Memory
Here's how call to different types of functions work
- Normal function: The arguments for the functions are the only values passed for the call.
- Anonymous functions: The arguments for the functions are passed along with the pointer/reference to the parent's environment
- Class method: A pointer/reference to the particular object instance is passed as the first argument along with the rest of the arguments.
- Static Class method: This is equivalent to a normal function and the object instance of the class is not passed at all.
So, both class methods and anonymous functions have an extra variable, essentially a pointer, required to work. So "technically" this increases memory usage. However you don't have to worry about such things when you are using Dart. Because the extra memory required for anonymous functions and class methods is almost equivalent to when you add a new argument int newArg to a normal function or static class method.
If the anonymous function is executed right after it is declared, like (() {})(), then the pointer associated with it should be cleared in the next garbage collection sweep. This of course depends on the implementation and the scenario. If the anonymous function is stored in a variable or passed as an argument, then the lifetime of the pointer depends on the lifetime of that variable or argument.
Also, the pointer refers to the parent environment and because of this, the variables belonging to the parent scope, referred in the anonymous function will be kept alive even if the execution of the parent scope is complete. This in fact means that less memory is cleared in the next garbage collection sweep. The memory allocated for those referred variables will be cleared only when the anonymous function itself is not required anymore.
Performance
In the case of anonymous functions, there is also the need for lookup of parent-scoped variables used in the function body, that is the lookup for all the variables that are from the parent environment, this adds to compile-time or runtime depending on whether your dart code is AOT or JIT compiled.
But the same thing happens with normal class methods, as there is a need to lookup all class members used. (This doesn't happen in languages like Rust, Python... where there is a keyword like self or this representing the object instance that has to be explicitly mentioned)
Additional Context
Anonymous functions are otherwise referred to as lambdas or closures. There is actually a difference in definition between lambdas and closures mathematically, and both of those can work as the other one depending on the use-case. Lambdas are supposed to be functions that takes only one argument and has only one statement. Closures are supposed to be functions that closes over the scope of its parent. Basically, closures can look for variables in the environment of its parent. In Dart, there are no two entities separately known as lambdas and closures, instead there are closures. This is commonly seen in many programming languages, as they don't bother to keep a separation. By the way, the definition doesn't restrict a lambda from being a closure. And vice-versa.
Answered By - Aldrin Mathew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.