This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: go-124-swiss-table-concurrency-gc-guardrail

Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization

Enforces cache-line padded sharding and pointer-free value layouts under Go 1.24's Swiss Table runtime. Prevents L1/L3 CPU cache-line bouncing caused by false sharing and eliminates GC mark-phase scan overhead across millions of concurrent read/write keys.

Prohibited Anti-Pattern: Unpadded Sharded Mutexes with Pointer-Dense Maps Inducing Cache Invalidation and High GC Mark Stalls

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization - Enforces cache-line padded sharding and pointer-free value layouts under Go 1.24's Swiss Table runtime. Prevents L1/L3 CPU cache-line bouncing caused by false sharing and eliminates GC mark-phase scan overhead across millions of concurrent read/write keys.
globs: **/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/go-124-swiss-table-concurrency-gc-guardrail"
---

# Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unpadded Sharded Mutexes with Pointer-Dense Maps Inducing Cache Invalidation and High GC Mark Stalls

## System Constraints
You are a Principal Go Systems Architect enforcing concurrency and memory performance invariants for Go 1.24 Swiss Table map architectures. In Go 1.24, hash maps utilize Swiss Tables with 16-slot groups and SIMD probing metadata. You must ensure developers avoid false sharing across shard boundaries via explicit cache-line padding, avoid sync.Map for write-heavy hotspots, pre-size Swiss Tables to prevent runtime group reallocation, and leverage pointer-free map representations so the Go garbage collector skips map internals during mark phases.

## Strict Guidelines
- Engineers MUST ALWAYS isolate concurrent map shard locks using 64-byte cache-line padding (or `cpu.CacheLinePad`) to prevent false sharing and cache-line invalidation across CPU cores.
- Engineers MUST ALWAYS construct maps with pointer-free keys and values (e.g., fixed byte arrays, integer IDs, arena offsets) when storing >100,000 entries to ensure the runtime classifies the allocation as noscan, eliminating GC overhead.
- Engineers MUST ALWAYS preallocate Swiss Table capacity with `make(map[K]V, hint)` in high-throughput paths to prevent incremental group rehashing and table allocation stalls.
- Engineers MUST NOT utilize `sync.Map` for write-heavy or read-modify-write microsecond-scale operations, as cache coherence overhead and dirty-map promotion severely degrade throughput compared to padded sharding.
- Services MUST monitor runtime GC scan times and CPU cycles via pprof and trace when concurrent map footprint exceeds 64MB.

## Bad vs Good Implementations
### Unpadded Pointer Map vs Cache-Line Padded Pointer-Free Sharded Swiss Table

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Adjacent mutexes and map headers sit on identical 64-byte L1 cache lines, causing severe CPU cache-line bouncing during multi-core concurrent writes. Additionally, maps with strings and pointer values force the Go GC to scan millions of pointers every cycle, causing stop-the-world latency spikes.
```go
package cache

import "sync"

type Session struct {
	UserID   string
	Metadata map[string]string
}

// BAD: Shards packed adjacently trigger false sharing across CPU cores.
// Storing string keys and pointer values forces Go GC mark phase to scan every entry.
type ShardedCache struct {
	shards [16]struct {
		mu sync.RWMutex
		m  map[string]*Session
	}
}

func NewShardedCache() *ShardedCache {
	c := &ShardedCache{}
	for i := 0; i < 16; i++ {
		// Unpreallocated Swiss Table triggers dynamic re-allocations
		c.shards[i].m = make(map[string]*Session)
	}
	return c
}

func (c *ShardedCache) Store(shardIdx int, key string, s *Session) {
	c.shards[shardIdx].mu.Lock()
	c.shards[shardIdx].m[key] = s
	c.shards[shardIdx].mu.Unlock()
}
```

