From 1511a158e64872782b7ed67da352bb8f67fb334b Mon Sep 17 00:00:00 2001 From: ritbrz Date: Fri, 8 Nov 2024 23:10:00 +0800 Subject: [PATCH] Replace Lock() with RLock() to optimize performance --- constellation.go | 4 ++-- outputer.go | 16 ++++++++-------- season.go | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/constellation.go b/constellation.go index 005b32e1..5bfba2d9 100755 --- a/constellation.go +++ b/constellation.go @@ -42,8 +42,8 @@ func (c Carbon) Constellation() string { index = i } } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["constellations"]; ok { slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { diff --git a/outputer.go b/outputer.go index 4b1c878d..6f255699 100644 --- a/outputer.go +++ b/outputer.go @@ -47,8 +47,8 @@ func (c Carbon) ToMonthString(timezone ...string) string { if len(c.lang.resources) == 0 { c.lang.SetLocale(defaultLocale) } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["months"]; ok { slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { @@ -70,8 +70,8 @@ func (c Carbon) ToShortMonthString(timezone ...string) string { if len(c.lang.resources) == 0 { c.lang.SetLocale(defaultLocale) } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["short_months"]; ok { slice := strings.Split(resources, "|") if len(slice) == MonthsPerYear { @@ -93,8 +93,8 @@ func (c Carbon) ToWeekString(timezone ...string) string { if len(c.lang.resources) == 0 { c.lang.SetLocale(defaultLocale) } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["weeks"]; ok { slice := strings.Split(resources, "|") if len(slice) == DaysPerWeek { @@ -116,8 +116,8 @@ func (c Carbon) ToShortWeekString(timezone ...string) string { if len(c.lang.resources) == 0 { c.lang.SetLocale(defaultLocale) } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["short_weeks"]; ok { slice := strings.Split(resources, "|") if len(slice) == DaysPerWeek { diff --git a/season.go b/season.go index f13adb1a..76402c75 100755 --- a/season.go +++ b/season.go @@ -38,8 +38,8 @@ func (c Carbon) Season() string { index = season.index } } - c.lang.rw.Lock() - defer c.lang.rw.Unlock() + c.lang.rw.RLock() + defer c.lang.rw.RUnlock() if resources, ok := c.lang.resources["seasons"]; ok { slice := strings.Split(resources, "|") if len(slice) == QuartersPerYear {