*Optimization in Go: Reducing Allocations and GC Pressure

Let’s take an application - a high-throughput log parser — write the obvious version first, measure it, and then slowly remove the garbage until there is almost nothing left for the GC to care about. (Similar question asked at google interview)

image.png

Every change should answer:


1. The problem

Imagine we have a service that receives logs like this:

1727537425123 INFO  auth     200  12  req-7f3a  GET   /v1/users/42  user loaded successfully
1727537425127 WARN  payments 429  3   req-91ac  POST  /v1/checkout  rate limit exceeded for customer
1727537425130 ERROR users    500  87  req-a213  GET   /v1/profile   failed to load user profile from database

The extra spaces above are only for visual alignment. In this article, we assume the actual log fields are separated by exactly one space.

The format is:

timestamp | level | service | status | latency_ms | request_id | method | path | message...

The first 8 fields are simple: they end at the next space. But message is different: failed to load user profile from database

It contains spaces. So logically our record is:

1727537425130 | ERROR | users | 500 | 87 | req-a213 | GET | /v1/profile | failed to load user profile from database

Naturally, we create a struct in go that is the same as input log:

type LogEntry struct {
	Timestamp int64
	Level     string
	Service   string
	Status    int
	LatencyMS int
	RequestID string
	Method    string
	Path      string
	Message   string
}