Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add context param query cache #1450

Merged
merged 3 commits into from
May 29, 2019
Merged

Conversation

ijaa
Copy link
Contributor

@ijaa ijaa commented Jul 31, 2018

When we call the context.Query() function multiple times to get the parameter value, adding a cache can greatly reduce the number of calls for url.ParseQuery, which make a map for each call.

@codecov
Copy link

codecov bot commented Jul 31, 2018

Codecov Report

Merging #1450 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1450      +/-   ##
==========================================
+ Coverage   98.75%   98.75%   +<.01%     
==========================================
  Files          38       38              
  Lines        2160     2164       +4     
==========================================
+ Hits         2133     2137       +4     
  Misses         15       15              
  Partials       12       12
Impacted Files Coverage Δ
context.go 98.39% <100%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e320c9...1b36ef1. Read the comment docs.

@dengoswei
Copy link

why hasn't this change been merged?

@appleboy appleboy added this to the 1.4 milestone Aug 17, 2018
@thinkerou thinkerou modified the milestones: 1.4, 1.5 Mar 1, 2019
Copy link
Contributor

@dmarkham dmarkham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
👍

@appleboy
Copy link
Member

Benchmark

func BenchmarkGetQueryArrayOld(b *testing.B) {
	c, _ := CreateTestContext(httptest.NewRecorder())
	c.Request = createMultipartRequest()

	var obj struct {
		Foo          string    `form:"foo"`
		Bar          string    `form:"bar"`
		BarAsInt     int       `form:"bar"`
		Array        []string  `form:"array"`
		ID           string    `form:"id"`
		TimeLocal    time.Time `form:"time_local" time_format:"02/01/2006 15:04"`
		TimeUTC      time.Time `form:"time_utc" time_format:"02/01/2006 15:04" time_utc:"1"`
		TimeLocation time.Time `form:"time_location" time_format:"02/01/2006 15:04" time_location:"Asia/Tokyo"`
		BlankTime    time.Time `form:"blank_time" time_format:"02/01/2006 15:04"`
	}
	c.Bind(&obj)
	for i := 0; i < b.N; i++ {
	c.GetQueryArray("array[]")
	}
}

result:

$ go test -benchmem -run=^$ github.com/gin-gonic/gin -bench ^BenchmarkGetQueryArray
goos: darwin
goarch: amd64
pkg: github.com/gin-gonic/gin
BenchmarkGetQueryArrayOld-4     20000000                88.8 ns/op            48 B/op          1 allocs/op
BenchmarkGetQueryArrayNew-4     100000000               11.0 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gin-gonic/gin        3.028s

@appleboy appleboy merged commit 233a3e4 into gin-gonic:master May 29, 2019
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this pull request Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants