mirror of
				https://github.com/gin-gonic/gin.git
				synced 2025-11-04 09:12:12 +08:00 
			
		
		
		
	Performance improvement
- Reduces number of allocations per context - Improves CPU cache usage
This commit is contained in:
		
							parent
							
								
									184a02ee2d
								
							
						
					
					
						commit
						48f4914165
					
				@ -54,6 +54,7 @@ func (a errorMsgs) String() string {
 | 
				
			|||||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
 | 
					// Context is the most important part of gin. It allows us to pass variables between middleware,
 | 
				
			||||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
 | 
					// manage the flow, validate the JSON of a request and render a JSON response for example.
 | 
				
			||||||
type Context struct {
 | 
					type Context struct {
 | 
				
			||||||
 | 
						writermem responseWriter
 | 
				
			||||||
	Request   *http.Request
 | 
						Request   *http.Request
 | 
				
			||||||
	Writer    ResponseWriter
 | 
						Writer    ResponseWriter
 | 
				
			||||||
	Keys      map[string]interface{}
 | 
						Keys      map[string]interface{}
 | 
				
			||||||
@ -70,7 +71,8 @@ type Context struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
 | 
					func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
 | 
				
			||||||
	c := engine.cache.Get().(*Context)
 | 
						c := engine.cache.Get().(*Context)
 | 
				
			||||||
	c.Writer.reset(w)
 | 
						c.writermem.reset(w)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.Request = req
 | 
						c.Request = req
 | 
				
			||||||
	c.Params = params
 | 
						c.Params = params
 | 
				
			||||||
	c.handlers = handlers
 | 
						c.handlers = handlers
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										4
									
								
								gin.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								gin.go
									
									
									
									
									
								
							@ -60,7 +60,9 @@ func New() *Engine {
 | 
				
			|||||||
	engine.router = httprouter.New()
 | 
						engine.router = httprouter.New()
 | 
				
			||||||
	engine.router.NotFound = engine.handle404
 | 
						engine.router.NotFound = engine.handle404
 | 
				
			||||||
	engine.cache.New = func() interface{} {
 | 
						engine.cache.New = func() interface{} {
 | 
				
			||||||
		return &Context{Engine: engine, Writer: &responseWriter{}}
 | 
							c := &Context{Engine: engine}
 | 
				
			||||||
 | 
							c.Writer = &c.writermem
 | 
				
			||||||
 | 
							return c
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return engine
 | 
						return engine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,6 @@ type (
 | 
				
			|||||||
		Written() bool
 | 
							Written() bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// private
 | 
							// private
 | 
				
			||||||
		reset(http.ResponseWriter)
 | 
					 | 
				
			||||||
		setStatus(int)
 | 
							setStatus(int)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user