Skip to content

Commit

Permalink
Optimize arguments handling
Browse files Browse the repository at this point in the history
Remove memory/cpu overhead of handling arguments when they are empty.
This is important in case of user loading a dump of their database.
In such a case, there are no arguments at all and the number of
statements is usually big.

Signed-off-by: Piotr Jastrzebski <piotr@chiselstrike.com>
  • Loading branch information
haaawk committed Jul 18, 2024
1 parent 2cfb92c commit 6878364
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
15 changes: 10 additions & 5 deletions libsql/internal/hrana/stream_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ func CloseStream() StreamRequest {
return StreamRequest{Type: "close"}
}

func ExecuteStream(sql string, params shared.Params, wantRows bool) (*StreamRequest, error) {
func ExecuteStream(sql string, params *shared.Params, wantRows bool) (*StreamRequest, error) {
stmt := &Stmt{
Sql: &sql,
WantRows: wantRows,
}
if err := stmt.AddArgs(params); err != nil {
return nil, err
if params != nil {
if err := stmt.AddArgs(*params); err != nil {
return nil, err
}
}
return &StreamRequest{Type: "execute", Stmt: stmt}, nil
}
Expand All @@ -40,14 +42,17 @@ func ExecuteStoredStream(sqlId int32, params shared.Params, wantRows bool) (*Str

func BatchStream(sqls []string, params []shared.Params, wantRows bool, transactional bool) (*StreamRequest, error) {
batch := &Batch{}
addArgs := len(params) > 0
for idx, sql := range sqls {
s := sql
stmt := &Stmt{
Sql: &s,
WantRows: wantRows,
}
if err := stmt.AddArgs(params[idx]); err != nil {
return nil, err
if addArgs {
if err := stmt.AddArgs(params[idx]); err != nil {
return nil, err
}
}
var condition *BatchCondition
if transactional {
Expand Down
6 changes: 5 additions & 1 deletion libsql/internal/http/hranaV2/hranaV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ func (h *hranaV2Conn) executeStmt(ctx context.Context, query string, args []driv
}
msg := &hrana.PipelineRequest{}
if len(stmts) == 1 {
executeStream, err := hrana.ExecuteStream(stmts[0], params[0], wantRows)
var p *shared.Params
if len(params) > 0 {
p = &params[0]
}
executeStream, err := hrana.ExecuteStream(stmts[0], p, wantRows)
if err != nil {
return nil, fmt.Errorf("failed to execute SQL: %s\n%w", query, err)
}
Expand Down
7 changes: 5 additions & 2 deletions libsql/internal/http/shared/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func ParseStatement(sql string) ([]string, []ParamsInfo, error) {
}

func ParseStatementAndArgs(sql string, args []driver.NamedValue) ([]string, []Params, error) {
stmts, _ := sqliteparserutils.SplitStatement(sql)

if len(args) == 0 {
return stmts, nil, nil
}
parameters, err := ConvertArgs(args)
if err != nil {
return nil, nil, err
}

stmts, _ := sqliteparserutils.SplitStatement(sql)

stmtsParams := make([]Params, len(stmts))
totalParametersAlreadyUsed := 0
for idx, stmt := range stmts {
Expand Down

0 comments on commit 6878364

Please sign in to comment.