Skip to content

Commit

Permalink
sessionctx/stmtctx: do not use copy-on-read for GetWarnings function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Dec 9, 2022
1 parent f612724 commit 96eb4ba
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ type StatementContext struct {

message string
warnings []SQLWarn
errorCount uint16
execDetails execdetails.ExecDetails
allExecDetails []*execdetails.DetailsNeedP90
}
Expand Down Expand Up @@ -730,9 +729,7 @@ func (sc *StatementContext) SetMessage(msg string) {
func (sc *StatementContext) GetWarnings() []SQLWarn {
sc.mu.Lock()
defer sc.mu.Unlock()
warns := make([]SQLWarn, len(sc.mu.warnings))
copy(warns, sc.mu.warnings)
return warns
return sc.mu.warnings
}

// TruncateWarnings truncates warnings begin from start and returns the truncated warnings.
Expand Down Expand Up @@ -763,7 +760,11 @@ func (sc *StatementContext) WarningCount() uint16 {
func (sc *StatementContext) NumErrorWarnings() (ec uint16, wc int) {
sc.mu.Lock()
defer sc.mu.Unlock()
ec = sc.mu.errorCount
for _, w := range sc.mu.warnings {
if w.Level == WarnLevelError {
ec++
}
}
wc = len(sc.mu.warnings)
return
}
Expand All @@ -773,12 +774,6 @@ func (sc *StatementContext) SetWarnings(warns []SQLWarn) {
sc.mu.Lock()
defer sc.mu.Unlock()
sc.mu.warnings = warns
sc.mu.errorCount = 0
for _, w := range warns {
if w.Level == WarnLevelError {
sc.mu.errorCount++
}
}
}

// AppendWarning appends a warning with level 'Warning'.
Expand Down Expand Up @@ -814,7 +809,6 @@ func (sc *StatementContext) AppendError(warn error) {
defer sc.mu.Unlock()
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelError, warn})
sc.mu.errorCount++
}
}

Expand Down Expand Up @@ -860,7 +854,6 @@ func (sc *StatementContext) resetMuForRetry() {
sc.mu.copied = 0
sc.mu.touched = 0
sc.mu.message = ""
sc.mu.errorCount = 0
sc.mu.warnings = nil
sc.mu.execDetails = execdetails.ExecDetails{}
sc.mu.allExecDetails = make([]*execdetails.DetailsNeedP90, 0, 4)
Expand Down

0 comments on commit 96eb4ba

Please sign in to comment.