The Ordinary Programmer

The Ordinary Programmer is for coders who embrace simplicity and practicality. We share real-world tips, project stories, and tech insights for developers at every level. You don’t need to be a superstar — just a programmer who loves to learn and build helpful software.

Follow publication

Member-only story

Featured

Go 1.24: Mutex Spin Optimization Significantly Enhances Performance

An overview of the mutex spin optimization introduced in Go 1.24 and its impact on performance.

huizhou92
The Ordinary Programmer
5 min readFeb 11, 2025

--

Background

In 2024, Rhys Hiltner proposed performance optimizations for mutex locks. This optimization has now been merged into the upcoming Go 1.24 release, potentially enhancing performance by up to 70% in scenarios with high lock contention.

source https://github.com/golang/go/issues/68578

In the benchmark test ChanContended, the author observed a significant decline in mutex performance as GOMAXPROCS increased.
Intel i7-13700H (linux/amd64):

  • With 4 threads allowed, the overall throughput is half that of a single thread.
  • With 8 threads allowed, the throughput is halved again.
  • With 12 threads allowed, the throughput is halved once more.
  • At GOMAXPROCS=20, 200 channel operations took an average of 44 microseconds, with an average of 220 nanoseconds per unlock2 call, each having the opportunity to wake a sleeping thread.

Another perspective is to consider the CPU usage time of the process. The following data shows that within 1.78 seconds of Wall-Clock Time, the process's 20 threads spent 27.74 seconds in CPU (spinning) during lock2 calls.

These lock2-related threads did not sleep but continuously spun, consuming significant CPU resources.

New Proposal: Adding Spinning State

Through the analysis above, the author found that in the current lock2 implementation, although threads theoretically can sleep, they spin, leading to slower lock handoffs and considerable performance loss. Thus, a new design proposal was introduced 《Proposal: Improve scalability of runtime.lock2》.

Core Optimization Points

--

--

Published in The Ordinary Programmer

The Ordinary Programmer is for coders who embrace simplicity and practicality. We share real-world tips, project stories, and tech insights for developers at every level. You don’t need to be a superstar — just a programmer who loves to learn and build helpful software.

Written by huizhou92

Golang backend engineer. Specializes in cloud, data, and security. Lifelong learner. Efficiency enthusiast.

No responses yet

Write a response