DatingDapp

First Flight #33
Beginner FriendlyFoundrySolidityNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

[G-4] Inefficient storage reads in tokenURI function wastes gas (Multiple SLOAD Operations)

Description: In SoulboundProfileNFT.sol, the tokenURI function performs multiple separate storage reads from the same struct, which is inefficient. Instead of loading the entire struct into memory once, it loads each field separately.

function tokenURI(uint256 tokenId) public view returns (string memory) {
// Current implementation - 3 separate storage reads
@> string memory profileName = _profiles[tokenId].name; // SLOAD
@> uint256 profileAge = _profiles[tokenId].age; // SLOAD
@> string memory imageURI = _profiles[tokenId].profileImage; // SLOAD

Impact:

  • Each storage read (SLOAD) costs ~100 gas

  • Current implementation uses 3 separate SLOADs (~300 gas)

  • This happens every time tokenURI is called

Proof of Concept:
Current implementation with multiple storage reads:

// 3 separate SLOAD operations (3 * 100 = 300 gas)
string memory profileName = _profiles[tokenId].name; // 100 gas
uint256 profileAge = _profiles[tokenId].age; // 100 gas
string memory imageURI = _profiles[tokenId].profileImage; // 100 gas

Recommended Mitigation: Cache the struct in memory with a single SLOAD:

function tokenURI(uint256 tokenId) public view returns (string memory) {
- string memory profileName = _profiles[tokenId].name;
- uint256 profileAge = _profiles[tokenId].age;
- string memory imageURI = _profiles[tokenId].profileImage;
+ Profile memory profile = _profiles[tokenId];
+ string memory profileName = profile.name;
+ uint256 profileAge = profile.age;
+ string memory imageURI = profile.profileImage;

This optimization:

  • Reduces storage reads from 3 to 1

  • Memory reads (MLOAD) are much cheaper (3 gas) than storage reads (SLOAD)

Updates

Appeal created

n0kto Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.