Skip to content

Optimize type(uint256).max into PUSH0 NOT #15761

Open
@radeksvarz

Description

@radeksvarz

Abstract and Motivation

type(uint256).max is often used as the indicator for something unlimited, such as approvals.

Solidity compiles type(uint256).max into PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

This requires 33 bytes in the code (3 gas) for each situation type(uint256).max is used (such as comparison).

I propose much shorter approach for the case the optimiser is set to optimise bytecode length.

Compile to this instead (in the case the optimiser is set to optimise bytecode length):

PUSH0
NOT

This results in to 2 bytes code and 5 gas.

Backwards Compatibility

The optimised bytecode is shorter (-31 bytes), but has +2 gas. Functionally the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions