Skip to content

Commit adea3bd

Browse files
Converting byte slice and string without memory allocation
1 parent 1dd6d0d commit adea3bd

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

reduxer/reduxer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func Do(ctx context.Context, urls ...string) (bundles Bundles, err error) {
156156
}
157157
fn := strings.TrimRight(helper.FileName(shot.URL, ""), "html") + "txt"
158158
fp := filepath.Join(dir, fn)
159-
if err := os.WriteFile(fp, []byte(article.TextContent), 0o600); err == nil && article.TextContent != "" {
159+
if err := os.WriteFile(fp, helper.String2Byte(article.TextContent), 0o600); err == nil && article.TextContent != "" {
160160
if err := helper.SetField(&assets.Txt, "Local", fp); err != nil {
161161
logger.Error("assign field Txt to assets struct failed: %v", err)
162162
}

service/discord/discord.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/fatih/color"
14+
"github.com/wabarc/helper"
1415
"github.com/wabarc/logger"
1516
"github.com/wabarc/wayback"
1617
"github.com/wabarc/wayback/config"
@@ -222,7 +223,7 @@ func (d *Discord) buttonHandlers() map[string]func(*discord.Session, *discord.In
222223

223224
s.ChannelTyping(i.Message.ChannelID)
224225

225-
i.Message.Content = string(data)
226+
i.Message.Content = helper.Byte2String(data)
226227
d.process(&discord.MessageCreate{Message: i.Message})
227228
s.InteractionResponseDelete(s.State.User.ID, i.Interaction)
228229
return
@@ -340,7 +341,7 @@ func (d *Discord) playback(s *discord.Session, i *discord.InteractionCreate) err
340341

341342
// Due to Discord restricted custom_id up to 100 characters, it requires to store
342343
// playback URLs to database.
343-
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString([]byte(text))}
344+
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString(helper.String2Byte(text))}
344345
if err := d.store.CreatePlayback(pb); err != nil {
345346
logger.Error("store collections failed: %v", err)
346347
return err

service/httpd/httpd.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/gorilla/mux"
1616
"github.com/prometheus/client_golang/prometheus/promhttp"
17+
"github.com/wabarc/helper"
1718
"github.com/wabarc/logger"
1819
"github.com/wabarc/wayback"
1920
"github.com/wabarc/wayback/config"
@@ -63,11 +64,11 @@ func (web *web) handle(pool pooling.Pool) http.Handler {
6364
web.router.HandleFunc("/playback", web.playback).Methods(http.MethodPost)
6465

6566
web.router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
66-
w.Write([]byte("ok"))
67+
w.Write(helper.String2Byte("ok"))
6768
}).Name("healthcheck")
6869

6970
web.router.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
70-
w.Write([]byte(version.Version))
71+
w.Write(helper.String2Byte(version.Version))
7172
}).Name("version")
7273

7374
if config.Opts.EnabledMetrics() {
@@ -80,7 +81,7 @@ func (web *web) handle(pool pooling.Pool) http.Handler {
8081

8182
web.router.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
8283
w.Header().Set("Content-Type", "text/plain")
83-
w.Write([]byte("User-agent: *\nDisallow: /"))
84+
w.Write(helper.String2Byte("User-agent: *\nDisallow: /"))
8485
})
8586

8687
return web.router

service/telegram/telegram.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/fatih/color"
17+
"github.com/wabarc/helper"
1718
"github.com/wabarc/logger"
1819
"github.com/wabarc/wayback"
1920
"github.com/wabarc/wayback/config"
@@ -126,7 +127,7 @@ func (t *Telegram) Serve() (err error) {
126127
return false
127128
}
128129

129-
callback.Message.Text = string(data)
130+
callback.Message.Text = helper.Byte2String(data)
130131
go t.process(callback.Message)
131132
case update.Message != nil && update.Message.FromGroup():
132133
logger.Debug("message: %#v", update.Message)
@@ -311,7 +312,7 @@ func (t *Telegram) playback(message *telegram.Message) error {
311312

312313
// Due to Telegram restricted callback data to 1-64 bytes, it requires to store
313314
// playback URLs to database.
314-
data := []byte(strings.ReplaceAll(callbackPrefix()+message.Text, "/playback", ""))
315+
data := helper.String2Byte(strings.ReplaceAll(callbackPrefix()+message.Text, "/playback", ""))
315316
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString(data)}
316317
if err := t.store.CreatePlayback(pb); err != nil {
317318
logger.Error("store collections failed: %v", err)

storage/telegram.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package storage // import "github.com/wabarc/wayback/storage"
77
import (
88
"bytes"
99

10+
"github.com/wabarc/helper"
1011
"github.com/wabarc/logger"
1112
"github.com/wabarc/wayback/entity"
1213
bolt "go.etcd.io/bbolt"
@@ -19,7 +20,7 @@ func (s *Storage) createPlaybackBucket() error {
1920
}
2021
defer tx.Rollback()
2122

22-
_, err = tx.CreateBucketIfNotExists([]byte(entity.EntityPlayback))
23+
_, err = tx.CreateBucketIfNotExists(helper.String2Byte(entity.EntityPlayback))
2324
if err != nil {
2425
return err
2526
}
@@ -36,9 +37,9 @@ func (s *Storage) Playback(id int) (*entity.Playback, error) {
3637
var pb entity.Playback
3738

3839
err := s.db.View(func(tx *bolt.Tx) error {
39-
b := tx.Bucket([]byte(entity.EntityPlayback))
40+
b := tx.Bucket(helper.String2Byte(entity.EntityPlayback))
4041
v := b.Get(itob(id))
41-
pb.Source = string(v)
42+
pb.Source = helper.Byte2String(v)
4243
pb.ID = id
4344
return nil
4445
})
@@ -54,7 +55,7 @@ func (s *Storage) CreatePlayback(pb *entity.Playback) error {
5455
}
5556

5657
return s.db.Update(func(tx *bolt.Tx) (err error) {
57-
b := tx.Bucket([]byte(entity.EntityPlayback))
58+
b := tx.Bucket(helper.String2Byte(entity.EntityPlayback))
5859
id, err := b.NextSequence()
5960
if err != nil {
6061
logger.Error("generate id for playback failed: %v", err)

0 commit comments

Comments
 (0)