form icon indicating copy to clipboard operation
form copied to clipboard

Poor performance when decoding data nested inside two collection layers

Open Jecoms opened this issue 1 year ago • 0 comments

When decoding a struct that has data nested inside two layers of slices or maps, the performance is impacted exponentially based on the number of values.

type FormRequest struct {
	Foos []*NestedFoo `form:"foos"`
}

type NestedFoo struct {
	Bars []*NestedBar `form:"bars"`
}

type NestedBar struct {
	Bazs   []string          `form:"bazs"`
	Lookup map[string]string `form:"lookup"`
}

// example payload to decode into FormRequest
var urlValues = url.Values{
  "foos[0].bars[0].bazs": []string{"..."},
  "foos[0].bars[0].lookup[A]": []string{"..."},
  ...
}

Go Playground

# ran on M3 MBP 18GB RAM
Slow first run
         1 decoded values took: 230.667µs

Decoder test without nesting
        10 decoded values took: 15.875µs
       100 decoded values took: 62.125µs
      1000 decoded values took: 621.334µs
     10000 decoded values took: 5.811209ms

Decoder test with layers of nesting
        10 decoded values took: 500.292µs
       100 decoded values took: 162.194542ms
      1000 decoded values took: 2m42.298277458s      
      # ... interrupted before 10000 completed ...

Jecoms avatar Apr 25 '25 15:04 Jecoms