Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Go GC Write Barriers Under Concurrent Mutation: Tri-Color Invariants, Barrier Costs, and the Allocation Pressure Trap

The Problem Isn't GC Pauses—It's What Triggers Them Most Go engineers know the GC runs concurrently and that STW pauses are short. Fewer understand why the write barrier exists, what it costs at the instruction level, and how specific allocation patterns force the runtime into assist mode—burning goroutine CPU on GC work during the critical path of a request. This matters most in…

The article discusses the write barrier mechanism in Go's concurrent garbage collection (GC) system and its impact on performance, particularly in latency-sensitive applications. The write barrier is designed to maintain an invariants during concurrent marking, ensuring that an object being pointed to by a black object (already scanned) is not pointed to by a white object (unmarked) to prevent dangling pointers.

In Go 1.17, Go introduced a hybrid write barrier that combines both Dijkstra insertion and Yuasa deletion barriers. The hybrid barrier shades the new pointer target grey (Dijkstra) and the old pointer target grey (Yuasa) on a write operation. This way, the runtime ensures both the objects affected by the write are grey before the write completes, eliminating the need for a stop-the-world (STW) re-scan of stacks at the end of the mark phase, as was required in previous versions without the hybrid barrier.

The write barrier only applies to heap pointer writes and not to stack writes. Stacks are rescanned at mark termination if they were active during marking. While this approach reduces CPU cost for stack writes, it does not eliminate the performance impact of the write barrier when dealing with sustained high mutation rates.

The write barrier's cost at the call site is illustrated by a simple example of linking nodes in a linked list. The assignment operation, which triggers a heap pointer write, results in a call to `runtime.gcWriteBarrier`, incurring a single load and a branch. When the write barrier is enabled, this operation incurs additional costs.

Furthermore, the write barrier buffer and drain cost are significant factors. Each processor (P) has a write barrier buffer of 256 pointer slots. When a goroutine fills its P's buffer, it must flush it, leading to synchronous work on the goroutine. The flush process greys the buffered pointers by marking them in the GC work queue.

Under heavy pointer mutation, this flush cost becomes proportional to the pointer write rate rather than the allocation rate alone. This means that a service with frequent pointer updates, such as linked structures or JSON/BSON document processing, can experience higher costs due to the write barrier buffer and drain mechanism.

As allocation pressure increases, the Go runtime may trigger GC assist, in which goroutines perform proportional GC marking work before fulfilling an allocation. This assist work is calculated as `assist_work = allocation_size * (scan_work_remaining / remaining_heap_budget)`. When allocation rates exceed the concurrent mark phase's capacity to keep up, the runtime puts goroutines into assist mode, leading to additional marking work during the request's critical path.

This latency cliff manifests as increased request handler latency rather than a noticeable GC pause.

To measure the assist latency in production, developers can use Go's runtime metrics, specifically the `/gc/scan/globals:bytes` metric, which represents the total heap size scanned during the concurrent marking phase. By correlating this metric with p99 handler latency spikes, developers can identify if assist is contributing non-trivially to latency issues.

The value of `GCCPUFraction`, which represents the percentage of available CPU being used by the GC, above 0.05 (5%) under sustained load also indicates that assist is impacting performance.

Lastly, the article highlights structural patterns that amplify the write barrier's cost. Pointer-heavy struct trees, especially when created through deserializing JSON or BSON into nested data structures, generate a large number of heap allocations and pointer writes, leading to increased write barrier flushes and allocation pressure.

Mitigation strategies include using flat, index-addressed representations or pre-allocated arenas to minimize internal pointer updates after initialization. Similarly, excessive interface boxing in hot paths, where each middleware wrapper allocates an interface value on the heap, can exacerbate the write barrier cost. Minimizing interface usage in performance-critical paths can help reduce this barrier's impact.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Parsing Character Card V2/V3 PNG Chunks in Pure Browser JS & Building a Zero-XSS Regex Status Bar Sandbox

Interactive fiction and AI roleplay creators pass around thousands of "character cards" every week—portable PNG portraits that secretly carry structured JSON world lore, persona definitions, alternate…

  • Character Card V2/V3 PNG chunks parsed using pure browser JS
  • Zero-dependency, browser-native engine developed by Rubii AI
  • Allowlist-based HTML/CSS sanitizer prevents zero-XSS regex vulnerabilities

Why Processing Large PDFs in the Browser Is Harder Than It Looks

A small PDF is usually easy to handle in a browser. Select a file, load it into a JavaScript library, perform an operation and download the result.

  • Large PDFs consume browser memory, making interfaces unresponsive.
  • PDF file size doesn't fully indicate processing complexity.
  • OCR demands additional image processing steps, making it resource-intensive.

PDF passwords explained: user vs owner passwords and what permission flags really do

PDF "password protection" is two different features that people mix up constantly. Two passwords, two jobs User (open) password : required to open the file.

  • User password unlocks PDF content, encrypts data with generated key
  • Owner password controls permission flags like printing, copying
  • Strong User password (four random words) better than complex combinations

More from Saturday, 28 November 2026 →