Skip to content

Conversation

@lucix-aws
Copy link
Contributor

@lucix-aws lucix-aws commented Nov 3, 2025

Adjusts the initial size of each middleware step. New base value of 8 was selected based on the downstream aws-sdk-go-v2, where initialize/serialize/build typically have a little under that many middlewares. Finalize and Deserialize are typically double that size.

Before change w/ 1024*1024 mock sqs ListQueues requests:

  • ~40GiB allocated
  • ~415M objects allocated

After:

  • ~37.5GiB allocated
  • ~400M objects allocated

If you'd like to run this yourself

Run the test code (make sure you checkout this patch and modreplace in your test project):

package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	_ "net/http/pprof"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/sqs"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

func main() {
	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil {
		panic(err)
	}

	svc := sqs.NewFromConfig(cfg, func(o *sqs.Options) {
		o.HTTPClient = smithyhttp.ClientDoFunc(func(r *http.Request) (*http.Response, error) {
			return &http.Response{
				StatusCode: 200,
				Body:       io.NopCloser(strings.NewReader("{}")),
			}, nil
		})
	})

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()

	fmt.Println("wait")
	time.Sleep(5 * time.Second)
	fmt.Println("run")

	for i := 0; i < 1024*1024; i++ {
		fmt.Println(i)
		out, err := svc.ListQueues(context.Background(), nil)
		if err != nil {
			panic(err)
		}
		_ = out
	}

	fmt.Println("ready")
	for {
	}
}

Once the "ready" is printed, run the pprof tool to get the visual:

go install github.com/google/pprof@latest # if you don't have it
pprof -http=localhost:9090 localhost:6060/debug/pprof/allocs

See aws/aws-sdk-go-v2#3197

@lucix-aws lucix-aws requested review from a team as code owners November 3, 2025 19:33
@Madrigal
Copy link
Contributor

Madrigal commented Nov 3, 2025

Thanks for including the repro steps! I'm sure they'll also come handy later on

@lucix-aws lucix-aws merged commit 8d4c6fb into main Nov 3, 2025
15 checks passed
@lucix-aws lucix-aws deleted the fix-middlewarealloc branch November 3, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants