I have been reducing the work Trest does around each handler call. In the latest benchmarks, a route returning a small JSON object went from 3.568 to 2.544 µs per request. An invoice route went from 6.600 to 4.798 µs. Both also used 14 fewer allocations.
Those results came from two local httptest fixtures. Their request contexts had no cancellation; a separate run with cancelable contexts was slower.
Combining validation and decoding
The request binder previously validated a JSON body, then decoded it into a Go value. For a flat object containing strings, integers, and booleans, both passes needed to read the same fields.
The new decoder combines those steps for supported types. At route construction, it records the JSON field names, Go field indexes, and validation constraints. When a request arrives, it parses each value, checks it, and assigns it to the target field.
Here is part of the object loop in internal/schema/decode.go:
field, ok := d.wire.field(&cursor)
if !ok || seen&field.bit != 0 {
return false
}
seen |= field.bit
cursor.skipSpace()
if !cursor.take(':') {
return false
}
cursor.skipSpace()
value, ok := cursor.scalar()
if !ok || !decodeScalar(
value,
field.node,
target.Field(d.fields[bits.TrailingZeros64(field.bit)]),
!d.alwaysValid,
) {
return false
}Each field has a bit in a 64-bit mask. seen detects duplicate fields; the same mask is checked against the required fields after the loop. The field's bit also indexes its Go struct position. decodeScalar checks the value and assigns it, including integer range checks.
This path deliberately supports a limited set of types. Pointers, collections, floats, embedded fields, and custom codecs use the existing validator and JSON decoder. Invalid input also falls back so the existing code produces the error response.
That fallback needs a zeroed target. The combined decoder can assign the first field and fail on the second, so the binder calls target.SetZero() before trying the general path. Decoded strings also get their own storage because the request buffer can be reused after binding.
Preparing the response encoder
The response type is known when a route is constructed. For supported types, Trest now builds an encoding plan containing field indexes, encoded JSON field names, and the encoder for each field.
For example, the bytes for "message": can be prepared once. The string value still has to be read and escaped for each response. The struct loop in internal/jsonoutput/encode.go looks like this:
dst = append(dst, '{')
written := false
for _, member := range p.fields {
child := value.Field(member.index)
if member.omitEmpty && child.IsZero() {
continue
}
if written {
dst = append(dst, ',')
}
written = true
dst = append(dst, member.name...)
dst = member.plan.Append(dst, child)
}
return append(dst, '}')The scalar encoders append directly to the byte slice. Integer fields use strconv.AppendInt or strconv.AppendUint; string fields use an escaping routine matching the standard encoder's behavior. Unsupported types retain the standard encoder.
Routes using the compiled encoder also have a pool for response buffers. A buffer goes back after Write returns, and backing storage larger than 32 KiB is discarded. Requests and contexts remain unpooled because middleware can retain references to them.
These are two changes from a larger batch that also changed request metadata allocation and invocation setup. I measured the batch together, so the results don't tell me how much time either JSON change saved on its own.
The before-and-after results
The hello fixture returns a freshly serialized {"message":"hello"} response. The invoice fixture sends a request that has already succeeded once, exercising validation, authorization, and duplicate lookup in an in-memory store. It measures the duplicate case, not insertion.
The time reductions were 28.70% and 27.31%. Both had p < 0.001. The baseline already contained the earlier 11 November optimizations; these numbers cover the additional changes measured on 13 November.
The requests in those fixtures had parents with neither cancellation nor a deadline. That matters because Trest can defer allocating its timeout context for unobserved invocations with those parents. Calling Done, or looking up an unknown context value, creates the standard-library context. A cancelable parent takes the eager timeout path.
Incoming HTTP requests have cancelable contexts, so I also ran a separate comparison that created and canceled a fresh parent for each request in every framework. Trest's hello median was 3.483 µs and its invoice median was 5.802 µs. Gin measured 2.358 and 4.942 µs. Trest was 47.72% and 17.40% slower in those cases. These were current-version comparisons; I didn't measure a before-and-after improvement for them.
Compared with Gin and Echo
In the original fixtures, Trest's hello route still took 17.64% more time than Gin's. The invoice medians were close, and the difference from Gin was statistically inconclusive at p = 0.701.
Trest used fewer allocations in those fixtures: 18 for hello and 34 for invoices, against Gin's 20 and 40 and Echo's 19 and 39. It did not beat both frameworks on request time.
The comparison used Gin v1.12.0 and Echo v5.4.4 on an Apple M2 Pro, macOS arm64, Go 1.27.3, and GOMAXPROCS=1. I used fourteen paired samples per case, each timed for at least 500 ms, with frozen before-and-after binaries and rotated run order. The unchanged Gin and Echo controls showed no significant timing shift between runs.
Both fixtures include fresh requests and response recorders. They exclude networking, TLS, and databases. The frameworks enforce the same fixture business rules, but differ in parser behavior, headers, and features. These are serial development benchmarks on a shared machine, not production latency or concurrent throughput measurements. Trest's IDs, deadlines, validation, and authorization stayed enabled where applicable.
The optimization pass passed race tests, vet, and builds across all seven modules. Differential fuzzing compared parsing and encoding behavior with the existing paths. The benchmark report has a verification summary and the measurement method. The measurement bundle includes the raw samples, exact medians, and benchstat output.