Skip to content

Commit 317aaee

Browse files
authored
perf: replace from ParseInt/ParseFloat with faster Itoa
There is no need to use strconv.ParseInt and strconv.ParseFloat instead of the much simpler/faster strconv.Itoa.
1 parent d862f79 commit 317aaee

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

internal/reader/processor/reading_time.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func fetchWatchTime(websiteURL, query string, isoDate bool) (int, error) {
4949
}
5050
ret = int(parsedDuration.Minutes())
5151
} else {
52-
parsedDuration, err := strconv.ParseInt(duration, 10, 64)
52+
parsedDuration, err := strconv.Atoi(duration)
5353
if err != nil {
5454
return 0, fmt.Errorf("unable to parse duration %s: %v", duration, err)
5555
}
56-
ret = int(parsedDuration / 60)
56+
ret = parsedDuration / 60
5757
}
5858
return ret, nil
5959
}

internal/reader/processor/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,31 @@ func parseISO8601Duration(duration string) (time.Duration, error) {
2525
num := ""
2626

2727
for _, char := range after {
28-
var val float64
28+
var val int
2929
var err error
3030

3131
switch char {
3232
case 'Y', 'W', 'D':
3333
return 0, fmt.Errorf("the '%c' specifier isn't supported", char)
3434
case 'H':
35-
if val, err = strconv.ParseFloat(num, 64); err != nil {
35+
if val, err = strconv.Atoi(num); err != nil {
3636
return 0, err
3737
}
3838
d += time.Duration(val) * time.Hour
3939
num = ""
4040
case 'M':
41-
if val, err = strconv.ParseFloat(num, 64); err != nil {
41+
if val, err = strconv.Atoi(num); err != nil {
4242
return 0, err
4343
}
4444
d += time.Duration(val) * time.Minute
4545
num = ""
4646
case 'S':
47-
if val, err = strconv.ParseFloat(num, 64); err != nil {
47+
if val, err = strconv.Atoi(num); err != nil {
4848
return 0, err
4949
}
5050
d += time.Duration(val) * time.Second
5151
num = ""
52-
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
52+
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
5353
num += string(char)
5454
continue
5555
default:

internal/reader/processor/utils_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ func TestISO8601DurationParsingErrors(t *testing.T) {
7070
{"PT1a2H", "invalid character in the period"},
7171
{"PT3b4M", "invalid character in the period"},
7272
{"PT5c6S", "invalid character in the period"},
73-
// Test cases for actual ParseFloat errors (empty number before specifier)
74-
{"PTH", "strconv.ParseFloat: parsing \"\": invalid syntax"},
75-
{"PTM", "strconv.ParseFloat: parsing \"\": invalid syntax"},
76-
{"PTS", "strconv.ParseFloat: parsing \"\": invalid syntax"},
73+
// Test cases for actual Atoi errors (empty number before specifier)
74+
{"PTH", "strconv.Atoi: parsing \"\": invalid syntax"},
75+
{"PTM", "strconv.Atoi: parsing \"\": invalid syntax"},
76+
{"PTS", "strconv.Atoi: parsing \"\": invalid syntax"},
7777
// Invalid character
7878
{"PT1X", "invalid character in the period"},
7979
// Invalid character mixed

internal/ui/form/settings.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ func (s *SettingsForm) Validate() *locale.LocalizedError {
166166

167167
// NewSettingsForm returns a new SettingsForm.
168168
func NewSettingsForm(r *http.Request) *SettingsForm {
169-
entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 0)
169+
entriesPerPage, err := strconv.Atoi(r.FormValue("entries_per_page"))
170170
if err != nil {
171171
entriesPerPage = 0
172172
}
173-
defaultReadingSpeed, err := strconv.ParseInt(r.FormValue("default_reading_speed"), 10, 0)
173+
defaultReadingSpeed, err := strconv.Atoi(r.FormValue("default_reading_speed"))
174174
if err != nil {
175175
defaultReadingSpeed = 0
176176
}
177-
cjkReadingSpeed, err := strconv.ParseInt(r.FormValue("cjk_reading_speed"), 10, 0)
177+
cjkReadingSpeed, err := strconv.Atoi(r.FormValue("cjk_reading_speed"))
178178
if err != nil {
179179
cjkReadingSpeed = 0
180180
}

0 commit comments

Comments
 (0)