Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Stamp): Gas golfing #174

Merged
merged 24 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
76567de
call require sooner to save gas on failed trx
0xCardinalError May 16, 2023
a2feb8a
proper grouping of functions and variables for better readability
0xCardinalError May 16, 2023
b85a290
Merge remote-tracking branch 'origin' into improvments_stamp
0xCardinalError Jun 20, 2023
d1e303a
reorder functions, add better visuals
0xCardinalError Jun 20, 2023
db5c3fd
packing in slot
0xCardinalError Aug 7, 2023
92fda9c
Merge branch 'master' of https://github.com/ethersphere/storage-incen…
0xCardinalError Aug 7, 2023
7ab5081
optimize top ups
0xCardinalError Aug 7, 2023
34416e1
fix batch calc
0xCardinalError Aug 7, 2023
2953cfe
improve depth increase storage
0xCardinalError Aug 7, 2023
dca92dd
add unchecked increment
0xCardinalError Aug 7, 2023
8557150
change to memory
0xCardinalError Aug 7, 2023
fbe162e
pack up too big vars
0xCardinalError Aug 8, 2023
5472269
use defaults
0xCardinalError Aug 8, 2023
d87bfd5
repack positions to use only 9 slots
0xCardinalError Aug 8, 2023
86ac9b8
Merge branch 'master' of https://github.com/ethersphere/storage-incen…
0xCardinalError Aug 22, 2023
8c00b39
replace requites with custom errors
0xCardinalError Aug 22, 2023
9ee2d93
fix tests
0xCardinalError Aug 22, 2023
9623acb
remove obsl
0xCardinalError Aug 22, 2023
4f52038
add custom errors to Hitchens
0xCardinalError Aug 24, 2023
1585554
use just equal as its uint
0xCardinalError Aug 30, 2023
9a07565
use just equal as its uint, second func
0xCardinalError Aug 30, 2023
47f96e6
Merge branch 'master' into gas_golf_stamp
0xCardinalError Sep 4, 2023
c3d4aa8
fix events
0xCardinalError Sep 4, 2023
20aa63e
change var name to local
0xCardinalError Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/OrderStatisticsTree/HitchensOrderStatisticsTreeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ library HitchensOrderStatisticsTreeLib {
mapping(uint => Node) nodes;
}

error ValueDoesNotExist(); // Provided value doesn't exist in the tree.
error ValueCannotBeZero(); // Value to insert cannot be zero
error ValueKeyPairExists(); // Value and Key pair exists. Cannot be inserted again.

function first(Tree storage self) internal view returns (uint _value) {
_value = self.root;
if (_value == EMPTY) return 0;
Expand All @@ -76,7 +80,10 @@ library HitchensOrderStatisticsTreeLib {
Tree storage self,
uint value
) internal view returns (uint _parent, uint _left, uint _right, bool _red, uint keyCount, uint __count) {
require(exists(self, value), "OrderStatisticsTree(403) - Value does not exist.");
if (!exists(self, value)) {
revert ValueDoesNotExist();
}

Node storage gn = self.nodes[value];
return (gn.parent, gn.left, gn.right, gn.red, gn.keys.length, gn.keys.length + gn.count);
}
Expand All @@ -87,7 +94,9 @@ library HitchensOrderStatisticsTreeLib {
}

function valueKeyAtIndex(Tree storage self, uint value, uint index) internal view returns (bytes32 _key) {
require(exists(self, value), "OrderStatisticsTree(404) - Value does not exist.");
if (!exists(self, value)) {
revert ValueDoesNotExist();
}
return self.nodes[value].keys[index];
}

Expand Down Expand Up @@ -219,11 +228,12 @@ library HitchensOrderStatisticsTreeLib {
*/

function insert(Tree storage self, bytes32 key, uint value) internal {
require(value != EMPTY, "OrderStatisticsTree(405) - Value to insert cannot be zero");
require(
!keyExists(self, key, value),
"OrderStatisticsTree(406) - Value and Key pair exists. Cannot be inserted again."
);
if (value == EMPTY) {
revert ValueCannotBeZero();
}
if (keyExists(self, key, value)) {
revert ValueKeyPairExists();
}
uint cursor;
uint probe = self.root;
while (probe != EMPTY) {
Expand Down Expand Up @@ -257,16 +267,23 @@ library HitchensOrderStatisticsTreeLib {
}

function remove(Tree storage self, bytes32 key, uint value) internal {
require(value != EMPTY, "OrderStatisticsTree(407) - Value to delete cannot be zero");
require(keyExists(self, key, value), "OrderStatisticsTree(408) - Value to delete does not exist.");
if (value == EMPTY) {
revert ValueCannotBeZero();
}
if (!keyExists(self, key, value)) {
revert ValueDoesNotExist();
}

Node storage nValue = self.nodes[value];
uint rowToDelete = nValue.keyMap[key];
bytes32 last = nValue.keys[nValue.keys.length - uint(1)];
nValue.keys[rowToDelete] = last;
nValue.keyMap[last] = rowToDelete;
nValue.keys.pop();

uint probe;
uint cursor;

if (nValue.keys.length == 0) {
if (self.nodes[value].left == EMPTY || self.nodes[value].right == EMPTY) {
cursor = value;
Expand All @@ -276,13 +293,16 @@ library HitchensOrderStatisticsTreeLib {
cursor = self.nodes[cursor].left;
}
}

if (self.nodes[cursor].left != EMPTY) {
probe = self.nodes[cursor].left;
} else {
probe = self.nodes[cursor].right;
}

uint cursorParent = self.nodes[cursor].parent;
self.nodes[probe].parent = cursorParent;

if (cursorParent != EMPTY) {
if (cursor == self.nodes[cursorParent].left) {
self.nodes[cursorParent].left = probe;
Expand All @@ -292,7 +312,9 @@ library HitchensOrderStatisticsTreeLib {
} else {
self.root = probe;
}

bool doFixup = !self.nodes[cursor].red;

if (cursor != value) {
replaceParent(self, cursor, value);
self.nodes[cursor].left = self.nodes[value].left;
Expand All @@ -303,9 +325,11 @@ library HitchensOrderStatisticsTreeLib {
(cursor, value) = (value, cursor);
fixCountRecurse(self, value);
}

if (doFixup) {
removeFixup(self, probe);
}

fixCountRecurse(self, cursorParent);
delete self.nodes[cursor];
}
Expand Down
Loading