Bug 9418 - Change internal operation of ServicePoint class
Summary: Change internal operation of ServicePoint class
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-01-09 09:50 UTC by Dean Cleaver
Modified: 2013-02-12 01:33 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Dean Cleaver 2013-01-09 09:50:01 UTC
Currently, the ServicePoint class uses DateTime.Now in several places, and this is a seemingly expensive call (internally, it makes calls to get time zone information, daylight savings information etc on every call).

I wondered if it was possible to change all references in ServicePoint from DateTime.Now to DateTime.UtcNow which appears to be a much more efficient call, and then in the public property simply change it to this:

public DateTime IdleSince {
	get {
		return idleSince.ToLocalTime();
	internal set {
		lock (locker)
			idleSince = value.ToUniversalTime();

This should mean there is no change at all in functionality, just ideally better performance. Currently, DateTime.Now is the highest single use of CPU time in my app, because it calls a web service every 15 seconds which calls the ServicePoint to check AvailableForRecycling.

This refactoring could also apply to a lot of other classes, but I have not looked beyond this one.
Comment 1 Miguel de Icaza [MSFT] 2013-02-12 00:26:55 UTC
It only checks for timezone changes every minute, are you sure that ServicePoint is the cause of your problem?

This is the source for DateTime.Now:

		// To reduce the time consumed by DateTime.Now, we keep
		// the difference to map the system time into a local
		// time into `to_local_time_span', we record the timestamp
		// for this in `last_now'
		static object to_local_time_span_object;
		static long last_now;
		public static DateTime Now {
			get {
				long now = GetNow ();
				DateTime dt = new DateTime (now);

				if (Math.Abs (now - last_now) > TimeSpan.TicksPerMinute){
					to_local_time_span_object = TimeZone.CurrentTimeZone.GetLocalTimeDiff (dt);
					last_now = now;


				// This is boxed, so we avoid locking.
				DateTime ret = dt + (TimeSpan) to_local_time_span_object;
				ret.encoded |= ((long)DateTimeKind.Local << KindShift);
				return ret;
Comment 2 Miguel de Icaza [MSFT] 2013-02-12 00:48:47 UTC
I applied the suggestion to Mono 3.0 anyways.
Comment 3 Dean Cleaver 2013-02-12 00:53:57 UTC
Miguel, according to Instruments it was - was using 3% overall of my CPU time, which was the highest of any single function. That said, it was being called every 15 seconds, so it would have been circumventing your 1 minute cache every 4 calls anyway.

Thank you for applying the suggestion.
Comment 4 Miguel de Icaza [MSFT] 2013-02-12 01:29:57 UTC
I dont see how this can use 3% of CPU over a span of 15 minutes, if you only ever hit that function every 15 minutes.

Would you mind taking a screenshot of the call trace when DateTime.Now was being slow?   I wonder what it could be that is so bad, perhaps the issue is beyond ServicePoint, and this fix wont solve your problem.
Comment 5 Dean Cleaver 2013-02-12 01:33:04 UTC
Every 15 seconds, not every 15 minutes. Next time I do a test I will screen-cap the results from an Instruments trace for you.