From 9b00959fdb6d0c6a746b6cd83f4f041fba070acd Mon Sep 17 00:00:00 2001 From: ChaoticByte Date: Thu, 12 Jun 2025 17:23:08 +0200 Subject: [PATCH] Improve memory efficiency of noticesToBeSent by mapping pointers instead of WidNotice structs --- mail.go | 2 +- main.go | 9 +++++---- notice.go | 2 +- template.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/mail.go b/mail.go index d2f6ae4..7cefa09 100644 --- a/mail.go +++ b/mail.go @@ -49,7 +49,7 @@ type SmtpSettings struct { Password string `json:"password"` } -func sendNotices(recipient string, notices []WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, cache *map[string][]byte) error { +func sendNotices(recipient string, notices []*WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, cache *map[string][]byte) error { logger.debug("Generating and sending mails for recipient " + recipient + " ...") cacheHits := 0 cacheMisses := 0 diff --git a/main.go b/main.go index 89456e3..8fc4ab0 100644 --- a/main.go +++ b/main.go @@ -121,16 +121,17 @@ func main() { if len(newNotices) > 0 { logger.info("Sending email notifications ...") // mail recipient : pointer to slice of wid notices to be sent - noticesToBeSent := map[string][]WidNotice{} + noticesToBeSent := map[string][]*WidNotice{} recipientsNotified := 0 var err error for _, l := range *config.Lists { // Filter notices for this list for _, f := range l.Filter { for _, n := range f.filter(newNotices) { + np := &n for _, r := range l.Recipients { - if !noticeSliceContains(noticesToBeSent[r], n) { - noticesToBeSent[r] = append(noticesToBeSent[r], n) + if !noticeSliceContains(noticesToBeSent[r], np) { + noticesToBeSent[r] = append(noticesToBeSent[r], np) } } } @@ -138,7 +139,7 @@ func main() { } for r, notices := range noticesToBeSent { // sort by publish date - slices.SortFunc(notices, func(a WidNotice, b WidNotice) int { + slices.SortFunc(notices, func(a *WidNotice, b *WidNotice) int { if a.Published == b.Published { return 0 } else if a.Published.After(b.Published) { diff --git a/notice.go b/notice.go index 758d85d..393f2c9 100644 --- a/notice.go +++ b/notice.go @@ -25,7 +25,7 @@ type WidNotice struct { PortalUrl string } -func noticeSliceContains(notices []WidNotice, notice WidNotice) bool { +func noticeSliceContains(notices []*WidNotice, notice *WidNotice) bool { for _, x := range notices { if x.Uuid == notice.Uuid { return true diff --git a/template.go b/template.go index 7d078e8..b07434c 100644 --- a/template.go +++ b/template.go @@ -33,7 +33,7 @@ Sent by WidNotifier {{ .WidNotifierVersion }} ` type TemplateData struct { - WidNotice + *WidNotice WidNotifierVersion string }