perf(context): optimize getMapFromFormData performance (#4339)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Name · committed by GitHub · parent 28172fa · Browse files · 2 files·+99 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: 1911860538 <alxps1911@gmail.com>
Diff stats
2 files+99 added−5 removed
- other:+99 added−5 removed
Provenance
- Commit
f9bd00a- Tree
a7bb9fc- Author
Name <1911860538@qq.com>- Committer
GitHub <noreply@github.com>- Parent
28172fa
2 files changed+99 added−5 removed
modified:context_test.go+85 added
3552 unmodified lines3553 assert.Equal(t, tc.found, found, "found mismatch")3554 })3555 }3556 }3557+3558+func BenchmarkGetMapFromFormData(b *testing.B) {3559+ // Test case 1: Small dataset with bracket notation3560+ smallData := map[string][]string{3561+ "ids[a]": {"hi"},3562+ "ids[b]": {"3.14"},3563+ "names[a]": {"mike"},3564+ "names[b]": {"maria"},3565+ }3566+3567+ // Test case 2: Medium dataset with mixed data3568+ mediumData := map[string][]string{3569+ "ids[a]": {"hi"},3570+ "ids[b]": {"3.14"},3571+ "ids[c]": {"test"},3572+ "ids[d]": {"value"},3573+ "names[a]": {"mike"},3574+ "names[b]": {"maria"},3575+ "names[c]": {"john"},3576+ "names[d]": {"jane"},3577+ "other[key1]": {"value1"},3578+ "other[key2]": {"value2"},3579+ "simple": {"data"},3580+ "another": {"info"},3581+ }3582+3583+ // Test case 3: Large dataset with many bracket keys3584+ largeData := make(map[string][]string)3585+ for i := 0; i < 100; i++ {3586+ key := fmt.Sprintf("ids[%d]", i)3587+ largeData[key] = []string{fmt.Sprintf("value%d", i)}3588+ }3589+ for i := 0; i < 50; i++ {3590+ key := fmt.Sprintf("names[%d]", i)3591+ largeData[key] = []string{fmt.Sprintf("name%d", i)}3592+ }3593+ for i := 0; i < 25; i++ {3594+ key := fmt.Sprintf("other[key%d]", i)3595+ largeData[key] = []string{fmt.Sprintf("other%d", i)}3596+ }3597+3598+ // Test case 4: Dataset with many non-matching keys (worst case)3599+ worstCaseData := make(map[string][]string)3600+ for i := 0; i < 100; i++ {3601+ key := fmt.Sprintf("nonmatching%d", i)3602+ worstCaseData[key] = []string{fmt.Sprintf("value%d", i)}3603+ }3604+ worstCaseData["ids[a]"] = []string{"hi"}3605+ worstCaseData["ids[b]"] = []string{"3.14"}3606+3607+ // Test case 5: Dataset with short keys (best case for early exit)3608+ shortKeysData := map[string][]string{3609+ "a": {"value1"},3610+ "b": {"value2"},3611+ "ids[a]": {"hi"},3612+ "ids[b]": {"3.14"},3613+ }3614+3615+ benchmarks := []struct {3616+ name string3617+ data map[string][]string3618+ key string3619+ }{3620+ {"Small_Bracket", smallData, "ids"},3621+ {"Small_Names", smallData, "names"},3622+ {"Medium_Bracket", mediumData, "ids"},3623+ {"Medium_Names", mediumData, "names"},3624+ {"Medium_Other", mediumData, "other"},3625+ {"Large_Bracket", largeData, "ids"},3626+ {"Large_Names", largeData, "names"},3627+ {"Large_Other", largeData, "other"},3628+ {"WorstCase_Bracket", worstCaseData, "ids"},3629+ {"ShortKeys_Bracket", shortKeysData, "ids"},3630+ {"Empty_Key", smallData, "notfound"},3631+ }3632+3633+ for _, bm := range benchmarks {3634+ b.Run(bm.name, func(b *testing.B) {3635+ b.ReportAllocs()3636+ for i := 0; i < b.N; i++ {3637+ _, _ = getMapFromFormData(bm.data, bm.key)3638+ }3639+ })3640+ }3641+}modified:context.go+14 added-5 removed
652 unmodified lines653 // It parses from data with bracket notation like "key[subkey]=value" into a map.654 func getMapFromFormData(m map[string][]string, key string) (map[string]string, bool) {655 d := make(map[string]string)656 found := false657+ keyLen := len(key)658+659 for k, v := range m {658- if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {659- if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {660- found = true661- d[k[i+1:][:j]] = v[0]662- }660+ if len(k) < keyLen+3 { // key + "[" + at least one char + "]"661+ continue662 }663+664+ if k[:keyLen] != key || k[keyLen] != '[' {665+ continue666+ }667+668+ if j := strings.IndexByte(k[keyLen+1:], ']'); j > 0 {669+ found = true670+ d[k[keyLen+1:keyLen+1+j]] = v[0]671+ }672 }673+674 return d, found675 }676 677 // FormFile returns the first file for the provided form key.726 unmodified lines