惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
C
Check Point Blog
B
Blog RSS Feed
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
S
SegmentFault 最新的问题
博客园 - Franky
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
B
Blog
Jina AI
Jina AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Gas Optimization Part 4: Solidity Tips for Cheaper Contracts
Mahima Thacker · 2026-05-29 · via DEV Community

Mahima Thacker

Every line of your smart contract costs something.

Some lines cost more than others.

In this part of our gas saving series, we’ll explore how to write smarter Solidity code that keeps your contract lean and efficient.

Here are six simple and practical ways to reduce gas costs while writing Solidity smart contracts.

1. Use payable Only When Needed, But Know It Saves Gas

In Solidity, a function marked payable can actually use slightly less gas than a non-payable one.

Even if you're not sending ETH, the EVM skips some internal checks when the function is marked payable.

See this example:

function hello() external payable {}    // 21,137 gas

function hello2() external {}           // 21,161 gas

That tiny difference may not seem like much, but across thousands of calls, it adds up.

Only use payable when your function is actually meant to accept ETH

2. Use unchecked for Safe Arithmetic When You’re Sure

Since Solidity 0.8.0, all arithmetic operations automatically check for overflows and underflows. While this makes contracts safer, it also uses extra gas. When you're certain that overflow won't occur, you can use the unchecked keyword to skip these safety checks.

uint256 public myNumber = 0;

function increment() external {

unchecked {

myNumber++;

}

}

Gas used: 24,347 (much cheaper than using safe math)

Warning: Use unchecked carefully. Only when you're confident there's no risk of overflow.

3. Turn On the Solidity Optimizer

The Solidity Optimizer is like a smart helper that cleans up and tightens your compiled bytecode.

It does not change how your contract works, but it removes waste and makes it cheaper to run.

If you’re using tools like Hardhat or Remix, always enable the Optimizer before deploying to mainnet.

4. Use uint256 Instead of Smaller Integers (Most of the Time)

Smaller types like uint8 or uint16 might look more efficient, but they can cost more gas during execution.

That’s because the EVM automatically converts them to uint256 behind the scenes.

So, if you're not tightly packing them in a struct for storage savings, just use uint256.

Use smaller types only in structs when trying to save storage space.

5. Understand Storage Costs: Read + Write Costs Almost Same as Write

Storage operations are expensive. But here’s something surprising:

Reading a storage variable before writing to it doesn’t cost much more than writing directly.

Example:

Read + Write = 2,100 (read) + 20,100 (write) = 22,200 gas
Write only = 22,100 gas

That means if your code needs to read before writing, it’s okay, you are not losing much.

Plan your storage usage wisely. Reuse variables and avoid unnecessary writes.

Reference: https://ethereum.github.io/yellowpaper/paper.pdf

6. Use < Instead of <= in Comparisons

When comparing numbers, use < instead of <=.

Why? Because:

< needs just one check

<= takes two checks (a comparison and an extra step to flip the result)

Fewer steps mean lower gas usage.

Example:

for (uint i = 0; i < limit; i++) {

...

}

This small change saves gas on every loop iteration and comparison operation.

Conclusion

Gas optimization is about understanding how the EVM works and making informed decisions about your code structure. Each of these techniques might seem to save small amounts of gas individually, but combined and applied across a large contract, they can result in significant cost savings.

Remember: Always test your optimizations thoroughly. Sometimes gas savings come at the cost of code readability or safety. Strike the right balance for your specific use case.

Key Takeaways:

  • Use payable functions when appropriate
  • Apply unchecked carefully for safe arithmetic
  • Enable the Solidity optimizer
  • Prefer uint256 for most variables
  • Understand storage access patterns
  • Choose efficient comparison operators

By implementing these strategies, you'll create contracts that not only work well but also cost less to deploy and interact with, making them more accessible to users and more profitable for developers.