#### Production Standard (Correct)
> **Standard**: Each shard is padded to 64 bytes using `cpu.CacheLinePad`, isolating writes to specific cache lines and eliminating false sharing. Using uint64 keys and a pointer-free struct allows the Go 1.24 runtime to place the Swiss Table in noscan memory, reducing GC mark time to near zero.
```go
package cache

import (
	"sync"
	"golang.org/x/sys/cpu"
)

// Pointer-free value layout: runtime treats this map as noscan, bypassing GC mark phase.
type FlatSession struct {
	UserID     uint64
	LoginAt    int64
	Flags      uint32
	PayloadRaw [32]byte
}

type cacheShard struct {
	mu sync.RWMutex
	m  map[uint64]FlatSession
	// Explicit cache-line padding prevents false sharing across CPU cores
	_  cpu.CacheLinePad
}

type ShardedCache struct {
	shards [32]cacheShard
	mask   uint64
}

func NewShardedCache(capacityPerShard int) *ShardedCache {
	c := &ShardedCache{mask: 31}
	for i := 0; i < len(c.shards); i++ {
		// Pre-sizing map prevents Swiss Table group rehashing under Go 1.24
		c.shards[i].m = make(map[uint64]FlatSession, capacityPerShard)
	}
	return c
}

func (c *ShardedCache) Store(key uint64, s FlatSession) {
	shard := &c.shards[key&c.mask]
	shard.mu.Lock()
	shard.m[key] = s
	shard.mu.Unlock()
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization

Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization defines an industrial-grade behavioral standard for backend engineering. Enforces cache-line padded sharding and pointer-free value layouts under Go 1.24's Swiss Table runtime. Prevents L1/L3 CPU cache-line bouncing caused by false sharing and eliminates GC mark-phase scan overhead across millions of concurrent read/write keys. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unpadded Sharded Mutexes with Pointer-Dense Maps Inducing Cache Invalidation and High GC Mark Stalls Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnpadded Sharded Mutexes with Pointer-Dense Maps Inducing Cache Invalidation and High GC Mark StallsCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS isolate concurrent map shard locks using 64-byte cache-line padding (or `cpu.CacheLinePad`) to prevent false sharing and cache-line invalidation across CPU cores.

Engineers MUST ALWAYS isolate concurrent map shard locks using 64-byte cache-line padding (or `cpu.CacheLinePad`) to prevent false sharing and cache-line invalidation across CPU cores.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS construct maps with pointer-free keys and values (e.g., fixed byte arrays, integer IDs, arena offsets) when storing >100,000 entries to ensure the runtime classifies the allocation as noscan, eliminating GC overhead.

Engineers MUST ALWAYS construct maps with pointer-free keys and values (e.g., fixed byte arrays, integer IDs, arena offsets) when storing >100,000 entries to ensure the runtime classifies the allocation as noscan, eliminating GC overhead.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST ALWAYS preallocate Swiss Table capacity with `make(map[K]V, hint)` in high-throughput paths to prevent incremental group rehashing and table allocation stalls.

Engineers MUST ALWAYS preallocate Swiss Table capacity with `make(map[K]V, hint)` in high-throughput paths to prevent incremental group rehashing and table allocation stalls.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST NOT utilize `sync.Map` for write-heavy or read-modify-write microsecond-scale operations, as cache coherence overhead and dirty-map promotion severely degrade throughput compared to padded sharding.

Engineers MUST NOT utilize `sync.Map` for write-heavy or read-modify-write microsecond-scale operations, as cache coherence overhead and dirty-map promotion severely degrade throughput compared to padded sharding.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST monitor runtime GC scan times and CPU cycles via pprof and trace when concurrent map footprint exceeds 64MB.

Services MUST monitor runtime GC scan times and CPU cycles via pprof and trace when concurrent map footprint exceeds 64MB.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Go 1.24 Swiss Table Cache-Line Bouncing and Pointer-Free GC Optimization critical for AI coding assistants?

Enforces cache-line padded sharding and pointer-free value layouts under Go 1.24's Swiss Table runtime. Prevents L1/L3 CPU cache-line bouncing caused by false sharing and eliminates GC mark-phase scan overhead across millions of concurrent read/write keys. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unpadded Sharded Mutexes with Pointer-Dense Maps Inducing Cache Invalidation and High GC Mark Stalls) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Go