Skip to content

Commit 639b3c5

Browse files
committed
perf: memory usage
1 parent 09d83a9 commit 639b3c5

File tree

5 files changed

+54
-41
lines changed

5 files changed

+54
-41
lines changed

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.7.6"
10+
Version string = "0.7.7"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

fetcher/service.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"context"
1111
es "errors"
1212
"fmt"
13-
"sync"
13+
"runtime"
1414

1515
. "ManyACG/logger"
1616

@@ -20,6 +20,7 @@ import (
2020
)
2121

2222
func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, storage storage.Storage, fromID int64) error {
23+
2324
artworkInDB, err := service.GetArtworkByURL(ctx, artwork.SourceURL)
2425
if err == nil && artworkInDB != nil {
2526
Logger.Debugf("Artwork %s already exists", artwork.Title)
@@ -73,22 +74,17 @@ func PostAndCreateArtwork(ctx context.Context, artwork *types.Artwork, bot *tele
7374
return fmt.Errorf("error when creating artwork %s: %w", artwork.SourceURL, err)
7475
}
7576
go afterCreate(context.TODO(), artwork, bot, storage, fromID)
77+
7678
return nil
7779
}
7880

7981
func afterCreate(ctx context.Context, artwork *types.Artwork, bot *telego.Bot, _ storage.Storage, fromID int64) {
80-
var wg sync.WaitGroup
8182
for _, picture := range artwork.Pictures {
82-
wg.Add(1)
83-
go func(picture *types.Picture) {
84-
defer wg.Done()
85-
if err := service.ProcessPictureAndUpdate(ctx, picture); err != nil {
86-
Logger.Warnf("error when processing %d of artwork %s: %s", picture.Index, artwork.Title, err)
87-
}
88-
}(picture)
83+
if err := service.ProcessPictureAndUpdate(ctx, picture); err != nil {
84+
Logger.Warnf("error when processing %d of artwork %s: %s", picture.Index, artwork.Title, err)
85+
}
8986
}
90-
wg.Wait()
91-
87+
runtime.GC()
9288
sendNotify := fromID != 0 && bot != nil
9389
artworkID, err := service.GetArtworkIDByPicture(ctx, artwork.Pictures[0])
9490
artworkTitleMarkdown := common.EscapeMarkdown(artwork.Title)

service/picture.go

+3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func ProcessPictureAndUpdate(ctx context.Context, picture *types.Picture) error
119119
if err != nil {
120120
return nil, err
121121
}
122+
defer func() {
123+
fileBytes = nil
124+
}()
122125
hash, err := common.GetPhash(fileBytes)
123126
if err != nil {
124127
return nil, err

storage/webdav/webdav.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (w *Webdav) SavePicture(artwork *types.Artwork, picture *types.Picture) (*t
6060
} else {
6161
go common.PurgeFileAfter(cachePath, time.Duration(config.Cfg.Storage.CacheTTL)*time.Second)
6262
}
63+
fileBytes = nil
6364
return storageInfo, nil
6465
}
6566

telegram/artwork.go

+42-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"ManyACG/storage"
88
"ManyACG/types"
99
"bytes"
10+
"runtime"
1011
"time"
1112

1213
. "ManyACG/logger"
@@ -25,29 +26,11 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
2526
return nil, errors.ErrNilArtwork
2627
}
2728

28-
inputMediaPhotos := make([]telego.InputMedia, len(artwork.Pictures))
29-
for i, picture := range artwork.Pictures {
30-
fileBytes, err := storage.GetFile(picture.StorageInfo)
31-
if err != nil {
32-
Logger.Errorf("failed to get file: %s", err)
33-
return nil, err
34-
}
35-
fileBytes, err = common.CompressImage(fileBytes, 10, 2560)
29+
if len(artwork.Pictures) <= 10 {
30+
inputMediaPhotos, err := getInputMediaPhotos(artwork, storage, 0, len(artwork.Pictures))
3631
if err != nil {
37-
Logger.Errorf("failed to compress image: %s", err)
3832
return nil, err
3933
}
40-
photo := telegoutil.MediaPhoto(telegoutil.File(telegoutil.NameReader(bytes.NewReader(fileBytes), picture.StorageInfo.Path)))
41-
if i == 0 {
42-
photo = photo.WithCaption(GetArtworkHTMLCaption(artwork)).WithParseMode(telego.ModeHTML)
43-
}
44-
if artwork.R18 {
45-
photo = photo.WithHasSpoiler()
46-
}
47-
inputMediaPhotos[i] = photo
48-
}
49-
50-
if len(inputMediaPhotos) <= 10 {
5134
return bot.SendMediaGroup(
5235
telegoutil.MediaGroup(
5336
ChannelChatID,
@@ -56,16 +39,17 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
5639
)
5740
}
5841

59-
allMessages := make([]telego.Message, len(inputMediaPhotos))
60-
for i := 0; i < len(inputMediaPhotos); i += 10 {
42+
allMessages := make([]telego.Message, len(artwork.Pictures))
43+
for i := 0; i < len(artwork.Pictures); i += 10 {
6144
end := i + 10
62-
if end > len(inputMediaPhotos) {
63-
end = len(inputMediaPhotos)
45+
if end > len(artwork.Pictures) {
46+
end = len(artwork.Pictures)
6447
}
65-
mediaGroup := telegoutil.MediaGroup(
66-
ChannelChatID,
67-
inputMediaPhotos[i:end]...,
68-
)
48+
inputMediaPhotos, err := getInputMediaPhotos(artwork, storage, i, end)
49+
if err != nil {
50+
return nil, err
51+
}
52+
mediaGroup := telegoutil.MediaGroup(ChannelChatID, inputMediaPhotos...)
6953
if i > 0 {
7054
mediaGroup = mediaGroup.WithReplyParameters(&telego.ReplyParameters{
7155
ChatID: ChannelChatID,
@@ -77,7 +61,36 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
7761
return nil, err
7862
}
7963
copy(allMessages[i:], messages)
80-
time.Sleep(time.Duration(int(config.Cfg.Telegram.Sleep)*len(inputMediaPhotos[i:end])) * time.Second)
64+
time.Sleep(time.Duration(int(config.Cfg.Telegram.Sleep)*len(inputMediaPhotos)) * time.Second)
8165
}
8266
return allMessages, nil
8367
}
68+
69+
// start from 0
70+
func getInputMediaPhotos(artwork *types.Artwork, storage storage.Storage, start, end int) ([]telego.InputMedia, error) {
71+
inputMediaPhotos := make([]telego.InputMedia, end-start)
72+
for i := start; i < end; i++ {
73+
picture := artwork.Pictures[i]
74+
fileBytes, err := storage.GetFile(picture.StorageInfo)
75+
if err != nil {
76+
Logger.Errorf("failed to get file: %s", err)
77+
return nil, err
78+
}
79+
fileBytes, err = common.CompressImage(fileBytes, 10, 2560)
80+
if err != nil {
81+
Logger.Errorf("failed to compress image: %s", err)
82+
return nil, err
83+
}
84+
photo := telegoutil.MediaPhoto(telegoutil.File(telegoutil.NameReader(bytes.NewReader(fileBytes), picture.StorageInfo.Path)))
85+
if i == 0 {
86+
photo = photo.WithCaption(GetArtworkHTMLCaption(artwork)).WithParseMode(telego.ModeHTML)
87+
}
88+
if artwork.R18 {
89+
photo = photo.WithHasSpoiler()
90+
}
91+
inputMediaPhotos[i-start] = photo
92+
fileBytes = nil
93+
}
94+
runtime.GC()
95+
return inputMediaPhotos, nil
96+
}

0 commit comments

Comments
 (0)