Skip to content

Commit

Permalink
Converting byte slice and string without memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Dec 26, 2021
1 parent 1dd6d0d commit adea3bd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion reduxer/reduxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func Do(ctx context.Context, urls ...string) (bundles Bundles, err error) {
}
fn := strings.TrimRight(helper.FileName(shot.URL, ""), "html") + "txt"
fp := filepath.Join(dir, fn)
if err := os.WriteFile(fp, []byte(article.TextContent), 0o600); err == nil && article.TextContent != "" {
if err := os.WriteFile(fp, helper.String2Byte(article.TextContent), 0o600); err == nil && article.TextContent != "" {
if err := helper.SetField(&assets.Txt, "Local", fp); err != nil {
logger.Error("assign field Txt to assets struct failed: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions service/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/wabarc/helper"
"github.com/wabarc/logger"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
Expand Down Expand Up @@ -222,7 +223,7 @@ func (d *Discord) buttonHandlers() map[string]func(*discord.Session, *discord.In

s.ChannelTyping(i.Message.ChannelID)

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

// Due to Discord restricted custom_id up to 100 characters, it requires to store
// playback URLs to database.
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString([]byte(text))}
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString(helper.String2Byte(text))}
if err := d.store.CreatePlayback(pb); err != nil {
logger.Error("store collections failed: %v", err)
return err
Expand Down
7 changes: 4 additions & 3 deletions service/httpd/httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/wabarc/helper"
"github.com/wabarc/logger"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
Expand Down Expand Up @@ -63,11 +64,11 @@ func (web *web) handle(pool pooling.Pool) http.Handler {
web.router.HandleFunc("/playback", web.playback).Methods(http.MethodPost)

web.router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
w.Write(helper.String2Byte("ok"))
}).Name("healthcheck")

web.router.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(version.Version))
w.Write(helper.String2Byte(version.Version))
}).Name("version")

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

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

return web.router
Expand Down
5 changes: 3 additions & 2 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/fatih/color"
"github.com/wabarc/helper"
"github.com/wabarc/logger"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/config"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (t *Telegram) Serve() (err error) {
return false
}

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

// Due to Telegram restricted callback data to 1-64 bytes, it requires to store
// playback URLs to database.
data := []byte(strings.ReplaceAll(callbackPrefix()+message.Text, "/playback", ""))
data := helper.String2Byte(strings.ReplaceAll(callbackPrefix()+message.Text, "/playback", ""))
pb := &entity.Playback{Source: base64.StdEncoding.EncodeToString(data)}
if err := t.store.CreatePlayback(pb); err != nil {
logger.Error("store collections failed: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions storage/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package storage // import "github.com/wabarc/wayback/storage"
import (
"bytes"

"github.com/wabarc/helper"
"github.com/wabarc/logger"
"github.com/wabarc/wayback/entity"
bolt "go.etcd.io/bbolt"
Expand All @@ -19,7 +20,7 @@ func (s *Storage) createPlaybackBucket() error {
}
defer tx.Rollback()

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

err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(entity.EntityPlayback))
b := tx.Bucket(helper.String2Byte(entity.EntityPlayback))
v := b.Get(itob(id))
pb.Source = string(v)
pb.Source = helper.Byte2String(v)
pb.ID = id
return nil
})
Expand All @@ -54,7 +55,7 @@ func (s *Storage) CreatePlayback(pb *entity.Playback) error {
}

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

0 comments on commit adea3bd

Please sign in to comment.