C# 6.0: An Introduction
booker.codes
booker.codes
https://channel9.msdn.com/Search?term=torgersen#ch9Search&pu...
You can pick the 7 minute quick overview, or the 1 hour session at Ignite2015.
(Side note to the poster... your blog layout displays a constant masthead using over 40% of the width. A technical audience for your blog might find it excessive to have that much space dedicated to "brand awareness" about your availability to create custom websites.)
In response to your side note: 40% is a bit of an exaggeration (unless your resolution is extremely low) but I do appreciate your point - I can see how the masthead might be a bit obnoxious. I will tweak the theme accordingly in the near future. Thanks.
No, it's not exaggeration.
I'm measuring the width of your actual text content. I'm guessing you're measuring the width of the browser window which is not relevant to the ratio calculation.
Your masthead takes up 530 pixels. The total width from left edge of masthead to right edge of actual text is about 1296 pixels. On my Chrome browser, even if I maximize the window (press F11) to be 2560 pixels, your text content still maxes out at 1296 pixels (which leaves 1264 pixels of empty space on the right).
40.9% = 530/1296
They haven't revolutionized my code at all. I can't decide if I'm too stupid to realize when to use them, or if they're really just a bit useless for most normal code. I can see the point to it, it's just that most of the time I'd consider using it, it just isn't worth the extra effort. Usually seems to be a premature optimization.
Lambda expressions, now they revolutionized my code completely.
In general, as much as possible service could should be async... That said, a lot of code isn't. The breakup of the request pipeline and thread pooling in ASP.Net make it slightly less necessary, as it's still very efficient.
That said, you should probably introduce it using Parallel Linq as much as possible, and/or when you need to consume multiple resources in a given request.
If you're writing stand alone applications, and already using threads/thread pools, then it's probably less of an impact. If you're writing server/service applications then it does make a difference in terms of scalability.
To put it in context, 10K simultaneous requests is 10 billion page views per day (10000 * 10 * 60 * 60 * 24, assuming 100ms processing time, which is actually pretty high for .Net, mine are usually 60ms even on crap hardware. Yes it's a silly calculation, but it gives you an idea about the scale we're talking about). If you're at that scale, you can afford to hire John Skeet to go back over your code and add a few `async`s into it where appropiate.
From what you just said it seems that anyone using it all the time is unnecessarily cluttering their code.
Also, It's not like it doesn't do you any good. With async you may be able to reduce the size of your cloud instances, as an example... saving money in the end. Doesn't mean as much if you have a dedicated server, or existing codebase though. If you're doing a green web project, you should probably use async from the start.
In general, denormalizing and caching data, and using async requests are what improves overall responsiveness in an application. Async allows for a given request to have more than one outbound IO statement waiting for a response, and return the aggregate together.
The kind of web scale business that should be using async, let's be frank, those kind of projects don't use C#. Go look at the YC list of technology, not a single C# user on there.
If that's all you the benefit of using async is, it hopefully looks like I'm ok not using it.
I don't disagree that it isn't that much of a benefit in many cases, but tbh if I'm to chose between async usage and, for example ever seeing EntLib Data classes, or a DI/IoC tool when no testing is being done, I'll take async. There are plenty of practices in C#/.Net shops that are just hideous to work with, async imho isn't one of them.
Most sites have a peak load. Some of ours have a difference of 4x from peak to trough.
Traffic bursts are also common. Especially if something is going wrong. You should always plan for something going wrong.
Nothing sucks more than going down because of a traffic burst because you just brought your systems back up from a failure. In the past we didn't always plan for this and it could take an hour to get the site back to full health even after we fixed the original problem.
Ideally, you'd have one thread per CPU. That's the most efficient. In reality, enough stuff blocks we gotta throw extra threads to let things saturate.
You can scale up the number of process-scheduled tasks much more than the number of kernel level threads. (Though, esp on x64, people might get really far just spamming threads.)
inbound service request
request remote resource
wait
request additional details for logged in user
wait
request X info
wait
resolve inbound request
Scenario 2 inbound service request
async requests
request remote resource
request additional details for logged in user
request X info
wait
resolve inbound request
Scenario 2 will finish quicker...Requests as part of a Service pipeline are best served as async. User -> http -> service endpoint <(requests to db's, and another service as async)> -> async response
In general, if you are IO bound (waiting on IO/service responses), then you will be well served with async. As part of a service pipeline that will access multiple backend resources it makes a lot of sense. If you can share/cache foreign data sources, it makes less sense. Cache first, other optimization strategies second and as needed. Unless it's a green project, then try to be async from the start.
I write an application that previously wasn't using threading to avoid blocking the UI. Certain long-running tasks would freeze the app for some time. It's not a big deal since it's an internal app for my work team, but now that the async syntax and semantics are a bit easier, I've started using them and my app is more responsive.
Async doesn't do anything fundamentally new. If you already implemented multithreading in your application that you're happy with, there's no compelling reason to switch to doing it the async way. But for new development it seems like it should be the default.
This is correct, but not in the way you're describing. The API calls that it ultimately makes have been in place before C# 5. C# 5 just made writing that kind of code significantly simpler than it was before.
> If you already implemented multithreading in your application that you're happy with, there's no compelling reason to switch to doing it the async way.
This isn't necessarily true.
From the perspective of a client application, it's less resource-intensive to use async code than to spin up another thread. On mobile devices that's less battery life used, which is always a good thing.
From the perspective of a server, multithreading only gets you so far, plus they can eat up tons of memory in the process. Async code does not spin up new threads and allows for spikes in traffic to be handled gracefully rather than (a) using tons of threads, or (b) being unable to handle the load at all.
It looks like multithreaded code, it taste like using multithread technics, but it is sequential code with thread jumping.
The only benefit that I have found is that in winforms apps the call to back end does not block the ui thread. Otherwise it is just silly.
On the code revolutions, lambda expressions and half implemented linq. I call it half implemented because I am missing the ability to yield return to 2 separate IEnumerables from inside the same method, think results and errors.
What happens when IIS runs out of threads?
Without async code, it queues of requests and users sit there getting nothing from your server.
With async code, that's not the case! Well, until you reach the limit of what async code does, but that limit is quite high.
Typically a request thread that ends up blocking on I/O in some way (like accessing a database) sits there doing absolutely nothing for a very long time. The time it takes to get data from a database could be spent servicing many, many more requests! Async does just that. And before you ask, it doesn't spawn off any threads ... it leverages the asynchronous nature of the operating system to service I/O requests. It's actually really, really rad.
> Usually seems to be a premature optimization
If you have a user-facing system, it's most definitely not a premature optimization. If you know, for a fact, that you'll never need to worry about responsiveness, they're not worth it.
That's an irrelevant question until you legitimately need to scale out. Async code lets you scale up so that when 32,769 users access a site relatively at the same time, you don't force a user to wait for IIS to free up a new thread.
Apologies if I'm not following your train of thought here. The thread-pool has the threads right, not IIS? When you do something like Task.Run(...) the task-scheduler will schedule your work on a thread-pool thread (32,768 pre-allocated at start-up). Of course you have to wait for the scheduler to give you one of the threads from the pool.
Are you saying async doesn't need threads/TPL? I know it has some funky concurrency stuff that allows interleaved processing (rather than thread-pool processing), is this what you're referring to? Even IO would have to do something like this behind the scenes:
Result IOMethod()
{
EventWaitHandle wait = new AutoResetEvent(false);
// Do your IO - pass in the handle
wait.WaitOne();
return result;
}
Wouldn't the pre-IO and post-IO need to be run via the scheduler if you want it to run asynchronously? The WaitOne will release the thread back to the pool. How is async any different?Here's a fantastic blog post about the details of it all: http://blog.stephencleary.com/2013/11/there-is-no-thread.htm...
Async is a goto state machine. While the IO is set as blocked at the operating system level, it does something else. Then when it's unblocked, it jumps back.
It can be confusing because Task objects original main use was running stuff on background worker threads. That is no longer the case.
Note, you can use async and threads. But a lot of code using async and task are NOT using threads.
I like to think of it as utilizing the operating system more efficiently. I/O on a system is asynchronous by default. Async code will essentially delegate work so the OS can do what it does best!
In our particular case, and oversimplifying, the app is a smart router. Take a request, farm out pieces of it to multiple other servers, aggregate responses. Some of those requests finish in 5ms, some take several seconds, which is a crappy pattern to deal with. Today's not a particularly busy day, so we're doing about 70 req/sec/VM at 5% CPU on a dual core VM. In load tests I've had it up to 300 req/sec/VM with no problems. It actually bottlenecked our 10Gb network cards before the the CPU maxed out.
I haven't tried running the actual app without async because it would be a major rewrite. But I did some benchmarking making lots of parallel HTTP calls with threads vs async, and found that async was 10-20x faster and used less CPU.
We use it almost exclusively. Because of the new async/await syntax it is (almost) as easy to write and get correct as non-async code.
Async/await makes an entire class of problems go away (and, ok, swaps it for a class of easier-to-solve problems).
In a web application you always need to consider how your application will be impacted by the traffic. Are all of your requests served within 50 milliseconds? Fine, then you do not need async. Do your handlers ever call out to an external site or even a local database where it may block for more than 50 milliseconds? Then you need to consider what happens when almost all of the requests block at at same time: A colossal waste of resources.
Traditionally we have handled this by increasing the number of threads we allow the server to use for serving requests. However, threads are expensive in overhead (memory, cpu for context switching) and it affects the footprint of your application, and with that how dense you can pack virtual servers.
Enter async: When a request blocks on an external call (to an external site, database etc), the request relinquishes the thread which will be used to serve other requests.
What's more: The more you use async/await, the more the application will auto-scale. It will only start new threads when there's actual work to be done rather than when there's waiting to be done.
Feel free to PM me if you get stuck. I'm no one special but have years on the stuff (and was in c++ for years prior).
Ultimately, I think the best thing to do is just jump in and start a project that you're interested in. Accept the fact that you'll be hitting StackOverflow all the time, and when you're there, pay attention if you see comments and answers from Jon Skeet, Eric Lippert, etc. There are some decent email link-dump newsletters that are good and have a .NET slant, like Morning Brew[1] and Morning Dew[2], which can help keep you up on what's new, and often link to articles explaining various nooks of the .NET world. Avoid CodeProject - most of that shit is old, poorly written, and as often as not incorrect. Once you have your bearings a bit, C# In Depth[3] is a great resource on the nittier, grittier details of C# and the .NET framework.
[1] http://themorningbrew.net/ [2] http://www.alvinashcraft.com/ [3] http://amzn.to/1fv0zNH
If you are just getting started I always recommend the msdn tutorials.
https://msdn.microsoft.com/en-us/library/aa288436%28v=vs.71%...
Download Visual Studio Community (if you don't already have it) then try out one of the project templates. The tooling is excellent and it's very easy to get started.
Then it depends on what kind of code you need to write. I work with web/information systems so the focus is on SOLID over pure performance. Using DI and TDD is more important than being an absolute language expert.
Drop me a line if you want more details tips; I've helped a number of people make the transition. Email is in my profile :)
A book that should get you up to speed on 90% of C# relatively quickly is Professional C# 5.0 and .NET 4.5.1 by Jay Glynn. It's not got info on C#6 and .NET 4.6 yet, but the differences there are pretty quick/intuitive to pick up.
Afraid I can't help to much with the databases portion - it's something I'd like to find a book on myself! Good luck on the career switch! (:
> Read-Only Auto-Implemented Properties - Allow you to omit the getter from an automatic property, which makes it read-only.
This should be omit the setter right? :)
BTW, is there somewhere I can get notified as the new chapters become available? I'll probably miss the tweet and I don't think this is your blog?
Thank you for your interest in the series, too. If you want to be notified when new articles come out, you could [subscribe](https://booker.codes/rss/) to the blog via an RSS client or you could "watch" this [GitHub repository](https://github.com/alexbooker/articles).
http://www.telerik.com/blogs/understanding-net-just-in-time-...
There is whole lotta difference between execution of a "AOT compiled to machine code, potentially profile guided if need be" program and a "selectively compiled under severe time pressure that precludes deep compiler optimizations" one, much that JIT camp would like to brush under.
There is still a substantial gap between the pregnant promise of a JIT and its delivered performance. The gap that needs to be bridge does gets narrower by the year though. [Mono does have AOT compilation, but I had given up on a then buggy Mono and haven't found enough motivation to go back. Should I ? A non bugged F# on Linux would be really sweet.]
Under circumstances JIT can, and do outperform, but those are not nearly the common enough cases and configurations, at least not in the number crunching world. And some of the scenarios that get held up in JIT's defense are plain hypothetical, 'for arguments sake only'.
C# is much better than Java about interoperating across the managed - non-managed bridge, D is significantly better still. The poverty of the C# eco-system in number crunching libraries, or even production quality glues to native number crunching libraries, is really stark. I guess it was never an area they wanted C# to venture in. For example their standard math library does not work on float32. WTF! The first time I realized this, I couldn't believe it.
By the way the author of the article you linked has this to say:
JIT can generate faster code, because it targets the
current platform of execution. AOT compilation must
target the lowest common denominator among all possible
execution platforms.
This points to another bridge that needs crossing (the one between the AOT and JIT communities). Had there been a garbage collector for the web, that should have been history by now. Command line options for compilers have been a thing for quite a while.Things like array slices being first class in D while ArraySegment<> looks like a hack in C#, D templates beating the hell out of C# generics, functional programming in D is actually inlined almost 100% at compile-time while LINQ adds noticeable overhead, and the list goes on :)
No decent IDE/debugging story though, so overall C# still wins for me at the moment. Especially for large projects.
Now if you were to say that they are going to merge in a float32 math library, I will probably throw a party.
It would be funny to review older "almost as native speed" claims from .NET JIT "overlords" vs current ".NET native is as fast as native" new claims :-)
This is on stock Android 5.1.1, unrooted.
Looking forward to using these features someday but I'll pass on Windows and Azure for now.