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

Multithreaded calls to Local::now() take a lot of time, which is abnormal. #1636

Open
chan369 opened this issue Dec 4, 2024 · 4 comments
Open

Comments

@chan369
Copy link

chan369 commented Dec 4, 2024

When Local::now() is called 10,000 times in a single thread, it barely takes any time. However, when called in a multithreaded environment, it takes significantly more time. Below is the example code:
fn test_proc()
{
let i=Instant::now();
for _ in 0..10000
{
let v=Local::now();
}
let d=i.elapsed();
println!("time:{}",d.as_millis());
}

fn main()
{
println!("first time");
for _ in 0..1
{
thread::spawn(test_proc);
}
sleep(Duration::from_secs(5));
println!("second time");
for _ in 0..10
{
thread::spawn(test_proc);
}
sleep(Duration::from_secs(5));
}

Output:
first time
time:4
second time
time:362
time:363
time:363
time:365
time:364
time:364
time:365
time:365
time:364
time:365

What's happening here?

@djc
Copy link
Member

djc commented Dec 4, 2024

I'm guessing there might be a bunch of contention on the RefCell that we use to cache the timezone data?

What platform/OS is this?

@chan369
Copy link
Author

chan369 commented Dec 5, 2024

I'm guessing there might be a bunch of contention on the RefCell that we use to cache the timezone data?

What platform/OS is this?

Windows 10 professional

@djc
Copy link
Member

djc commented Dec 6, 2024

My former comment assumed Unix. For Windows, I'm not sure what's going on -- I don't think there are any locks in chrono's Windows code so I'm guessing something in the OS API is causing contention?

The Windows code is here:

https://github.com/chronotope/chrono/blob/main/src/offset/local/windows.rs

I think we'd be calling into <Local as TimeZone>::offset_from_utc_datetime().

@ChrisDenton would you happen to have any knowledge of this stuff (or know someone who does)?

@ChrisDenton
Copy link

I've not looked into it yet but my immediate reaction in such cases is to always check allocations first. The built-in Windows allocator is synchronous so a lot of threads allocating at the same time is going to be slow.

Try and see if using mimalloc solves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants