Skip to content

Using a stopwatch and timespan instead of DateTime and uint - #1171

Open
batzen wants to merge 2 commits into
dotnet:mainfrom
batzen:StopWatchAndTimeSpanInsteadOfUintAndDateTime
Open

batzen wants to merge 2 commits into
dotnet:mainfrom
batzen:StopWatchAndTimeSpanInsteadOfUintAndDateTime

Conversation

@batzen

@batzen batzen commented Jul 3, 2019

Copy link
Copy Markdown
Contributor

This fixes #1170

@ghost
ghost requested review from rladuca, ryalanms, stevenbrix and vatsan-madhavan July 3, 2019 20:07
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Jul 3, 2019
@ghost
ghost requested a review from SamBent July 3, 2019 20:07
}
}
while (!endOfParagraph && DateTime.Now < stopTime);
while (!endOfParagraph && measureStopwatch.Elapsed < stopTimeout);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know how sensitive this code-path is to the accuracy of this measurement? the differences pointed out by SO make me wonder if we could possibly alter behavior that was dependent on the inaccuracy of this: https://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how sensitive the code is to the accuracy. I proposed this change not only for increased accuracy, but primarily for increased performance.
Accessing DateTime.Now is much slower than using a stopwatch. And every bit of performance gain we can achieve, especially if it's easy, should be taken, i guess.
I could write a benchmark to show the real difference if you want me to.

@weltkante weltkante Oct 3, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll probably never matter, but DateTime.Now has correctness issues when being used for measuring timeouts or implementing timers, as the clock can be changed both manually and automatically (time zone changes esp. scheduled ones like summer/winter time changes can introduce an unexpected one hour delay). DateTime.UtcNow avoids the timezone problem and is faster than DateTime.Now but is still suspect to manual clock changes.

@stevenbrix

Copy link
Copy Markdown
Contributor

Hey @batzen, thanks for filing this issue and sending out a PR! Sorry for the lack of communication on this PR, we're all pretty new to working in OSS and haven't been the best at following through with things.

I'd like to help get this PR moving along, since it seems like this is the right thing to do, I just want to make sure we aren't regressing anything.

@batzen

batzen commented Oct 3, 2019

Copy link
Copy Markdown
Contributor Author

No worries, i didn't expect fast reactions as i already imagined that the whole team is busy open sourcing the code and getting it ready for the public.
I am very happy that WPF is finally open source.

At the time i wrote the PR there were no tests and i could run, or i was unable to find those.
Is there anything i can do to help to prevent regressions?

@batzen

batzen commented Apr 26, 2020

Copy link
Copy Markdown
Contributor Author

@stevenbrix any news on this?

@stevenbrix

Copy link
Copy Markdown
Contributor

@batzen thanks for the ping. I haven't been working on WPF, and I actually can't help you out here anymore unfortunately 😞

@fabiant3 or @SamBent might be able to help provide more clarity here, and what the plan is for getting WPF into a state where we can comfortably accept community PRs

@stevenbrix

stevenbrix commented Apr 26, 2020 •

Copy link
Copy Markdown
Contributor

To further explain my reasoning/hesitation on this change:

I just want to make sure we've done our due diligence, because a 15-20% difference in a measurement seems like it could have unintended consequences. For example, if we're 20% slower and think that we're giving 200ms to read the paragraph, but in reality this completes in 220-230ms in most cases, then we have a change in behavior. Before we'd be exiting the loop because endOfParagraph was true, but now we're exiting because measureStopwatch.Elapsed < stopTimeout is true. It's unclear to me if this means we have a bug or not. Do you have any idea on the differences in these code paths:

if (!endOfParagraph)
{
       // Ran out of time.  Defer to background layout.
       SetFlags(true, Flags.BackgroundLayoutPending);
       this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(OnBackgroundMeasure), null);
}
else
{
        // Finished the entire document.  Stop background layout.
        SetFlags(false, Flags.BackgroundLayoutPending);
}

@batzen

batzen commented May 6, 2020

Copy link
Copy Markdown
Contributor Author

Well, currently, by using DateTime the code suffers from these differences. It might change it's behavior on every invocation. Using a Stopwatch would make the behavior stable and predictable.
I don't understand why making the code work correctly would be an issue here.

@ryalanms ryalanms added the Performance Performance related issue label Jan 7, 2021
Base automatically changed from master to main March 17, 2021 17:38
@ryalanms
ryalanms requested a review from a team as a code owner March 17, 2021 17:38
@batzen

batzen commented Mar 12, 2022

Copy link
Copy Markdown
Contributor Author

@dotnet/wpf-developers any news on this one?

@batzen batzen added the Community Contribution A label for all community Contributions label May 5, 2022
@ghost ghost assigned batzen May 5, 2022
@singhashish-wpf singhashish-wpf added the DrtsPassed Smoke tests (DRTs passed) label Jun 29, 2022
@singhashish-wpf

Copy link
Copy Markdown
Contributor

This PR has passed smoke tests (DRTs) and is under review.

@batzen

batzen commented Nov 9, 2024

Copy link
Copy Markdown
Contributor Author

@singhashish-wpf any news? The PR seems to have passed DRTs over two years ago. Has it been reviewed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution A label for all community Contributions DrtsPassed Smoke tests (DRTs passed) Performance Performance related issue PR metadata: Label to tag PRs, to facilitate with triage Under Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextBoxView uses DateTime.Now for time measurements

6 participants