Skip to content

Commit c33e933

Browse files
committed
mload is actually cheaper than jump
1 parent ce1c5ad commit c33e933

File tree

2 files changed

+6
-32
lines changed

2 files changed

+6
-32
lines changed

contracts/utils/Base58.sol

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,11 @@ library Base58 {
6565
// Assembly is ~50% cheaper for buffers of size 32.
6666
assembly ("memory-safe") {
6767
function clzBytes(ptr, length) -> i {
68-
let chunk
6968
for {
7069
i := 0
71-
} lt(i, length) {
70+
} and(iszero(shr(248, mload(add(ptr, i)))), lt(i, length)) {
7271
i := add(i, 1)
73-
} {
74-
// Every 32 bytes, load a new chunk
75-
if iszero(mod(i, 0x20)) {
76-
chunk := mload(add(ptr, i))
77-
}
78-
// If the first byte of the chunk is not zero, break
79-
if shr(248, chunk) {
80-
break
81-
}
82-
// Shift chunk
83-
chunk := shl(8, chunk)
84-
}
72+
} {}
8573
}
8674

8775
encoded := mload(0x40)

contracts/utils/Bytes.sol

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,14 @@ library Bytes {
7979
* @dev Count number of occurrences of `search` in `buffer`, starting from position `offset`.
8080
*/
8181
function countConsecutive(bytes memory buffer, uint256 offset, bytes1 search) internal pure returns (uint256 i) {
82-
uint256 length = buffer.length;
83-
if (offset > length) return 0;
84-
82+
uint256 length = Math.saturatingSub(buffer.length, offset);
8583
assembly ("memory-safe") {
86-
let chunk
87-
let end := sub(length, offset)
8884
for {
85+
let ptr := add(add(buffer, 0x20), offset)
8986
i := 0
90-
} lt(i, end) {
87+
} and(iszero(shr(248, xor(mload(add(ptr, i)), search))), lt(i, length)) {
9188
i := add(i, 1)
92-
} {
93-
// every 32 bytes, load a new chunk
94-
if iszero(mod(i, 0x20)) {
95-
chunk := mload(add(buffer, add(0x20, add(offset, i))))
96-
}
97-
// if the first byte of the chunk does not match the search element, exit
98-
if shr(248, xor(chunk, search)) {
99-
break
100-
}
101-
// shift chunk
102-
chunk := shl(8, chunk)
103-
}
89+
} {}
10490
}
10591
}
10692

0 commit comments

Comments
 (0)