Gas cost of getting the whole struct vs getting struct single values

I wanted to know if it is worth it to make a lookup for the whole struct and parse info from it or to always get single values with getters from it to optimize.
So the value of gas needed for most single lookup is around 2900 gas or less.
But the value for the whole struct lookup is 9861 gas, which means that if the lookup is using less than 3 values it’s more worth it to do a single lookup for each value in a struct.
This changes as the stuct is larger, if there are more values in it then it’s less worth it to do a lookup of the whole struct and more sense to do lookup for just one value whish is kind of obvious.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract StakingContract {
    struct Stake {
        // Overlay of the node that is being staked
        bytes32 overlay;
        // Amount of tokens staked
        uint256 stakeAmount;
        // Block height the stake was updated
        uint256 lastUpdatedBlockNumber;
        // Used to indicate presence in stakes struct
        bool isValue;
    }

    // Associate every stake id with node address data.
    mapping(address => Stake) public stakes;

    // Function to add or update a stake
    function setStake(address _nodeAddress, bytes32 _overlay, uint256 _stakeAmount) public {
        stakes[_nodeAddress] = Stake({
            overlay: _overlay,
            stakeAmount: _stakeAmount,
            lastUpdatedBlockNumber: block.number,
            isValue: true
        });
    }

    // Getter for overlay
    function getOverlay(address _nodeAddress) public view returns (bytes32) {
        return stakes[_nodeAddress].overlay;
    }

    // Getter for stakeAmount
    function getStakeAmount(address _nodeAddress) public view returns (uint256) {
        return stakes[_nodeAddress].stakeAmount;
    }

    // Getter for lastUpdatedBlockNumber
    function getLastUpdatedBlockNumber(address _nodeAddress) public view returns (uint256) {
        return stakes[_nodeAddress].lastUpdatedBlockNumber;
    }


    // Getter for the whole struct
    function getStake(address _nodeAddress) public view returns (Stake memory) {
        return stakes[_nodeAddress];
    }
}
Subscribe to N00b21337
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.