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

perf: use DEBRUIJN_SEQ #8

Merged
merged 9 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

33 changes: 0 additions & 33 deletions .eslintrc.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/deployer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install --ignore-scripts
- run: npm publish --access public
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ '18.10' ]
node-version: [ '20.9' ]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
16 changes: 12 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ '18.10' ]
node-version: [ '20.9' ]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down Expand Up @@ -55,17 +55,21 @@ jobs:
run: forge test -vv

hardhat-coverage:
name: Hardhat Coverage CI
name: Forge Coverage CI
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ '18.10' ]
node-version: [ '20.9' ]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
Expand All @@ -85,8 +89,12 @@ jobs:
run: npm list
- name: Install Dependencies
run: npm install --ignore-scripts
- name: Install Forge
run: forge install
- name: Foundry Forge test
run: forge test -vv
- name: Hardhat coverage
run: npm run hardhat:coverage
run: npm run coverage:forge
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.10.0
v20.9.0
18 changes: 4 additions & 14 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120,
"trailingComma": "all",
"semi": false,
"arrowParens": "always",
"endOfLine": "auto",
"overrides": [
{
"files": "*.sol",
"options": {
"singleQuote": false,
"semi": true,
"printWidth": 120,
"explicitTypes": "always"
}
}
]
}
"endOfLine": "auto"
}
19 changes: 0 additions & 19 deletions .solhint.json

This file was deleted.

1 change: 0 additions & 1 deletion .solhintignore

This file was deleted.

7 changes: 2 additions & 5 deletions contracts/DirtyUint64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.0;

library DirtyUint64 {
error DirtyUint64Error(uint256 errorCode);

uint256 private constant _OVERFLOW_ERROR = 0;
uint256 private constant _UNDERFLOW_ERROR = 1;

Expand Down Expand Up @@ -69,11 +70,7 @@ library DirtyUint64 {
}
}

