From 0268cbddc7dd413fb11be679e929d4c4be4ca46a Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Thu, 25 Aug 2016 13:20:54 -0400 Subject: Document use of smaller storage variables --- docs/miscellaneous.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index 882a6002..98497872 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -15,6 +15,23 @@ Statically-sized variables (everything except mapping and dynamically-sized arra - If an elementary type does not fit the remaining part of a storage slot, it is moved to the next storage slot. - Structs and array data always start a new slot and occupy whole slots (but items inside a struct or array are packed tightly according to these rules). +.. warning:: + When using elements that are smaller than 32 bytes, your contract's gas usage may be higher. + This is because the EVM operates on 32 bytes a a time. Therefore, if the element is smaller than + that, the EVM must use more operations in order to reduce the size of the element from 32 bytes + to the desired size. + + It is only beneficial to use reduced-size arguments if you are dealing with storage values + because the compiler will pack multiple elements into one storage slot. When dealing with + function arguments or memory values, there is no inherent benefit because the compiler does not + pack these values. + + Finally, in order to allow the EVM to optimize for this, ensure that you try to order your + storage variables such that they can be packed tightly. For example, declaring your storage + variables in the order of ``uint128, uint128, uint256`` instead of ``uint128, uint256, + uint128``, as the former will only take up two slots of storage whereas the latter will take up + three. + The elements of structs and arrays are stored after each other, just as if they were given explicitly. Due to their unpredictable size, mapping and dynamically-sized array types use a ``sha3`` -- cgit v1.2.3 From 89489da4ccc80ef716db8c23ebd99cd06cc19a20 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Fri, 26 Aug 2016 13:23:34 -0400 Subject: Fix typo --- docs/miscellaneous.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index 98497872..e06a241f 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -17,9 +17,9 @@ Statically-sized variables (everything except mapping and dynamically-sized arra .. warning:: When using elements that are smaller than 32 bytes, your contract's gas usage may be higher. - This is because the EVM operates on 32 bytes a a time. Therefore, if the element is smaller than - that, the EVM must use more operations in order to reduce the size of the element from 32 bytes - to the desired size. + This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller + than that, the EVM must use more operations in order to reduce the size of the element from 32 + bytes to the desired size. It is only beneficial to use reduced-size arguments if you are dealing with storage values because the compiler will pack multiple elements into one storage slot. When dealing with -- cgit v1.2.3 From 2d834f723a12ae7b33dc4d1ebd35578b8b584b52 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Fri, 26 Aug 2016 13:25:57 -0400 Subject: Write about multiple reads and writes --- docs/miscellaneous.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index e06a241f..69d4d310 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -22,9 +22,9 @@ Statically-sized variables (everything except mapping and dynamically-sized arra bytes to the desired size. It is only beneficial to use reduced-size arguments if you are dealing with storage values - because the compiler will pack multiple elements into one storage slot. When dealing with - function arguments or memory values, there is no inherent benefit because the compiler does not - pack these values. + because the compiler will pack multiple elements into one storage slot, and thus, combine + multiple reads or writes into a single operation. When dealing with function arguments or memory + values, there is no inherent benefit because the compiler does not pack these values. Finally, in order to allow the EVM to optimize for this, ensure that you try to order your storage variables such that they can be packed tightly. For example, declaring your storage -- cgit v1.2.3 From d905f0e85bba5954ffe50a3f29ca68649130ea77 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Fri, 26 Aug 2016 13:28:44 -0400 Subject: Write about struct members as well --- docs/miscellaneous.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs') diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index 69d4d310..8d1452b4 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -27,10 +27,10 @@ Statically-sized variables (everything except mapping and dynamically-sized arra values, there is no inherent benefit because the compiler does not pack these values. Finally, in order to allow the EVM to optimize for this, ensure that you try to order your - storage variables such that they can be packed tightly. For example, declaring your storage - variables in the order of ``uint128, uint128, uint256`` instead of ``uint128, uint256, - uint128``, as the former will only take up two slots of storage whereas the latter will take up - three. + storage variables and ``struct`` members such that they can be packed tightly. For example, + declaring your storage variables in the order of ``uint128, uint128, uint256`` instead of + ``uint128, uint256, uint128``, as the former will only take up two slots of storage whereas the + latter will take up three. The elements of structs and arrays are stored after each other, just as if they were given explicitly. -- cgit v1.2.3