Memory management of const values in Dart

后端 未结 1 1032
感情败类
感情败类 2021-01-24 11:02

Where and how long const values in classes being stored in dart language? For example const Duration()(link to the img below). I know that const and global values a

相关标签:
1条回答
  • 2021-01-24 12:06

    To officialize my comments with an answer, when you talk about values being stored on the stack or on the heap, you are talking about variables. Variables exist in a bunch of different forms, such as local variables, global variables, class-member variables, closured variables, and so on. Each type of variable gets stored in a different place in a different way, but a (very) broad nutshell explanation is that all variables either get stored on the stack (a linear section of memory that follows the program's execution path) or the heap (a less structured blob of memory that more or less exists for the duration of a long process if not the app's lifetime.) When you assign a value to a variable, you are telling the program to go to that variable's location in memory and make it equal to that value.

    Constants are a different concept entirely. They are not declared, instantiated, or assigned a value, and asking whether they are stored on the stack or on the heap is nonsensical because they are not variables. A constant is a value that is known and catalogued at compile time, such as 1, true, and "Hello World". When you declare a constant (e.g. const SECONDS_IN_MINUTE = 60;) you are not instantiating a variable, you are in essence creating an alias to a known value that the compiler substitutes in everywhere the constant is used. (i.e. The code int fiveMinutes = SECONDS_IN_MINUTE * 5; will be compiled into int fiveMinutes = 60 * 5;*)

    Likewise, const Duration(seconds: 1) is not a variable or an object that gets created at runtime but is a value that is known when the program is compiled and before it ever runs. This is why when you declare a constant constructor the class has to only have final fields and the parameters can only be types that are themselves constant, since an object that can be defined with non-constant fields is by definition not a constant.

    What's more, Dart supports a concept called canonical constants, which means that every constant you create points to the same value structure. For example:

    var a = Duration(seconds: 1);
    var b = Duration(seconds: 1);
    var c = Duration(seconds: 1);
    var d = Duration(seconds: 1);
    

    Each of the a, b, c, and d variables are storing non-constant values, which means you have four different Duration objects that are created separately from each other. On the other hand:

    var a = const Duration(seconds: 1);
    var b = const Duration(seconds: 1);
    var c = const Duration(seconds: 1);
    var d = const Duration(seconds: 1);
    

    Each of these variables are assigned constant values with the same value for seconds, which means they each point to the same value of Duration. This is a source of a lot of optimization when you are creating an app that uses a lot of values that could be made constant. (For example, if you have a lot of Padding widgets that have the same padding, changing all the EdgeInsets to be constant will prevent them from creating a new EdgeInsetsGeometry every time you use it.)


    *: Or something resembling this, assuming of course an optimization pass doesn't change 60 * 5 into 300 or other such predictive optimizations.

    0 讨论(0)
提交回复
热议问题