mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #11271 from ethereum/cleanUpArrayEndSol2Yul
[Sol->Yul] Clean up array end util function
This commit is contained in:
commit
1f6aa4c4b7
@ -1168,21 +1168,7 @@ std::string YulUtilFunctions::resizeArrayFunction(ArrayType const& _type)
|
||||
</isDynamic>
|
||||
|
||||
<?needsClearing>
|
||||
// Size was reduced, clear end of array
|
||||
if lt(newLen, oldLen) {
|
||||
let oldSlotCount := <convertToSize>(oldLen)
|
||||
let newSlotCount := <convertToSize>(newLen)
|
||||
let arrayDataStart := <dataPosition>(array)
|
||||
let deleteStart := add(arrayDataStart, newSlotCount)
|
||||
let deleteEnd := add(arrayDataStart, oldSlotCount)
|
||||
<?packed>
|
||||
// if we are dealing with packed array and offset is greater than zero
|
||||
// we have to partially clear last slot that is still used, so decreasing start by one
|
||||
let offset := mul(mod(newLen, <itemsPerSlot>), <storageBytes>)
|
||||
if gt(offset, 0) { <partialClearStorageSlot>(sub(deleteStart, 1), offset) }
|
||||
</packed>
|
||||
<clearStorageRange>(deleteStart, deleteEnd)
|
||||
}
|
||||
<cleanUpArrayEnd>(array, oldLen, newLen)
|
||||
</needsClearing>
|
||||
})");
|
||||
templ("functionName", functionName);
|
||||
@ -1193,19 +1179,49 @@ std::string YulUtilFunctions::resizeArrayFunction(ArrayType const& _type)
|
||||
bool isMappingBase = _type.baseType()->category() == Type::Category::Mapping;
|
||||
templ("needsClearing", !isMappingBase);
|
||||
if (!isMappingBase)
|
||||
{
|
||||
templ("convertToSize", arrayConvertLengthToSize(_type));
|
||||
templ("dataPosition", arrayDataAreaFunction(_type));
|
||||
templ("clearStorageRange", clearStorageRangeFunction(*_type.baseType()));
|
||||
templ("packed", _type.baseType()->storageBytes() <= 16);
|
||||
templ("itemsPerSlot", to_string(32 / _type.baseType()->storageBytes()));
|
||||
templ("storageBytes", to_string(_type.baseType()->storageBytes()));
|
||||
templ("partialClearStorageSlot", partialClearStorageSlotFunction());
|
||||
}
|
||||
templ("cleanUpArrayEnd", cleanUpStorageArrayEndFunction(_type));
|
||||
return templ.render();
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::cleanUpStorageArrayEndFunction(ArrayType const& _type)
|
||||
{
|
||||
solAssert(_type.location() == DataLocation::Storage, "");
|
||||
solAssert(_type.baseType()->category() != Type::Category::Mapping, "");
|
||||
solAssert(!_type.isByteArray(), "");
|
||||
solUnimplementedAssert(_type.baseType()->storageBytes() <= 32, "");
|
||||
|
||||
string functionName = "cleanup_storage_array_end_" + _type.identifier();
|
||||
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
||||
_args = {"array", "len", "startIndex"};
|
||||
return Whiskers(R"(
|
||||
if lt(startIndex, len) {
|
||||
// Size was reduced, clear end of array
|
||||
let oldSlotCount := <convertToSize>(len)
|
||||
let newSlotCount := <convertToSize>(startIndex)
|
||||
let arrayDataStart := <dataPosition>(array)
|
||||
let deleteStart := add(arrayDataStart, newSlotCount)
|
||||
let deleteEnd := add(arrayDataStart, oldSlotCount)
|
||||
<?packed>
|
||||
// if we are dealing with packed array and offset is greater than zero
|
||||
// we have to partially clear last slot that is still used, so decreasing start by one
|
||||
let offset := mul(mod(startIndex, <itemsPerSlot>), <storageBytes>)
|
||||
if gt(offset, 0) { <partialClearStorageSlot>(sub(deleteStart, 1), offset) }
|
||||
</packed>
|
||||
<clearStorageRange>(deleteStart, deleteEnd)
|
||||
}
|
||||
)")
|
||||
("convertToSize", arrayConvertLengthToSize(_type))
|
||||
("dataPosition", arrayDataAreaFunction(_type))
|
||||
("clearStorageRange", clearStorageRangeFunction(*_type.baseType()))
|
||||
("packed", _type.baseType()->storageBytes() <= 16)
|
||||
("itemsPerSlot", to_string(32 / _type.baseType()->storageBytes()))
|
||||
("storageBytes", to_string(_type.baseType()->storageBytes()))
|
||||
("partialClearStorageSlot", partialClearStorageSlotFunction())
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
||||
{
|
||||
string functionName = "resize_array_" + _type.identifier();
|
||||
@ -1230,6 +1246,30 @@ string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::cleanUpDynamicByteArrayEndSlotsFunction(ArrayType const& _type)
|
||||
{
|
||||
solAssert(_type.isByteArray(), "");
|
||||
solAssert(_type.isDynamicallySized(), "");
|
||||
|
||||
string functionName = "clean_up_bytearray_end_slots_" + _type.identifier();
|
||||
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
||||
_args = {"array", "len", "startIndex"};
|
||||
return Whiskers(R"(
|
||||
if gt(len, 31) {
|
||||
let dataArea := <dataLocation>(array)
|
||||
let deleteStart := add(dataArea, div(<roundUp>(startIndex), 32))
|
||||
// If we are clearing array to be short byte array, we want to clear only data starting from array data area.
|
||||
if lt(startIndex, 32) { deleteStart := dataArea }
|
||||
<clearStorageRange>(deleteStart, add(dataArea, div(add(len, 31), 32)))
|
||||
}
|
||||
)")
|
||||
("dataLocation", arrayDataAreaFunction(_type))
|
||||
("roundUp", roundUpFunction())
|
||||
("clearStorageRange", clearStorageRangeFunction(*_type.baseType()))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::decreaseByteArraySizeFunction(ArrayType const& _type)
|
||||
{
|
||||
string functionName = "byte_array_decrease_size_" + _type.identifier();
|
||||
@ -1805,28 +1845,19 @@ string YulUtilFunctions::copyByteArrayToStorageFunction(ArrayType const& _fromTy
|
||||
|
||||
let oldLen := <byteArrayLength>(sload(slot))
|
||||
|
||||
// potentially truncate data
|
||||
<cleanUpEndArray>(slot, oldLen, newLen)
|
||||
|
||||
let srcOffset := 0
|
||||
<?fromMemory>
|
||||
srcOffset := 0x20
|
||||
</fromMemory>
|
||||
|
||||
// This is not needed in all branches.
|
||||
let dstDataArea
|
||||
if or(gt(oldLen, 31), gt(newLen, 31)) {
|
||||
dstDataArea := <dstDataLocation>(slot)
|
||||
}
|
||||
|
||||
if gt(oldLen, 31) {
|
||||
// potentially truncate data
|
||||
let deleteStart := add(dstDataArea, div(add(newLen, 31), 32))
|
||||
if lt(newLen, 32) { deleteStart := dstDataArea }
|
||||
<clearStorageRange>(deleteStart, add(dstDataArea, div(add(oldLen, 31), 32)))
|
||||
}
|
||||
switch gt(newLen, 31)
|
||||
case 1 {
|
||||
let loopEnd := and(newLen, not(0x1f))
|
||||
<?fromStorage> src := <srcDataLocation>(src) </fromStorage>
|
||||
let dstPtr := dstDataArea
|
||||
let dstPtr := <dstDataLocation>(slot)
|
||||
let i := 0
|
||||
for { } lt(i, loopEnd) { i := add(i, 0x20) } {
|
||||
sstore(dstPtr, <read>(add(src, srcOffset)))
|
||||
@ -1860,7 +1891,7 @@ string YulUtilFunctions::copyByteArrayToStorageFunction(ArrayType const& _fromTy
|
||||
templ("dstDataLocation", arrayDataAreaFunction(_toType));
|
||||
if (fromStorage)
|
||||
templ("srcDataLocation", arrayDataAreaFunction(_fromType));
|
||||
templ("clearStorageRange", clearStorageRangeFunction(*_toType.baseType()));
|
||||
templ("cleanUpEndArray", cleanUpDynamicByteArrayEndSlotsFunction(_toType));
|
||||
templ("srcIncrement", to_string(fromStorage ? 1 : 0x20));
|
||||
templ("read", fromStorage ? "sload" : fromCalldata ? "calldataload" : "mload");
|
||||
templ("maskBytes", maskBytesFunctionDynamic());
|
||||
|
@ -207,6 +207,11 @@ public:
|
||||
/// signature: (array, newLen)
|
||||
std::string resizeArrayFunction(ArrayType const& _type);
|
||||
|
||||
/// @returns the name of a function that zeroes all storage array elements from `startIndex` to `len` (excluding).
|
||||
/// Assumes that `len` is the array length. Does nothing if `startIndex >= len`. Does not modify the stored length.
|
||||
/// signature: (array, len, startIndex)
|
||||
std::string cleanUpStorageArrayEndFunction(ArrayType const& _type);
|
||||
|
||||
/// @returns the name of a function that reduces the size of a storage array by one element
|
||||
/// signature: (array)
|
||||
std::string storageArrayPopFunction(ArrayType const& _type);
|
||||
@ -543,6 +548,14 @@ private:
|
||||
/// signature: (array, newLen)
|
||||
std::string resizeDynamicByteArrayFunction(ArrayType const& _type);
|
||||
|
||||
/// @returns the name of a function that cleans up elements of a storage byte array starting from startIndex.
|
||||
/// It will not copy elements in case of transformation to short byte array, and will not change array length.
|
||||
/// In case of startIndex is greater than len, doesn't do anything.
|
||||
/// In case of short byte array (< 32 bytes) doesn't do anything.
|
||||
/// If the first slot to be cleaned up is partially occupied, does not touch it. Cleans up only completely unused slots.
|
||||
/// signature: (array, len, startIndex)
|
||||
std::string cleanUpDynamicByteArrayEndSlotsFunction(ArrayType const& _type);
|
||||
|
||||
/// @returns the name of a function that increases size of byte array
|
||||
/// when we resize byte array frextractUsedSetLenom < 32 elements to >= 32 elements or we push to byte array of size 31 copying of data will occur
|
||||
/// signature: (array, data, oldLen, newLen)
|
||||
|
@ -24,6 +24,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0x20, 0x8, 0x40, 0x3, 0x9, 0xa, 0xb
|
||||
// gas irOptimized: 193543
|
||||
// gas irOptimized: 193521
|
||||
// gas legacy: 196426
|
||||
// gas legacyOptimized: 193405
|
||||
|
@ -11,6 +11,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
|
||||
// gas irOptimized: 130053
|
||||
// gas irOptimized: 130031
|
||||
// gas legacy: 131690
|
||||
// gas legacyOptimized: 130582
|
||||
|
@ -15,7 +15,7 @@ contract c {
|
||||
// ----
|
||||
// getLength() -> 0
|
||||
// set(): 1, 2 -> true
|
||||
// gas irOptimized: 102993
|
||||
// gas irOptimized: 102970
|
||||
// gas legacy: 103126
|
||||
// gas legacyOptimized: 102967
|
||||
// getLength() -> 68
|
||||
|
@ -7,11 +7,11 @@ contract c {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// set(uint256): 1, 2 -> true
|
||||
// gas irOptimized: 103242
|
||||
// gas irOptimized: 103224
|
||||
// gas legacy: 103491
|
||||
// gas legacyOptimized: 103136
|
||||
// set(uint256): 2, 2, 3, 4, 5 -> true
|
||||
// gas irOptimized: 163929
|
||||
// gas irOptimized: 163911
|
||||
// gas legacy: 164121
|
||||
// gas legacyOptimized: 163766
|
||||
// storageEmpty -> 0
|
||||
|
@ -19,25 +19,25 @@ contract c {
|
||||
// ----
|
||||
// f(uint256): 0 -> 0x20, 0x00
|
||||
// f(uint256): 31 -> 0x20, 0x1f, 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e00
|
||||
// gas irOptimized: 221724
|
||||
// gas irOptimized: 221696
|
||||
// gas legacy: 255464
|
||||
// gas legacyOptimized: 250998
|
||||
// f(uint256): 32 -> 0x20, 0x20, 1780731860627700044960722568376592200742329637303199754547598369979440671
|
||||
// gas irOptimized: 229335
|
||||
// gas irOptimized: 229291
|
||||
// gas legacy: 267931
|
||||
// gas legacyOptimized: 263329
|
||||
// f(uint256): 33 -> 0x20, 33, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x2000000000000000000000000000000000000000000000000000000000000000
|
||||
// gas irOptimized: 238042
|
||||
// gas irOptimized: 238003
|
||||
// gas legacy: 277538
|
||||
// gas legacyOptimized: 272818
|
||||
// f(uint256): 63 -> 0x20, 0x3f, 1780731860627700044960722568376592200742329637303199754547598369979440671, 14532552714582660066924456880521368950258152170031413196862950297402215316992
|
||||
// gas irOptimized: 348712
|
||||
// gas irOptimized: 348673
|
||||
// gas legacy: 423428
|
||||
// gas legacyOptimized: 414868
|
||||
// f(uint256): 12 -> 0x20, 0x0c, 0x0102030405060708090a0b0000000000000000000000000000000000000000
|
||||
// gas legacy: 106445
|
||||
// gas legacyOptimized: 104379
|
||||
// f(uint256): 129 -> 0x20, 0x81, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f, 29063324697304692433803953038474361308315562010425523193971352996434451193439, 0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f, -57896044618658097711785492504343953926634992332820282019728792003956564819968
|
||||
// gas irOptimized: 802359
|
||||
// gas irOptimized: 802315
|
||||
// gas legacy: 954517
|
||||
// gas legacyOptimized: 937521
|
||||
|
@ -37,7 +37,7 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0x40, 0x80, 6, 0x6162636465660000000000000000000000000000000000000000000000000000, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000
|
||||
// gas irOptimized: 172318
|
||||
// gas irOptimized: 172282
|
||||
// gas legacy: 174794
|
||||
// gas legacyOptimized: 174188
|
||||
// g() -> 0x40, 0xc0, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000, 0x11, 0x3132333435363738393233343536373839000000000000000000000000000000
|
||||
|
@ -48,6 +48,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0xff
|
||||
// gas irOptimized: 136164
|
||||
// gas irOptimized: 136027
|
||||
// gas legacy: 137645
|
||||
// gas legacyOptimized: 134376
|
||||
|
@ -9,7 +9,7 @@ contract c {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// set(): 1, 2, 3, 4, 5 -> true
|
||||
// gas irOptimized: 163680
|
||||
// gas irOptimized: 163657
|
||||
// gas legacy: 163756
|
||||
// gas legacyOptimized: 163596
|
||||
// storageEmpty -> 0
|
||||
|
@ -20,6 +20,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 3
|
||||
// gas irOptimized: 173135
|
||||
// gas irOptimized: 173108
|
||||
// gas legacy: 179707
|
||||
// gas legacyOptimized: 178763
|
||||
|
@ -13,6 +13,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0x20, 0x02, 0x40, 0x80, 3, 0x6162630000000000000000000000000000000000000000000000000000000000, 0x99, 44048183304486788312148433451363384677562265908331949128489393215789685032262, 32241931068525137014058842823026578386641954854143559838526554899205067598957, 49951309422467613961193228765530489307475214998374779756599339590522149884499, 0x54555658595a6162636465666768696a6b6c6d6e6f707172737475767778797a, 0x4142434445464748494a4b4c4d4e4f5051525354555658595a00000000000000
|
||||
// gas irOptimized: 198279
|
||||
// gas irOptimized: 198253
|
||||
// gas legacy: 199159
|
||||
// gas legacyOptimized: 198137
|
||||
|
@ -18,6 +18,6 @@ contract c {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// test1() -> true
|
||||
// gas irOptimized: 527501
|
||||
// gas irOptimized: 527479
|
||||
// gas legacy: 613377
|
||||
// gas legacyOptimized: 606411
|
||||
|
@ -19,5 +19,5 @@ contract c {
|
||||
// ----
|
||||
// test((uint16,uint16,uint16[3],uint16[])): 0x20, 2, 3, 0, 0, 4, 0xC0, 4, 0, 0, 5, 0, 0 -> 2, 3, 4, 5
|
||||
// gas irOptimized: 147998
|
||||
// gas legacy: 152444
|
||||
// gas legacy: 152522
|
||||
// gas legacyOptimized: 146671
|
||||
|
@ -26,6 +26,6 @@ contract Creator {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h"
|
||||
// gas irOptimized: 330976
|
||||
// gas irOptimized: 330957
|
||||
// gas legacy: 422873
|
||||
// gas legacyOptimized: 292281
|
||||
|
@ -19,5 +19,5 @@ contract C {
|
||||
// ----
|
||||
// f(uint32,(uint128,uint256[][2],uint32)): 55, 0x40, 77, 0x60, 88, 0x40, 0x40, 2, 1, 2 -> 55, 77, 1, 2, 88
|
||||
// gas irOptimized: 197768
|
||||
// gas legacy: 205149
|
||||
// gas legacy: 205266
|
||||
// gas legacyOptimized: 196983
|
||||
|
@ -25,7 +25,7 @@ contract c {
|
||||
// ----
|
||||
// storageEmpty -> 1
|
||||
// set(uint256,bytes,uint256): 12, 0x60, 13, 33, "12345678901234567890123456789012", "3" -> true
|
||||
// gas irOptimized: 124227
|
||||
// gas irOptimized: 124205
|
||||
// gas legacy: 124736
|
||||
// gas legacyOptimized: 124179
|
||||
// test(uint256): 32 -> "3"
|
||||
|
@ -36,6 +36,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(bytes): 0x20, 0x5, "abcde" -> 0
|
||||
// gas irOptimized: 241342
|
||||
// gas irOptimized: 241328
|
||||
// gas legacy: 239258
|
||||
// gas legacyOptimized: 238582
|
||||
|
@ -22,6 +22,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// g() -> 2, 6
|
||||
// gas irOptimized: 169804
|
||||
// gas irOptimized: 169790
|
||||
// gas legacy: 172490
|
||||
// gas legacyOptimized: 171209
|
||||
|
Loading…
Reference in New Issue
Block a user