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

Improve query pool #1104

Merged
merged 6 commits into from
May 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 24 additions & 36 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,21 +311,11 @@ func (s *Session) SetTrace(trace Tracer) {
// value before the query is executed. Query is automatically prepared
// if it has not previously been executed.
func (s *Session) Query(stmt string, values ...interface{}) *Query {
s.mu.RLock()
qry := queryPool.Get().(*Query)
qry.session = s
qry.stmt = stmt
qry.values = values
qry.cons = s.cons
qry.session = s
qry.pageSize = s.pageSize
qry.trace = s.trace
qry.observer = s.queryObserver
qry.prefetch = s.prefetch
qry.rt = s.cfg.RetryPolicy
qry.serialCons = s.cfg.SerialConsistency
qry.defaultTimestamp = s.cfg.DefaultTimestamp
qry.idempotent = s.cfg.DefaultIdempotence
s.mu.RUnlock()
qry.defaultsFromSession()
return qry
}

Expand All @@ -343,11 +333,11 @@ type QueryInfo struct {
// During execution, the meta data of the prepared query will be routed to the
// binding callback, which is responsible for producing the query argument values.
func (s *Session) Bind(stmt string, b func(q *QueryInfo) ([]interface{}, error)) *Query {
s.mu.RLock()
qry := &Query{stmt: stmt, binding: b, cons: s.cons,
session: s, pageSize: s.pageSize, trace: s.trace, observer: s.queryObserver,
prefetch: s.prefetch, rt: s.cfg.RetryPolicy}
s.mu.RUnlock()
qry := queryPool.Get().(*Query)
qry.session = s
qry.stmt = stmt
qry.binding = b
qry.defaultsFromSession()
return qry
}

Expand Down Expand Up @@ -679,6 +669,22 @@ type Query struct {
disableAutoPage bool
}

func (q *Query) defaultsFromSession() {
s := q.session

s.mu.RLock()
q.cons = s.cons
q.pageSize = s.pageSize
q.trace = s.trace
q.observer = s.queryObserver
q.prefetch = s.prefetch
q.rt = s.cfg.RetryPolicy
q.serialCons = s.cfg.SerialConsistency
q.defaultTimestamp = s.cfg.DefaultTimestamp
q.idempotent = s.cfg.DefaultIdempotence
s.mu.RUnlock()
}

// String implements the stringer interface.
func (q Query) String() string {
return fmt.Sprintf("[query statement=%q values=%+v consistency=%s]", q.stmt, q.values, q.cons)
Expand Down Expand Up @@ -1064,25 +1070,7 @@ func (q *Query) Release() {

// reset zeroes out all fields of a query so that it can be safely pooled.
func (q *Query) reset() {
q.stmt = ""
q.values = nil
q.cons = 0
q.pageSize = 0
q.routingKey = nil
q.routingKeyBuffer = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

looking at this, we could probably keep these buffers between pooling but would need to verify that they are not kept around anywhere after being used.

q.pageState = nil
q.prefetch = 0
q.trace = nil
q.session = nil
q.rt = nil
q.binding = nil
q.attempts = 0
q.totalLatency = 0
q.serialCons = 0
q.defaultTimestamp = false
q.disableSkipMetadata = false
q.disableAutoPage = false
q.context = nil
*q = Query{}
}

// Iter represents an iterator that can be used to iterate over all rows that
Expand Down