Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow date parsing #10942

Closed
bep opened this issue May 15, 2023 · 2 comments · Fixed by #10943
Closed

Slow date parsing #10942

bep opened this issue May 15, 2023 · 2 comments · Fixed by #10943
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented May 15, 2023

I'm investigating some performance degradation in some work I'm doing, and see time parsing (unrelated to the work I'm doing) light up as a christmas tree in the memory profiler:

flat  flat%   sum%        cum   cum%
2097170 13.54% 13.54%    2097170 13.54%  time.cloneString (inline)
1409116  9.10% 22.64%    3506286 22.64%  time.newParseError

I guess this comes from the ineffective way of guessing the format in the upstream cast library:

func parseDateWith(s string, location *time.Location, formats []timeFormat) (d time.Time, e error) {

	for _, format := range formats {
		if d, e = time.Parse(format.format, s); e == nil {

			// Some time formats have a zone name, but no offset, so it gets
			// put in that zone name (not the default one passed in to us), but
			// without that zone's offset. So set the location manually.
			if format.typ <= timeFormatNamedTimezone {
				if location == nil {
					location = time.Local
				}
				year, month, day := d.Date()
				hour, min, sec := d.Clock()
				d = time.Date(year, month, day, hour, min, sec, d.Nanosecond(), location)
			}

			return
		}
	}
	return d, fmt.Errorf("unable to parse date: %s", s)
}
@bep bep self-assigned this May 15, 2023
@bep bep removed the NeedsTriage label May 15, 2023
@bep bep added this to the v0.112.0 milestone May 15, 2023
bep added a commit to bep/cast that referenced this issue May 15, 2023
The current top 2 formats are, from some checks I've done, 2 dominant formats used in Hugo projects.

Checking these first has a big effect:

```bash

name                  old time/op    new time/op    delta
CommonTimeLayouts-10    1.45µs ± 0%    0.33µs ± 1%  -77.45%  (p=0.029 n=4+4)

name                  old alloc/op   new alloc/op   delta
CommonTimeLayouts-10    1.55kB ± 0%    0.18kB ± 0%  -88.14%  (p=0.029 n=4+4)

name                  old allocs/op  new allocs/op  delta
CommonTimeLayouts-10      38.0 ± 0%       6.0 ± 0%  -84.21%  (p=0.029 n=4+4)
```

See gohugoio/hugo#10942
bep added a commit to spf13/cast that referenced this issue May 15, 2023
The current top 2 formats are, from some checks I've done, 2 dominant formats used in Hugo projects.

Checking these first has a big effect:

```bash

name                  old time/op    new time/op    delta
CommonTimeLayouts-10    1.45µs ± 0%    0.33µs ± 1%  -77.45%  (p=0.029 n=4+4)

name                  old alloc/op   new alloc/op   delta
CommonTimeLayouts-10    1.55kB ± 0%    0.18kB ± 0%  -88.14%  (p=0.029 n=4+4)

name                  old allocs/op  new allocs/op  delta
CommonTimeLayouts-10      38.0 ± 0%       6.0 ± 0%  -84.21%  (p=0.029 n=4+4)
```

See gohugoio/hugo#10942
bep added a commit to bep/hugo that referenced this issue May 15, 2023
```
name                    old time/op    new time/op    delta
Baseline/skiprender-10    24.5ms ±10%    22.3ms ± 4%   -8.93%  (p=0.029 n=4+4)

name                    old alloc/op   new alloc/op   delta
Baseline/skiprender-10    30.0MB ± 0%    26.2MB ± 0%  -12.74%  (p=0.029 n=4+4)

name                    old allocs/op  new allocs/op  delta
Baseline/skiprender-10      373k ± 0%      279k ± 0%  -25.07%  (p=0.029 n=4+4)
```

Fixes gohugoio#10942
@bep
Copy link
Member Author

bep commented May 15, 2023

We should probably start using https://github.com/araddon/dateparse somehow, but that feels like a little more work than I'm prepare to deliver for this today.

bep added a commit that referenced this issue May 15, 2023
```
name                    old time/op    new time/op    delta
Baseline/skiprender-10    24.5ms ±10%    22.3ms ± 4%   -8.93%  (p=0.029 n=4+4)

name                    old alloc/op   new alloc/op   delta
Baseline/skiprender-10    30.0MB ± 0%    26.2MB ± 0%  -12.74%  (p=0.029 n=4+4)

name                    old allocs/op  new allocs/op  delta
Baseline/skiprender-10      373k ± 0%      279k ± 0%  -25.07%  (p=0.029 n=4+4)
```

Fixes #10942
@github-actions
Copy link

github-actions bot commented Jun 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant