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

GC: Struct packing #546

Merged
merged 7 commits into from
Feb 28, 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
1 change: 1 addition & 0 deletions conf/rulesets/solhint-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = Object.freeze({
'gas-indexed-events': 'warn',
'gas-multitoken1155': 'warn',
'gas-small-strings': 'warn',
'gas-struct-packing': 'warn',
'comprehensive-interface': 'warn',
quotes: ['error', 'double'],
'const-name-snakecase': 'warn',
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ title: "Rule Index of Solhint"
| [gas-indexed-events](./rules/gas-consumption/gas-indexed-events.md) | Suggest indexed arguments on events for uint, bool and address | | |
| [gas-multitoken1155](./rules/gas-consumption/gas-multitoken1155.md) | ERC1155 is a cheaper non-fungible token than ERC721 | | |
| [gas-small-strings](./rules/gas-consumption/gas-small-strings.md) | Keep strings smaller than 32 bytes | | |
| [gas-struct-packing](./rules/gas-consumption/gas-struct-packing.md) | Suggest to re-arrange struct packing order when it is inefficient | | |


## Miscellaneous
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/gas-consumption/gas-struct-packing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
warning: "This is a dynamically generated file. Do not edit manually."
layout: "default"
title: "gas-struct-packing | Solhint"
---

# gas-struct-packing
![Category Badge](https://img.shields.io/badge/-Gas%20Consumption%20Rules-informational)
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow)

## Description
Suggest to re-arrange struct packing order when it is inefficient

## Options
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn.

### Example Config
```json
{
"rules": {
"gas-struct-packing": "warn"
}
}
```

### Notes
- This rule assumes all UserDefinedTypeName take a new slot. (beware of Enums inside Structs)
- [source 1](https://coinsbench.com/comprehensive-guide-tips-and-tricks-for-gas-optimization-in-solidity-5380db734404) of the rule initiative (Variable Packing)
- [source 2](https://www.rareskills.io/post/gas-optimization?postId=c9db474a-ff97-4fa3-a51d-fe13ccb8fe3b#viewer-f8m1r) of the rule initiative

## Examples
This rule does not have examples.

## Version
This rule is introduced in the latest version.

## Resources
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/gas-consumption/gas-struct-packing.js)
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/gas-consumption/gas-struct-packing.md)
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/gas-consumption/gas-struct-packing.js)
4 changes: 0 additions & 4 deletions lib/rules/gas-consumption/gas-increment-by-one.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable */
const { isArray } = require('lodash')
const BaseChecker = require('../base-checker')

const operators = ['+', '-', '++', '--']
const binaryOperators = ['+', '-', '+=', '-=']

const ruleId = 'gas-increment-by-one'
const meta = {
type: 'gas-consumption',
Expand Down
184 changes: 184 additions & 0 deletions lib/rules/gas-consumption/gas-struct-packing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* eslint-disable */
const BaseChecker = require('../base-checker')

const ruleId = 'gas-struct-packing'
const meta = {
type: 'gas-consumption',

docs: {
description: 'Suggest to re-arrange struct packing order when it is inefficient',
category: 'Gas Consumption Rules',
notes: [
{
note: 'This rule assumes all UserDefinedTypeName take a new slot. (beware of Enums inside Structs) ',
},
{
note: '[source 1](https://coinsbench.com/comprehensive-guide-tips-and-tricks-for-gas-optimization-in-solidity-5380db734404) of the rule initiative (Variable Packing)',
},
{
note: '[source 2](https://www.rareskills.io/post/gas-optimization?postId=c9db474a-ff97-4fa3-a51d-fe13ccb8fe3b#viewer-f8m1r) of the rule initiative',
},
],
},

isDefault: false,
recommended: false,
defaultSetup: 'warn',

schema: null,
}

class GasStructPacking extends BaseChecker {
constructor(reporter) {
super(reporter, ruleId, meta)
}

StructDefinition(node) {
const reportError = this.isInefficientlyPacked(node)
if (reportError) {
this.reportError(node)
}
}

isInefficientlyPacked(structNode) {
if (!structNode || !Array.isArray(structNode.members) || structNode.members.length <= 1) {
// if (!structNode || !Array.isArray(structNode.members)) {
return false // Early return for structs with 1 or no members
}

let members = structNode.members.map((member) => ({
name: member.name,
size: this.getVariableSize(member.typeName),
type: member.typeName.type,
}))

const canBeImproved = this.analyzePacking(members)
return canBeImproved
}

// Function to calculate the optimal slots needed for given members
calculateOptimalSlots(arr) {
let totalSize = 0
arr.forEach(({ size }) => {
totalSize += size
})
return Math.ceil(totalSize / 32)
}

calculateCurrentSlotsUsed(members) {
let slotsUsed = 0;
let currentSlotSpace = 0;

members.forEach(member => {
if (member.size === 32) {
if (currentSlotSpace > 0) {
slotsUsed += 1; // Finish the current slot if it was partially filled
currentSlotSpace = 0;
}
slotsUsed += 1; // This member occupies a full slot
} else {
if (currentSlotSpace + member.size > 32) {
slotsUsed += 1; // Move to the next slot if adding this member exceeds the current slot
currentSlotSpace = member.size; // Start filling the next slot
} else {
currentSlotSpace += member.size; // Add to the current slot space
}
}
});

// If there's any space used in the current slot after looping, it means another slot is partially filled
if (currentSlotSpace > 0) {
slotsUsed += 1;
}

return slotsUsed;
}


analyzePacking(members) {
// Separate members into large and small for analysis
// const largeMembers = members.filter((member) => member.size === 32)
const smallMembers = members.filter((member) => member.size < 32)

// Sort small members by size, descending, to optimize packing
smallMembers.sort((a, b) => b.size - a.size)

// Initial slots count: one slot per large member
let currentSlots = this.calculateCurrentSlotsUsed(members)

// Track remaining space in the current slot
let remainingSpace = 32
smallMembers.forEach((member) => {
if (member.size <= remainingSpace) {
// If the member fits in the current slot, subtract its size from the remaining space
remainingSpace -= member.size
} else {
// If not, start a new slot and adjust remaining space
remainingSpace = 32 - member.size
}
})

// Calculate optimal slots needed if perfectly packed
const optimalSlots = this.calculateOptimalSlots(members)

// console.log(`\n\nCurrent slot usage: ${currentSlots}`)
// console.log(`Optimal (minimum possible) slot usage: ${optimalSlots}`)

return currentSlots > optimalSlots
}

getVariableSize(typeNode) {
if (!typeNode) {
return 32
}

switch (typeNode.type) {
case 'ElementaryTypeName':
return this.getSizeForElementaryType(typeNode.name)
case 'UserDefinedTypeName':
// this can be improved for enums
return 32
case 'ArrayTypeName':
if (typeNode.length) {
// if array is fixed, get the length * var size
const varSize = this.getSizeForElementaryType(typeNode.baseTypeName.name)
const size = parseInt(typeNode.length.number) * varSize
return size > 32 ? 32 : size
}
return 32 // Dynamic arrays occupy a full slot

default:
return 32
}
}

getSizeForElementaryType(typeName) {
switch (typeName) {
case 'address':
return 20
case 'bool':
return 1
case 'string':
case 'bytes':
return 32
default:
return typeName.includes('uint') || typeName.includes('int')
? this.getSizeForIntType(typeName)
: 32
}
}

getSizeForIntType(typeName) {
const bits = parseInt(typeName.replace(/\D/g, ''), 10)
return Math.ceil(bits / 8)
}

reportError(node) {
this.error(
node,
`GC: For [ ${node.name} ] struct, packing seems inefficient. Try rearranging to achieve 32bytes slots`
)
}
}

module.exports = GasStructPacking
7 changes: 4 additions & 3 deletions lib/rules/gas-consumption/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ const GasMultitoken1155 = require('./gas-multitoken1155')
const GasSmallStrings = require('./gas-small-strings')
const GasIndexedEvents = require('./gas-indexed-events')
const GasCalldataParameters = require('./gas-calldata-parameters')
const IncrementByOne = require('./gas-increment-by-one')
const GasIncrementByOne = require('./gas-increment-by-one')
const GasStructPacking = require('./gas-struct-packing')

// module.exports = function checkers(reporter, config, tokens) {
module.exports = function checkers(reporter, config) {
return [
new GasMultitoken1155(reporter, config),
new GasSmallStrings(reporter, config),
new GasIndexedEvents(reporter, config),
new GasCalldataParameters(reporter, config),
new IncrementByOne(reporter, config),
new GasIncrementByOne(reporter, config),
new GasStructPacking(reporter, config),
]
}
Loading
Loading