function sumPackedUnsafe(
uint256 packed,
uint256 from,
uint256 to
) internal pure returns (uint64 ret) {
function sumPackedUnsafe(uint256 packed, uint256 from, uint256 to) internal pure returns (uint64 ret) {
packed = packed >> (from << 6);
unchecked {
for (uint256 i = from; i < to; ++i) {
Expand Down
87 changes: 37 additions & 50 deletions contracts/OctopusHeap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import "./PackedUint256.sol";
import "./SignificantBit.sol";

/**
🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙

Octopus Heap
by Clober

⢀⣀⣠⣀⣀⡀
⣠⣾⣿⣿⣿⣿⣿⣿⣷⣦⡀
⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀ ⣠⣶⣾⣷⣶⣄
⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧ ⢰⣿⠟⠉⠻⣿⣿⣷
⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⢷⣄⠘⠿ ⢸⣿⣿⡆
⠈⠿⣿⣿⣿⣿⣿⣀⣸⣿⣷⣤⣴⠟ ⢀⣼⣿⣿⠁
⠈⠙⣛⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⣀⣀⣴⣾⣿⣿⡟
⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠋⣠⣤⣀
⣴⣿⣿⣿⠿⠟⠛⠛⢛⣿⣿⣿⣿⣿⣿⣧⡈⠉⠁ ⠈⠉⢻⣿⣧
⣼⣿⣿⠋ ⢠⣾⣿⣿⠟⠉⠻⣿⣿⣿⣦⣄ ⣸⣿⣿⠃
⣿⣿⡇ ⣿⣿⡿⠃ ⠈⠛⢿⣿⣿⣿⣿⣶⣿⣿⣿⡿⠋
⢿⣿⣧⡀ ⣶⣄⠘⣿⣿⡇ ⠠⠶⣿⣶⡄⠈⠙⠛⠻⠟⠛⠛⠁
⠈⠻⣿⣿⣿⣿⠏ ⢻⣿⣿⣄ ⣸⣿⡇
⠻⣿⣿⣿⣶⣾⣿⣿⠃
⠈⠙⠛⠛⠛⠋

🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙
*/

* 🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙
*
* Octopus Heap
* by Clober
*
* ⢀⣀⣠⣀⣀⡀
* ⣠⣾⣿⣿⣿⣿⣿⣿⣷⣦⡀
* ⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀ ⣠⣶⣾⣷⣶⣄
* ⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧ ⢰⣿⠟⠉⠻⣿⣿⣷
* ⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⢷⣄⠘⠿ ⢸⣿⣿⡆
* ⠈⠿⣿⣿⣿⣿⣿⣀⣸⣿⣷⣤⣴⠟ ⢀⣼⣿⣿⠁
* ⠈⠙⣛⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⣀⣀⣴⣾⣿⣿⡟
* ⢀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠋⣠⣤⣀
* ⣴⣿⣿⣿⠿⠟⠛⠛⢛⣿⣿⣿⣿⣿⣿⣧⡈⠉⠁ ⠈⠉⢻⣿⣧
* ⣼⣿⣿⠋ ⢠⣾⣿⣿⠟⠉⠻⣿⣿⣿⣦⣄ ⣸⣿⣿⠃
* ⣿⣿⡇ ⣿⣿⡿⠃ ⠈⠛⢿⣿⣿⣿⣿⣶⣿⣿⣿⡿⠋
* ⢿⣿⣧⡀ ⣶⣄⠘⣿⣿⡇ ⠠⠶⣿⣶⡄⠈⠙⠛⠻⠟⠛⠛⠁
* ⠈⠻⣿⣿⣿⣿⠏ ⢻⣿⣿⣄ ⣸⣿⡇
* ⠻⣿⣿⣿⣶⣾⣿⣿⠃
* ⠈⠙⠛⠛⠛⠋
*
* 🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙
*/
library OctopusHeap {
using PackedUint256 for uint256;
using SignificantBit for uint256;

error OctopusHeapError(uint256 errorCode);

uint256 private constant _ALREADY_INITIALIZED_ERROR = 0;
uint256 private constant _HEAP_EMPTY_ERROR = 1;
uint256 private constant _ALREADY_EXISTS_ERROR = 2;
Expand Down Expand Up @@ -92,24 +92,19 @@ library OctopusHeap {
return core.heap[heapIndex >> _HEAD_SIZE_P].get8Unsafe(heapIndex & _HEAD_SIZE);
}

function _getWordIndex(
uint256 head,
uint256 arm,
uint16 heapIndex
) private pure returns (uint8) {
function _getWordIndex(uint256 head, uint256 arm, uint16 heapIndex) private pure returns (uint8) {
if (heapIndex <= _HEAD_SIZE) {
return head.get8Unsafe(heapIndex);
}
return arm.get8Unsafe(heapIndex & _HEAD_SIZE);
}

// returns new values for the part of the heap affected by updating value at heapIndex to new value
function _updateWordIndex(
uint256 head,
uint256 arm,
uint16 heapIndex,
uint8 newWordIndex
) private pure returns (uint256, uint256) {
function _updateWordIndex(uint256 head, uint256 arm, uint16 heapIndex, uint8 newWordIndex)
private
pure
returns (uint256, uint256)
{
if (heapIndex <= _HEAD_SIZE) {
return (head.update8Unsafe(heapIndex, newWordIndex), arm);
} else {
Expand Down Expand Up @@ -233,18 +228,10 @@ library OctopusHeap {
core.bitmap[wordIndex] = word | mask;
}

function _pop(
Core storage core,
uint256 head,
uint256[] memory arms
)
function _pop(Core storage core, uint256 head, uint256[] memory arms)
private
view
returns (
uint256,
uint16,
uint256
)
returns (uint256, uint16, uint256)
{
uint8 newLength;
uint256 arm;
Expand Down Expand Up @@ -296,11 +283,11 @@ library OctopusHeap {
return (head, bodyPartIndex, arm);
}

function popInMemory(
Core storage core,
uint256 word,
uint256[] memory heap
) internal view returns (uint256, uint256[] memory) {
function popInMemory(Core storage core, uint256 word, uint256[] memory heap)
internal
view
returns (uint256, uint256[] memory)
{
uint8 rootBitIndex = word.leastSignificantBit();
uint256 mask = 1 << rootBitIndex;
if (word != mask) return (word & (~mask), heap);
Expand Down
Loading