Skip to content

Poor performance when decoding data nested inside two collection layers #71

Open
@Jecoms

Description

@Jecoms

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 ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions