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

use unsafeBytesToString to reuse memory #1434

Open
wants to merge 1 commit into
base: release-branch.v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bulk_delete_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (r *BulkDeleteRequest) Source() ([]string, error) {
return nil, err
}

lines := []string{string(body)}
lines := []string{unsafeBytesToString(body)}
r.source = lines

return lines, nil
Expand Down
4 changes: 2 additions & 2 deletions bulk_index_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (r *BulkIndexRequest) Source() ([]string, error) {
return nil, err
}

lines[0] = string(body)
lines[0] = unsafeBytesToString(body)

// "field1" ...
if r.doc != nil {
Expand All @@ -241,7 +241,7 @@ func (r *BulkIndexRequest) Source() ([]string, error) {
if err != nil {
return nil, err
}
lines[1] = string(body)
lines[1] = unsafeBytesToString(body)
case json.RawMessage:
lines[1] = string(t)
case *json.RawMessage:
Expand Down
4 changes: 2 additions & 2 deletions bulk_update_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (r *BulkUpdateRequest) Source() ([]string, error) {
return nil, err
}

lines[0] = string(body)
lines[0] = unsafeBytesToString(body)

// 2nd line: {"doc" : { ... }} or {"script": {...}}
var doc interface{}
Expand Down Expand Up @@ -327,7 +327,7 @@ func (r *BulkUpdateRequest) Source() ([]string, error) {
return nil, err
}

lines[1] = string(body)
lines[1] = unsafeBytesToString(body)

r.source = lines
return lines, nil
Expand Down
2 changes: 1 addition & 1 deletion msearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *MultiSearchService) Do(ctx context.Context) (*MultiSearchResult, error)
if err != nil {
return nil, err
}
lines = append(lines, string(header))
lines = append(lines, unsafeBytesToString(header))
lines = append(lines, body)
}
body := strings.Join(lines, "\n") + "\n" // add trailing \n
Expand Down
6 changes: 3 additions & 3 deletions search_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,15 @@ func (r *SearchRequest) Body() (string, error) {
if err != nil {
return "", err
}
return string(body), nil
return unsafeBytesToString(body), nil
}
switch t := r.source.(type) {
default:
body, err := json.Marshal(r.source)
if err != nil {
return "", err
}
return string(body), nil
return unsafeBytesToString(body), nil
case *SearchSource:
src, err := t.Source()
if err != nil {
Expand All @@ -541,7 +541,7 @@ func (r *SearchRequest) Body() (string, error) {
if err != nil {
return "", err
}
return string(body), nil
return unsafeBytesToString(body), nil
case json.RawMessage:
return string(t), nil
case *json.RawMessage:
Expand Down
11 changes: 11 additions & 0 deletions unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import "unsafe"

func unsafeBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}