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

refactor: convert byte to string using unsafe, reduce memory alloc #19844

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
9 changes: 6 additions & 3 deletions pkg/sql/util/csvparser/csv_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"unicode/utf8"

"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/common/util"
"github.com/matrixorigin/matrixone/pkg/config"
"github.com/spkg/bom"
)
Expand Down Expand Up @@ -261,8 +262,7 @@ func (parser *CSVParser) readRow(row []Field) ([]Field, error) {
return nil, err
}

str := string(parser.recordBuffer) // Convert to string once to batch allocations

str := util.UnsafeBytesToString(parser.recordBuffer) // convert byte to string using unsafe, reduce memory allocgit
// remove the last empty value
if parser.cfg.TrimLastSep {
i := len(parser.fieldIndexes) - 1
Expand Down Expand Up @@ -722,7 +722,10 @@ func (parser *CSVParser) readColumns() error {
return nil
}

parser.columns = make([]string, 0, len(parser.fieldIndexes))
parser.columns = parser.columns[:0]
if cap(parser.columns) < len(parser.fieldIndexes) {
parser.columns = make([]string, 0, len(parser.fieldIndexes))
}
str := string(parser.recordBuffer) // Convert to string once to batch allocations
var preIdx int
for i, idx := range parser.fieldIndexes {
Expand Down
Loading