From 0caec9d28beea1297c22c8e4f5a72a804fadec15 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 27 Jul 2020 10:40:33 +0200 Subject: [PATCH] Explain gas implications of constant and immutable variables. --- docs/contracts/constant-state-variables.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/contracts/constant-state-variables.rst b/docs/contracts/constant-state-variables.rst index c5c520d33..33204f58d 100644 --- a/docs/contracts/constant-state-variables.rst +++ b/docs/contracts/constant-state-variables.rst @@ -12,6 +12,14 @@ for ``immutable``, it can still be assigned at construction time. The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value. +Compared to regular state variables, the gas costs of constant and immutable variables +are much lower. For a constant variable, the expression assigned to it is copied to +all the places where it is accessed and also re-evaluated each time. This allows for local +optimizations. Immutable variables are evaluated once at construction time and their value +is copied to all the places in the code where they are accessed. For these values, +32 bytes are reserved, even if they would fit in fewer bytes. Due to this, constant values +can sometimes be cheaper than immutable values. + Not all types for constants and immutables are implemented at this time. The only supported types are `strings `_ (only for constants) and `value types `_.