You are on page 1of 17

Designing .

NET Class Libraries


Session: CLR Performance Tips
Speaker(s): Rico Mariani
Transcription
Mariani: I am Rico Mariani, and I'm here today to talk to you about performance and
building performance culture in your team and some things you can do to
improve the performance of your managed libraries and applications
I thought I'd start maybe by doing a little !uick survey of the audience so I
could get a feel for "here you all are Maybe if you could raise your hand if
you've been "orking "ith managed code for a year or more# That's a good
chunk $nd maybe, so, say si% months or less at the other end# &i% months or
less, okay &o it looks like about '( percent at about si% months or less and
about () percent at a year or more and some some"here in bet"een there, so
that's a good mi%
*kay, "ell, I usually like to start to talk by +ust telling you a little bit about
myself so you have a clue "ho the heck I am and "hy you might "ant to listen
to my advice &o I told you my name already I'm Rico Mariani $nd in a fe"
"eeks, I'll be celebrating my ,-th anniversary at Microsoft, and so ,- years is a
long time to be here My first seven years "ere in the .eveloper division, and I
"orked on a variety of different products, starting "ith C-//that's not 0isual C-,
mind you, that's before the 0isual//from C- all the "ay up to about 0C1 "as
about "hen I left $nd then I did seven years in M&2 on a variety of pro+ects
there, starting "ith 3&ide"alk, "hich some of you may have heard of and
maybe some of you haven't Then I learned a bunch of different things about
operational systems and servers and back ends and reliability and all kinds of
cool stuff $nd then about t"o years ago, I returned to my roots and came
back to the .eveloper division, and I took a position as a Performance $rchitect
on the CLR, and that's "hat I've been doing for the last t"o years, and so it's
been !uite a ride &o that's me in a nutshell
$nd so today I "ant to give you some tips about performance, so let's move up
*kay, the first rule of performance is measure There's actually very fe" rules
"hen it comes to doing hardcore performance because so many things can
change out from under you, but, really, it all begins "ith having an
understanding of "hat your customer needs, "hat's going to be important to
your success, and being rigorous about measuring "hatever it is that's
important to you on a regular basis
2o", I like to say if you're not measuring, you're not really engineering
4ngineering is a !uantitative discipline &o any sort of approach that you take
that is about, "ell, let me guess or let me see "hat the best practices are or
"hatever else, people have called that anything from creating coding to maybe
soft"are craftsmanship, but it's something short of engineering, okay# &o the
!uantitative aspect is "hat makes it an engineering procedure
&o once you've decided that you're going to measure, a bunch of good things
happen for you, and I think that's really kind of one of the big benefits there
.esigning 24T Class Libraries Page , of ,5
6 '))7 Microsoft Corporation
The moment you say, okay, "ell, I need to measure because I "ant to be
engineering I'm going to "ant to measure *kay, so the ne%t !uestion is8
"ell, "hat should I measure# 9ell, right there already you're asking yourself a
great !uestion because in order to decide "hat to measure, you have to get in
touch "ith "ho your customer's going to be and "hat's important in any given
scenario *kay so you'll be saying, "ell, does my customer care about space#
.oes my customer care about time# .oes my customer care about disk
footprint# 9hat is it that my customer cares about any"ay, and is that al"ays
going to be the thing he cares about, or does my customer care about one thing
in some scenarios and something else in other scenarios, and ho" am I going to
do a good +ob of getting in touch "ith "hat my customer's going to need out of
the performance of my product# $ll of those kinds of metrics sort of force you
do"n an e%cellent path of understanding and process, because you'll be able to
take those metrics and milk them, as "e'll talk about, during other parts of the
soft"are development process and not +ust the performance analysis
*kay, so the first rule, measure If you haven't measured, you haven't done
your "hole +ob
The second rule is do your home"ork 2o", again, let me contrast this to
another kind of engineering &ay you "ere a civil engineer and you "ere going
to put up a building, you have to make a lot of choices about ho" you're going
to go ahead and put up that building, right# :ou're going to have to decide,
"ell, ho" many stories should it be, "hat should the placement be, blah, blah,
blah, blah, blah $nd in order to make those kinds of decisions, you're going to
have to have a superior understanding of "hat it is that buildings are made of
and "hat are the big issues surrounding the building of buildings $nd so a
simple one is you might have to understand "hat parts of your building you're
going to have to make "ith steel and "hat parts of your building you're going to
have to make "ith aluminum 2o", in order to make that decision, you have to
be very familiar "ith the properties of steel and aluminum If you're not
familiar "ith the properties of steel and aluminum, it's going to be impossible
for you to make a good choice, and so you're going to end up using material
that's too strong or too "eak or too e%pensive or too "hatever it is in
inappropriate places
2o", in the soft"are "orld, this translates to t"o sort of popular phenomenon,
both of "hich net you disastrous performance
The first popular phenomenon//and this is like "hen I say popular, I mean I see
this like once every t"o "eeks//the first popular phenomenon is you build your
service in such a "ay that it depends//no service your library//it depends on a
system or on assembly or some other kind of thing that you fundamentally can't
afford to use This is classic &o, for instance, suppose you have a very strong
start/up time re!uirement such that you have to get your little thing up "ith a
marginal cost of no more than, say, five milliseconds 9ith a five/millisecond
budget for your start/up time, you can rule out all kinds of things that you
might "ant to use +ust by running the 3dir command, okay# :ou can look, say,
3dir, hmm, that one's a Meg *h, guess I can't load that one, okay# ;ecause
there's no "ay you could even soft/fault in that much memory in five
milliseconds 2o", on the other hand, if you're up "ith like 5(, ,))
milliseconds and you're going to be running longer or maybe sort of time isn't
an issue for you, then you have a great deal more fle%ibility $nd the number/
one assembly that people misuse in this category is &ystem<ml People are
.esigning 24T Class Libraries Page ' of ,5
6 '))7 Microsoft Corporation
al"ays "oeful about &ystem<ml People are al"ays "oeful about &ystem<ml
*kay, but &ystem<ml is a perfectly fine assembly "ith a perfectly fine design
point, and it offers a variety of different services that you may find useful and
that, indeed, "e try very hard to make easy to use, but that doesn't mean that
they're appropriate to use in every conte%t If you need to be up in a fe"
hundred microseconds, probably you don't have time to load up &ystem<ml, so
maybe you need another solution for getting your initiali=ation state Maybe
you shouldn't use, say, an <ML initiali=ation file, and I'm picking on the <ML
system again because lots of people pick it up, and that one's fre!uently
abused, not because I'm particularly find "hat they've done distasteful> you
need to understand the design point of "hat it is you're going to pick up
&o phenomenon number one, people take a dependency on a system or service
that they fundamentally can't afford
Phenomenon number t"o is people pick up or use an algorithm that
fundamentally lets them do"n, okay# &o for instance, suppose you need to do
some sorting $nd so you have this comple% mess of stuff that you're going to
have to sort, but you don't "ant to think very hard about ho" to do the sort, so
you choose like a simple/minded sorting algorithm, and it turns out that the sort
that you chose behaves abysmally in the situation that you needed 2o" if you
do a little bit of home"ork, you'll understand "hat the properties are of the
various sorting algorithms, and you can choose one Maybe you need a stable
sort Maybe that means you're going to have to pick heap sort over !uick
sorting, and the top performance of !uick sort is better than the top
performance of heap sort, but you kno", heap sort is stable &o you need to
kno" "hich one is stable *r maybe you "ent really cheap, and decided you
"ere going to pick up something, even less sophisticated than that, and then
you found yourself in really big trouble &o in both cases, okay, you could find
yourself in a situation "here you've taken a dependency you can't afford :ou
could find yourself in a situation "here you're using an algorithm that's letting
you do"n ;oth of these are problems you solve by doing your home"ork
?nderstand the basic properties of the things you intend to use .on't make
e%cuses for having used a techni!ue or relied on a service that then lets you
do"n because it is your responsibility to make sure that your ra" materials are
up to the +ob, okay# It's definitely not the responsibility of the guy building the
ra" materials, although to be fair, "e like to partner "ith those people
sometimes, too, because it could be that "hoever's building that component
"ants to rise to the challenge of your application as "ell $nd so if you can
make the underlying goods better for everyone, that's a good thing, too ;ut
that should be done "ith the plan as "ell
*kay, there are no more rules Rule number one and rule number t"o are
about the only things that I "ill give to you as sort of mandates in the
performance "orld :ou must measure :ou must do your home"ork @ailure
to do those t"o things is a recipe for disaster 4verything else IAm going to tell
you today has a bunch of caveats on it//ifs, buts, and !uid pro !uos :ou +ust
never kno" Performance "ork is plagued "ith po"erful secondary
3antisharing effects that fre!uently dominate the thing that you thought "as
the problem and, resulted in, "ell, surprises &o the best engineers "ill find a
"ay to do the e%periments they need, do the measurements they need in an
efficient manner, and !uickly discard the losing paths, because there'll be plenty
of them, and you kno", by measurement, by prototype, or by "hatever other
kind of techni!ue, "e'll be able to hone in on the "inning techni!ues
.esigning 24T Class Libraries Page 1 of ,5
6 '))7 Microsoft Corporation
comparatively !uickly and not spend a "hole lot of time on the losing
techni!ues &o you should plan for the kinds of risks that you'll encounter "hen
doing performance "ork *nly rule number one and rule number t"o are
mandates
$lright, so given that "e have this "hole sort of measurement culture and
doing our home"ork culture, "hat kind of techni!ues could you use on your
team to improve sort of the overall culture, and get people follo"ing a process
that's likely to lead to substantial successes I have sort of this three/step plan
that I use, and I've, gotten the CLR team to adopt it in many areas, and more
people are getting to adopt it $nd I think it can "ork for a variety of different
teams, so I'll put this forth as kind of a starting point for you to have more perf
culture in your team
2o", the first step is budget 9hatever it is that you're going to build needs to
have a budget 9ell, if "e're building a feature, not having a budget is like
saying, BI "ant this feature at any priceB *kay, "ell, that's a load of bunk I
mean I don't kno" "hat that is, okay, but clearly, "hatever it is that "e're
building, if it takes seven days to boot, probably "e can't afford it &o
some"here bet"een the t"o milliseconds that a customer "ill tolerate and the
seven days that I +ust mentioned is the actual sort of budget that "e have to
build this feature in
2o", ho" do "e get a budget# 9e don't get a budget by looking at the code
;udgeting is not a directly technical e%ercise ;udgeting is an e%ercise "herein
"e get to kno" our customer 9e talk to our customer, and "e say, B9ell, "e
have this ne" feature "e'd like to build, and "e think it "ould offer these and
these different servicesB $nd "e go and "e look and see ho" are people using
our product and ho" "ould that impact their "ork cycle or impact the "ay that
they use our product, and they might tell us, B9ell, yeah, "e like that feature a
"hole lot 9hy, "e'll get all these substantial productivity gainsB $nd that's
great The sad part for us, those of us doing the budget, is rarely do customers
come back to you and say, B:eah, "e're "illing to pay t"o milliseconds and -)
kilobytes for that feature, but no moreB :ou kno", I don't get a "hole lot of
that I get, B9ell, yeah, that sounds good, but "e really need start/up time to
still be goodB $nd they'll say !ualitative things like that
.uring the budgeting phase, "e have to take our customers' !ualitative
remarks, and "e have to turn them into !uantitative budgets for our feature,
okay, or for our "hole release maybe because "e may say, B9ell, "e have this
series of productivity features that "e're planning to add, okay, and they're
going to impact start/upB ;ut our customers still need it to be snappy $nd "e
did some usability tests, and "e find that they start feeling like it's sluggish at
around the 1)/millisecond mark *kay, great &o that means "e have '(
milliseconds for our "hole enhancement suite for this release, okay# &o then
"e can say, okay, of the '( milliseconds, ho" are "e going to spend it# :ou
kno", "e have these si% features *kay, "ell, this guy is going to need some
start/up time for this fancy initiali=ation he needs Maybe "e can budget him
,) of that, so '( These other three guys don't need very much 9e'll give
them four milliseconds each $nd "hat have "e got left# 9ell, maybe "e'll use
that for a cushion in case something comes in in beta ;ut at the end of your
planning cycle, you "ill have taken "hatever !ualitative feedback you had and
"hatever understanding you had of the customer and turned that into
!uantitative resource constraints, sometimes on time, sometimes on memory
.esigning 24T Class Libraries Page 7 of ,5
6 '))7 Microsoft Corporation
because it might be "orking set :ou kno", ho" big a bo% do these customers
have, right# $t "hat point are "e going to start s"apping# &ometimes they're
on "orking sets &ometimes they're on private pages &ometimes they're on
throughput &ometimes they're on "hatever it is that's important to your
customer Remember, "e "ere thinking about "hat "e have to measure $nd
those metrics that "e chose for our product, right, should tie very tightly back
to the budgets that "e're then considering during our performance planning
*kay, so step number one, "e did this budgeting e%ercise 9e haven't even
looked at the code yet, right#
&tep number t"o, "e have to plan, and "hat does that mean# 9e have to be
in a position "here "e can say, BCere is a "ay that I can deliver this feature//I
have a design for this feature//and it is substantially likely to be "ithin the
budgetB *kay, and notice the "ords that I chose//substantially likely $
!uestion that I often get asked is, B9ell, Rico, ho" do I kno" ho" much
planning to do# I mean I could plan and prototype and +ust keep planning and
planning and planning and never get any actual "ork done I mean at some
point, "e've got to actually code the thing &urely you don't "ant the plan to
be so comprehensive that it's more comple% than the code "asB $nd, "ell, of
course, I don't &o the planning phase is about managing your risk &o "e
have to say B9hat is it that "e intend to do# Co" much "ork is that# Co"
much planning should I do given the "ork that's at stake#B If I have a t"o/
man/year kind of effort to get this feature in, probably it's "orth it to spend a
fe" "eeks, doing design documents and maybe making some spreadsheets to
model the performance, and getting a feel for "hat are the pivot points or "hat
are the important metrics, and "hat is going to be performance/sensitive and
"hat isn't, and getting a very good understanding 9e do all of those things to
manage the risk 2o", at some point, "e're going to start feeling BI've planned
this pretty much to death I'm really very confident that I can e%ecute on this
plan at this pointB Co" confident do you have to be# 9ell, if it's a t"o/man/
year effort, pretty confident If it's, on the other hand, a t"o/day effort, if it
doesn't go so "ell, it's not the end of the "orld &o the risk is going to depend
on the nature of the product, the nature of the deliverables, the nature of the
customers $nd at Microsoft, sometimes "e take pretty aggressive bets $nd
other times, "e're like kind of shy, all right# $nd it's all going to depend on the
conte%t and the importance and the inter/group dependencies and other kinds
of things :ou may find that you can get good guidance as to ho" much risk
you should be taking from your management or your partners or your
customers ;ut taking some bets is a good thing
$ll right 2o", remember, "e "ere taking a bet &o at the planning phase, "e
had a plan that "as substantially likely to succeed, and "e had an
understanding "hat the risks "ere, "hat our performance metrics should be
looking like
&tep three is verify, right# Remember, "e "ere taking a risk Could go "rong
It's very important if it does go "rong that "e not inflict it on the "orld, right#
9e "ent through the budgeting e%ercise for a reason 9e "anted to
understand "hat it is that our customers "ould tolerate, "here is it a good
value# $t some point, your customer "ould rather have the speed, right, or the
space or "hatever it is &o if you're adding seven days to start up, they're
going to think, B:ou kno", maybe I didn't need that smart coloring !uite so
much I don't think it's "orth that seven/day start/up,B right# &o if you're
.esigning 24T Class Libraries Page ( of ,5
6 '))7 Microsoft Corporation
"ithin your budget, you're great If you're not "ithin your budget, it's probably
time to cut bait or maybe go back and recalibrate, or, maybe you can "hittle
a"ay the feature and still deliver something that's of value ;ut at that point,
you'll have to recalibrate 2o", hopefully, you're not in a situation "here the
only verification you intend to do is at the very end, okay# There should be
checkpoints, right# I mean it should've been clear fairly early on that things
"ere not going terribly "ell $nd only rarely "ill you find yourself at the very
end and say, B*h, no, no" "e're at the end, and the very last thing didn't "ork
out, and no" I've got to thro" the "hole thing a"ayB That "ould be a very
bad situation to be in 2ot a good "ay to get a big bonus
&o thinking about your plan, your budgets, and your verification steps helps you
throughout your entire lifecycle because that plan is going to tell your testers
"hat they need to be looking for It's going to help you on the scheduling
phase It's even going to help you after you're out in production 9hat
performance counter should you have# 9hat things should you be including in
your debug logs# If your customer's having problems in the field, "hat
!uestions should you be asking him# Maybe you could say, BCey, "hat's your
start/up time looking like# :ou're supposed to be in the ()/millisecond =one
*h, you're at 5( milliseconds *h, something very bad is going on 9hich one
of our assumptions must have gone "rong, if you're at 5( milliseconds# That's
out of our design pointB &o it can help you every"here from a test plan to an
operations manual and for feedback in the ne%t version of your product
because, of course, you'll "ant to look at those constraints and see, no", ne%t
generation of hard"are, ne" set of features, "hat's a good trade/off, let's look
at our old dimensions Milk the plan, okay, because you're going to spend a lot
of time looking at it .on't +ust do it and think about it and thro" it a"ay
&ometimes all the planning "e need to do is a fe" numbers on the back of a
napkin, because it's that obvious that it's going to be easy and the re!uirements
are very easy to meet @or some internal tools, they're one/offs, "hatever good
enough 9e need to be able to go through our files a couple times in a batch
Cas to run overnight 4asy stuff, all right# &o don't over plan, because that's
bad, too ;ut other times, "e're going to "ant to take lots and lots of very
careful steps
&o three steps//budget, plan, verify
&ome things that you "ill see in your organi=ation if you're doing a good +ob
*kay, these are sort of healthy symptoms, if you "ill If "e look at some of
these things, you'll say, BIf I am having good performance practices, I should be
seeing that I have "ell/defined goals and I should have benchmarks that IAm
running on a regular basis I should have maybe gauntlets that are protecting
my check/ins so that on every check/in I have that certain set of minimal sort of
0$Rs that I have to meet, and maybe those can evolve over time &o I should
be seeing things like that If I'm not seeing things like that, maybe I should be
getting a little bit "orried because my perf could get out of control "ith any
check/in The thing about performance "ork is//"ell, soft"are's like any other
comple% system 4ntropy tends to dominate it, okay# &o unless you're taking
regular sort of controlled steps, you're going to find that +ust by entropy, things
are going to get "orse over time People make check/ins, and they're not all
thinking about performance $nd so someone checks in a thing and they're not
actively thinking of performance, the statistics are it's more likely to hurt you
than help you, and that tends to compound $nd so by entropy, things "ill tend
to +ust sort of decay &o it's very important to be tracking that and making sure
.esigning 24T Class Libraries Page - of ,5
6 '))7 Microsoft Corporation
that you have a process to help you to identify "here those things are
happening
2o", the tighter that//the bigger your team is, the more people you'll have
"orking and the greater the entropy is, and so the more important it "ill be to
be able to get right back do"n to the source of that one check/in that caused a
big problem $nd "e "ill find it in a sea of noise If you have a comparatively
small team, it might be ade!uate for you to run your performance benchmarks
maybe once a "eek, maybe once every fe" days because the amount of "ork
that happens in the course of a "eek isn't so bad that you that it "ould be very
difficult for you to go back and find out "hat your problems "ere after one
"eek of "orking ;ut on the other hand, on the CLR, "e have, "hat, 5)/odd
people checking in things If "e "eren't "atching the performance on every
check/in, "e'd go mad at the end of a "eek Co" many check/ins is that, a fe"
hundred check/ins# :ou kno", B*h, there's been a regression Dood luck
finding itB
&o you need processes that are going to help you control that entropy There
should be unit testing that developers are doing before they check in $nd that
could be part and parcel to 00Ts or not $nd that should involve performance
2o", remember, "e had a plan $nd "e kne" "hat our metrics "ere, and "e
should be measuring those things to make sure that "e're delivering on those
metrics and that "e're on track "ith our checkpoints If all that culture is
follo"ing from your scheduling practices to your planning practices and your
feature delivering practices, you'll be milking that plan, and it "ill be very clear
to your developers "hat they're supposed to be doing at "hat step, and it "ill,
again, be clear to the testers "hat they're supposed to do because if "e're
supposed to be at checkpoint < and the performance is supposed to be at a
certain level, they'll kno" "hat metrics they're supposed to be at, and they
"on't be guessing as to ho" to test the perf, right, because everyone "ill agree
*ne last thing especially "hen "e're doing performance improvements, it's
fre!uently the case that "e can't get there all in one bite, nor "ould "e "ant to
because of the risk that's getting there all in one bite &o, for instance, on the
CLR, "e "anted to go do"n from around 7) percent of the pages in an
assembly being private//and I'll talk a little bit more about private pages later//
but "e "ere up about 7) percent private pages 9e "anted to get that number
do"n 9e "ant to go do"n as lo" as five percent Dreat &o that's great, 7)
do"n to five ;ut can "e do that in one milestone# Probably not That "ould
be some pretty insane "ork happening there &o "e "ant to have both goals in
mind @or any given milestone, "e're saying, okay, "ell, this time "e're going
from 7) do"n to 1(, 1( do"n to '(, '( do"n to ,(, so "e have staged goals
for particular milestones that are very achievable and people can understand
"hat they're supposed to do and can feel good about those successes ;ut "e
also keep that long/term goal in mind because "hen people are thinking about
adding ne", "e "ant them to be "eighing against the long/term goal of five
percent @ive percent private pages could be like t"o pages out of the total
T"o might be that five percent $nd so if someone's thinking of doing
something to add one "e "ant them to be thinking, B*h, it's supposed to be
t"o "hen "e're at five percent I'm going to add one That "ould make it
three That's really badB 9hereas if "e "ere up, if he "as thinking about
adding one "hile "e "ere still up high at 7) percent, it might not feel so bad, I
mean I didn't do the math in my head, but it "ould be a lot more than t"o
It'd be more like ,- $nd so he's adding one to ,-, so ,- plus one e!uals ,5
.esigning 24T Class Libraries Page 5 of ,5
6 '))7 Microsoft Corporation
doesn't feel nearly so bad as t"o plus one e!uals three *kay, so it's important
to have both that short/term focus and the long/term focus in your goal setting
$ll of these things are applicable//I mean I talk about them in the conte%t of the
CLR, but all of these things are applicable to +ust about any kind of managed
code development that you might encounter
I guess the ne%t point that I "ant to make is that "e have to strive for
simplicity in our designs, and I think this//especially in managed code, people
tend to go cra=y "hen they pick up managed code $nd they think, B*h,
managed code, it's so easy I can add all these features Look at this *h, this
is so much funB $nd they're having fun and their class supports this method
and that method and the other method, and it's polymorphic, and it has
subtypes and supports '' interfaces $nd they +ust go completely nutso $nd I
guess it's kind of cool that you can go nutso and your head doesn't e%plode
doing the development, but at the end of that cycle, you end up "ith this thing
that's so general purpose that it could everything I mean it could do things
that you never even dreamed that it could do $nd this is not inherently a good
thing for several reasons The first reason is your poor customer picking up this
class that you deliver is going to look at it, and he's going to feel like he's
picked up a &"iss army knife that has like about E)) blades on it Ce's like, BI
+ust needed a scre"driver 9here is it#B $nd he's opening it up, B2o, pliers,B
you kno", and big, giant things are springing out, right# I mean you could
imagine this &"iss army knife "here he keeps opening up blade after blade
after blade looking desperately for the knife, and like, B2o scre"driver, no
hammer, nope "hat is this# I don't even kno" "hat that is Put that back inB
*kay# 2o", "e have things to help manage this I mean our I.4 offers
Intelli&ense, thank goodness, okay# $nd so "e can help you sort through that
scre"driver or that &"iss army knife "ith the ()) blades or E)) blades or
"hatever it is ;ut fundamentally, if "e'd made a much smaller &"iss army
knife "ith +ust the blades "e needed for our particular application, the overall
utility to your customer "ould be a lot higher $nd then people say funny stuff
Like they say, B9ell, I "anted my class to be very reusable, Rico, you kno"#
$nd so there "ere all these other cases it needed to handle, so it "as "ell/
rounded and reusableB $nd I look at him and I say, BReusable# :ou "eren't
even usable, much less reusable I mean no one "ould ever "ant to pick up
this thingB &o you first have to be usable Then you can be reusable &o
speciali=ing or understanding at least "hat your core competencies should be
and making sure those are very easy to find, and then making sure that those
secondary things aren't completely contaminating the primary things, both in
terms of comple%ity to your user and in terms of performance because you
kno" "hen you add those E)) other features and you make the thing all "ell/
rounded in general, you're probably going to get an internal structure that's a
"hole heck of a lot more complicated than it other"ise needed to be I mean a
really popular one is people decide that they have to add synchroni=ation to
their class, so it has a mode "here it's lockable or not lockable $nd they say,
B9ell, you can have all the updates be atomicB $nd, l, that could be useful, but
it comes at !uite a price $ll the people "ho are going to use your class are
going to have to choose "hether or not they're taking the synchroni=ed version
or the not synchroni=ed version Maybe a "hole bunch of methods that
other"ise didn't need to be virtual are going to be virtual no", so there are
going to be subtypes, so that's going to prevent inlining, and so there's all this
collateral damage +ust in case someone on the off chance that they needed a
synchroni=ed version could go your costs directly $nd then the alternative "as
.esigning 24T Class Libraries Page E of ,5
6 '))7 Microsoft Corporation
he could synchroni=e it himself "ith a locking primitive, and maybe he's even in
a better position to synchroni=e it himself because he kno"s "here the
atomicity needs to be, and you didn't :ou're +ust guessing &o I don't kno"
That's +ust a classic thing that people do
Feep in mind that every time you add a ne" feature, there's collateral damage
associated "ith adding that feature, both in terms of comple%ity and in terms of
performance costs $nd the simpler design is sort of inherently better
9henever you add ne" features, they start at a net penalty, and they have to
+ustify themselves both in terms of value to the customer and in terms of
performance in some cases
Doing back to the planning phase, measuring, doing your home"ork, if you
focus on "hat your customer needs, be customer driven and put your emphasis
"here your customer puts it, you'll tend to naturally do the right thing because
you "on't "ant to spend a "hole lot of time adding Bgee "hi=B features, and
you "on't "ant to spend perf on the Bgee "hi=B features :ou'll "ant to stick to
your core competencies, and that's a good thing
Cere's another little one Many people !uote me this line, and most people
don't even really kno" the conte%t in "hich it "as given ;ut "ho's heard the
phrase, BPremature optimi=ation is the root of all evilB# Raise your hands if
you've heard that phrase 9ell, a good number of people but not everyone It
"as made popular by .on Fnuth, although he "as !uoting Tony Coare Tony
Coare, by the "ay, is the investor of Guicksort *kay, so premature
optimi=ation is the root of all evil It's hard to stand here and say that
optimi=ing prematurely is a good thing 9hat I should get ,) milligrams of
premature optimi=ation daily# I don't even kno" *bviously, premature
anything is kind of bad It's hard to begin "ith the "ord BprematureB that's a
positive thing Maybe lottery "innings, premature lottery "innings I "as
trying of something, and it's very hard to think of anything
Premature is bad, but people use this phrase, BPremature optimi=ation is the
root of all evil,B as an e%cuse not to think about performance at all in their early
planning &o let me go back to an e%ample from before &ay I "anted to code
a sort, and so IAm a guy, and I've got the premature optimi=ation is the root of
all evil religion, and so I say, B*h, no, I'm not going to invest in my sorting
algorithm +ust no" That "ould be premature 9hat I'm going to do is code it
up the easy "ay first and then make it fast later,B right# *kay, that's the
religion &o they "rite the easiest sort that they can think of//bubble sort
Dreat Dot that bubble sort in there It's nice and easy Then later they find
out it's too slo" ;ig surprise there &tunning development//;ubble&ort about
the "orst sort that isn't completely stupid, okay, +ust "asn't up to snuff &o
they've got to replace it &o they go//because they're obviously a fan of Tony
Coare's because they're !uoting him//they go and they pick up Guicksort 2o",
let me ask you, if you "ere in a position "here you needed to substitute the
;ubble&ort algorithm//and I'm using ;ubble&ort as kind of the pro%y for the
problem here//If you "ere substituting your ;ubble&ort "ith a Guicksort, ho"
much learning did you get out of coding up the ;ubble&ort# $re you going to
+ust tune it up# $re you going to, B*h, I'll +ust tune it up right all day, and I'll
turn it into a GuicksortB# :ou're not going to tune it up :ou're going to thro"
it out, right# I mean there's not a darn thing in common bet"een those t"o
things e%cept for the greater/than test The ;ubble&ort is gone and pitched out
the "indo" and you're basically "riting a "hole ne" sort from scratch, and you
.esigning 24T Class Libraries Page H of ,5
6 '))7 Microsoft Corporation
spent a "hole lot of time maybe, living "ith your ;ubble&ort and planning
around it or not thinking about "hat you need to be doing, and no" at the end,
you find yourself having to discover the Guicksort 2o", if you're lucky enough
that you can reach off the shelf and have Mr Coare save you, okay, that "ould
be a good situation to be in ;ut a lot of times, there is no off/the/shelf that
you're going to plug in :our design is fundamentally fla"ed, and you at the
last minute are going to have to go back to the dra"ing board and figure out
ho" the heck you're going to do something that's plug/compatible "ith the
Beasiest "ayB that is going to fit into your overall system :ou can easily find
yourself doomed
&o Tony Coare "asn't talking about making sure that your design is sound and
that you've chosen the right algorithm "hen he said, BPremature optimi=ation is
the root of all evilB Ce "as talking about don't go "ithout even looking at a
profiler, start to hand/tune the assembly language that's associated "ith your
sorting routine That's completely ludicrous ;ut do make sure that you have
the right algorithm in place and that you have a plan in place that lets you be
substantially likely to succeed against your performance goals That's
performance planning
Remember, "hen "e "ere doing all this planning, "e had to be !uantitative If
"e "eren't being !uantitative, "e "eren't being engineers It's not a case that
"e're being premature by insisting on some numbers 9e're failing to be
engineers "hen "e fail to ask for !uantitative metrics that help us make
choices 9e should be able to !uantify the risks that "e're taking, and "e
should be able to !uantify the performance characteristics of "hat it is that "e
intend to build If "e're not doing that, again, "e're on this, B*h, I "ant this
feature at any priceB philosophy, and that's a losing strategy .on't let a nifty
slogan like, BPremature optimi=ation is the root of all evil,B stand bet"een you
and greatness
I think I've given you probably all the performance culture you can stand at this
point The ne%t fe" slides are a little bit more technical and "ill be talking
about some "ays that you can maybe get more mileage out of modern
processors and ho" that sort of relates to managed code a little bit and some
other kinds of things you might think about and also some planning e%ercises
that you might "ant to go through and "hat approaches you might "ant to
take ;ut in terms of the dogma, I guess IAm done
&o my ne%t slide is about locality 2o", many people don't think about locality,
and//"ell, raise your hand if you even kno" "hat I mean "hen I say locality
*kay, "ell, that's not very many people That's like maybe ') percent of the
audience &o locality refers to ho" close together things are &o if I have a
bunch of ob+ects and I allocated them or I made a data structure and I've
allocated over time, if they "ere all allocated pretty close together in time and I
"as careful about ho" I made my data structures, all the pieces parts "ill tend
to be close together in memory, and that's a good situation on a modern
processor In fact, in addition to rule number one and rule number t"o, about
the only thing that I'm really "illing to predict is that on a going/for"ard basis,
microprocessors "ill get faster at a faster rate than memory "ill get faster $nd
so "hat that means is that having your things be close together and be readily
cacheable and so forth "ill tend to become more important over time rather
than less important I "ant to have data structures that are fairly
straightfor"ard that don't have lots of pointers flying all over the place, and
.esigning 24T Class Libraries Page ,) of ,5
6 '))7 Microsoft Corporation
there's many reasons for this *ne of the reasons is, of course, that I "ant to
have good performance $nd another reason is that I "ant to have fairly simple
data structures that my mom could understand//or maybe not my mom but at
least 32ord any"ay
*kay, so think about your memory as being divided into pieces parts, very
much like the "ay you think your disk is 9hen you think about ho" to lay
things out on disk, you think, B*h, "ell, there's sectors, and I have to go, and I
"ant the related sectors to be together, and I "ant to have not too many seeks
and so forthB &o let me sho" you a couple of pictures *kay, so here's an
e%ample of a data structure that's maybe less than good $nd the funny thing
is people dra" these pointers, and they don't really think about "hat it is that
they're dra"ing Like, for instance, look at that pointer bet"een $ and ; at the
very top there &ee ho" that's a short pointer# It's +ust little &ee, it's +ust a
teeny, tiny, cute little pointer 9ell, it can't possibly be very far from $ to ;
"ith such a short line bet"een the t"o They must be practically ne%t to one
another ;ut compared to $ and C no", see, there's a long pointer there
bet"een $ and C, long arro" &ee, so that one, that's a long pointer, so C must
be pretty far from $, so that's ho" you read this diagram, right# The short lines
mean that the pointer is nice and close, right, and the long lines//no, that isn't
true at all, is it, right# 4very pointer could be a total disaster and, in fact,
probably is unless you take pains to make sure that that doesn't happen 2o",
one of the things you can do in managed code//and it's fairly straightfor"ard in
managed code actually//to improve your locality, one of the things you can do
is make sure you allocate as many of these things as possible at the same time
If you allocate things close together in time in the garbage/collected "orld, they
tend to also be close together in space $nd furthermore, as your program
runs, things that "ere allocated fairly close together "ill tend to get s!uee=ed
closer together because the free ob+ects that might've been//the ob+ects that
might've been bet"een them "ill tend to die, and things get s!uee=ed together
because our allocator compacts over time &o the situation "ill never be "orse
than it "as "hen you started and might actually get better if you allocate things
together 2o", if you're using unmanaged systems, I can assure you the
situation "ill never be any better than it "as "hen you started because none of
these ob+ects are ever going to move, because you get the address, and you've
got the address, and that's the address forever $nd it could be arbitrarily bad
because you don't kno" I mean if ; is slightly bigger than C, then you
might've got a different heap for ; than C because there "as a free block of the
right si=e over there, but C "as over here, and then, oh, $ "as nice and small,
so it "as up there $nd so "ho the heck kno"s "here any of these guys are#
:ou might have to go through all manner of gymnastics to arrange for these
guys to be allocated together That's "hy people do "acky things like custom
heaps and "hatever else in the unmanaged "orld
In the managed "orld, forget all that The main thing you have to remember to
get good locality is to say, B9hatever I'm going to use together, I try to allocate
together, and I'll get pretty good behaviorB 2o", I have a little "ay of scoring
these diagrams I'm going to illustrate 2o", you can all practice it along "ith
me Cave your developer//or if it's you//dra" the diagram on the board, like
this one $nd every time you dra" an arro", do that &o $, ;, C, ., 4, @, o"
&ee# $nd then you'll be able to tell ho" good your data structure is by ho"
much your hand hurts, so if your hand doesn't hurt very much, you're in pretty
good shape
.esigning 24T Class Libraries Page ,, of ,5
6 '))7 Microsoft Corporation
2o", I've seen these things go across multiple "hiteboards, +ust pages and
pages of stuff, and I think, "o", I mean "hat feature are "e building here for
this spaghetti structure# It's pretty a"ful $nd, remember, if you're building a
library, you especially "ant your structures to be simple because some poor guy
has got to understand them other than you, and probably they'll be e%posed
&o less of this, more of this &ee that nice, big, fat, thick arro"# &ee# That's a
pointer that is carrying lots of information because it's pointing bet"een those
t"o nice big blocks, and so I have blocks that "ere allocated in the middle of
arrays, and I have nice guaranteed continuity bet"een my @s and $, ;, Cs and
.s
2o", okay, the truth is liable to be some"here in bet"een, and IAm not saying
never use pointers I mean, obviously, it turns out pointers are kind of handy
.on't ever use pointers, right# Iust remember rule number one and rule
number t"o Think about "hat you're doing Think about the simplicity that
you might gain by consolidating some of this stuff, and don't be gratuitously be
allocating and adding things that are for special cases :ou have to be sure that
"hen you're adding a ne" feature that it's paying its "ay It has to be a good
deal to add that ne" feature
If "e al"ays said no to adding ne" features +ust because it "as going to slo"
things do"n, "e'd never add anything, right# &o "e are "illing to slo" things
do"n or make things fatter or bigger or "hatever else, but "e do "ant to be
giving our customers a good deal Makes them feel like they're getting value
for the space or the speed or "hatever it is that they're trading off
Cere are a fe" e%amples of these kinds of things that I ran into in my travels
through managed code in the "orld
The first one "as in the $valon code base, and I "as looking at that !uite a
"hile ago $nd here "e had an e%ample of someone using a general/purpose
enumerator structure in the middle of their message dispatch 2o", people
probably kno" the foreach pattern in enumerator, but have you stopped to
think, if I have a four/each "ithin a four/each, the inner four/each is going to
allocate in enumerator on every pass through the outer one# &o there "as a
case "here a poor little guy, and he "as a perfectly smart guy, but he +ust
temporarily lost his mind It's okay Cappens to the best of us *kay,
temporarily lost his mind, did a four/each inside of a four/each inside of the
message dispatch loop This loop "as responsible for one/third of all the
allocations in the $valon scenario in !uestion $nd "e "ere talking about
iterating an array list in this case, and it had three elements in it It "as really
kind of pathetic &o the "hole thing could trivially be replaced by a four/loop
and have no allocations at all
&o "hat's important# :ou have to be looking at ho" you're using the memory
If you "ere measuring, you'd see this instantly :ou'd go, BLook *h, holy
mackerel 9hat am I doing there# Look at all these enumerators *h, I see
"hat's happening I've nested four/eaches, you kno" I don't need to do that
here That enumerator's not paying its "ay, so let me get rid of thatB
*kay, other cases "here there's multiple levels//I think I've talked about this
several times//multiple levels of in/directions to handle e%tensibility cases that
are +ust e%traordinarily ta%ing &ometimes it's important e%tensibility cases, but
"e have to make sure that they don't penali=e the main path &o, for instance,
.esigning 24T Class Libraries Page ,' of ,5
6 '))7 Microsoft Corporation
sometimes the accessibility path can be//it's important "e have to get that
right It has to be in there &o "e don't not do accessibility paths, but "e do
say, B*kay, "ell, I have to make sure that the accessibility path is something I
only pay for "hen IAm using the accessibility featuresB $t "hich point it is
definitely earning its keep That's a good deal The guy "ho's using those
accessibility features, boy, is he glad they're there $nd "e "ant to be there for
that guy ;ut "e don't "ant it to be the case that "e're paying the cost of the
accessibility path on all the normal, easy paths that don't need that e%tra "ork
$nd that's true of an accessibility's an easy one to pick on maybe, but that's
true of any kind of e%otic feature &ometimes "e things that tick in they're
operating system/specific, they're user/specific Maybe the user likes his
customi=ation feature That's fine, but in the default case, "e don't "ant to be
incurring a lot of cost &o keep in mind that your features should be very much
pay for play
$nother kind of thing that happens//people lose their mind in kind of "acky
"ays 9hen you're "riting a sorting function//or a comparison function that's
going to be used in the conte%t of a sort, you really don't "ant to be allocating
memory in there &o, for instance, if you "ant to take a bunch of strings and
sort them by the characters that occur after the first comma# *ne "ay you
could do that is to call stringsplit and get the field that happens after the first
comma, the comma separated, okay# 2o", if you did that, though, turns out
stringsplit returns an array, right# &o "here do "e think this array came from#
Co" much home"ork do "e have to do here to determine "here that array
came from# Pretty much it'd have to be allocated on the heap, right# $nd then
all the members of the array, "ell, they're strings, too, right# &o "here do you
think they came from# Ceap, right# &o no" should anybody be surprised that
stringsplit did some 3allocs# Probably not, right# *kay &o if you are
surprised, it'd be helpful to look at a trace of the run, and that "ill unsurprised
you ;ut if you're "riting a comparison function or a hashing function, you
think to yourself, BI'm calling this function, and I kno" it's going to do 3allocs,
"o", can I afford to do this#B I mean it seems like, oh, it's +ust a little
comparison function &tringsplit//oh, that's pretty efficient ;ut comparison
functions, they get called 3end log, 3end times in the conte%t of sorting, right#
&o "e can't afford any 3allocs 2o", it turns out that the old/fashioned "ay of
doing string comparison "ith the strings in place, like in CJJ you'd do 3storchar
or something like that to find the first comma, and then you could do 3stercom
or 3sterncomp to compare the appropriate numbers of characters :ou can still
do that in managed code It still "orks &tringcompare, it's still there :ou
kno", inde%//it's still there &till "orks That "hole tried/and/true, been
"orking since the late '()s, still do that $nd you kno" "hat# It uses no
memory, and it's fast as heck *kay# &o I totally recommend no memory and
fast as heck :ou kno", I can't emphasi=e enough ho"//like if you got that free
and cheap and easy and, and the split isn't even particularly easier to read than
some of the other stuff &o if you're "riting a hashing function, okay//and,
actually, a good tip, if you're "ondering "hether or not you're "riting a hashing
function, you need only look at the name If it includes the "ord BhashB in the
name, probably you're "riting a hashing function &o usually it's called hash
something &o if you're "riting a hashing function, probably a bad idea to
allocate any memory at all :elling at the garbage collector for being slo" given
that you allocated during a hashing function, not going to get you a "hole lot of
sympathy &imilarly, if you're "riting a function that includes the "ord
BcompareB in its name, good chance that you're "riting a comparison function,
and probably it "ill be called in the conte%t of sorting or other searching, and it,
.esigning 24T Class Libraries Page ,1 of ,5
6 '))7 Microsoft Corporation
too, needs to be e%traordinarily frugal &o be double careful in those cases
0ery smart guys that I deeply respect +ust//they lose their mind temporarily I
don't kno" "hat it is It's +ust//and they do this $nd then they don't measure,
and after"ards, they'd make that Comer &impson sound, you kno"#
Think about your data structures Think about their shape Think about
"hether they need to be gro"able or e%tensible in some "acky kind of "ay, and
pay for "hat you need to pay for $gain, comes back to doing your home"ork
and measuring :ou "ill make mistakes That's okay 9e all make mistakes,
you kno"# 2otice I never mention the mistakes I made during this little talk#
*kay, "ell, that's because there's too many, and I don't "ant to bore you all,
and I only have an hour to talk ;ut I guess the trick to being a successful
performance person is kind of the same as the trick to successful
photographers, right# :ou kno", successful photography is about taking ())
pictures and then sho"ing your friends three, right# $nd you say, B*h, look at
the pictures I took,B and you sho" them three, and he'd say, B9o", "hat a
great photographer you areKB $nd you're like, B:eah, IAm not sho"ing you the
7H5 bad ones,B you kno" &o that's okay $ll right# $nd perf "ork is
substantially like that, too 9hen you're looking, you're going to have losers
4%pect it Make sure you're ready to 3cull those, and don't be too ashamed of
them ;ut if you're there doing your home"ork, that "ill help you to make
better decisions more often Measuring your results regularly, you'll be in a
great position to cash in on the "ins and thro" a"ay the losers
I've got three more slides They're kind of dense, and mostly they're here I
put in all these "ords so that you "ould have them to refer to "hen you looked
at the handout later I'm not even going to talk to all of this because "e don't
have time for all that ;ut the ne%t three slides, I'm going to talk about some
things that you might be targeting &ome common things that people might
"ant to be doing in terms of improving their performance or planning for their
performance, and ho" you might go about doing that
2o", the first one is "orking set 2o", let me do another audience survey
Co" many people kno" "hat "orking set is# Raise your hand if you kno" "hat
"orking set is *kay, great *h, that's a lot more people than I e%pected to
raise their hand, about like 5) percent of the people 9ith reducing your
"orking set refers to the amount of memory, of course, that the process//that
the operating system, rather, has committed for use by your process, okay, to
get your +ob done &o, obviously, if you can use less memory, that's a good
thing because it frees more memory for use by other applications $nd as I'm
fond of saying, +ust because the bo% doesn't//has t"o gigabytes of memory in it
doesn't mean you get to use it all, because the user probably has other things
he "ants to do, too &o you should be al"ays thinking about being frugal
&o I'm trying to plan for the "orking set of some ne" feature that I intend to
deliver *kay, great &o ho" do I do that# 9ell, it turns out "orking set has
t"o big drivers, and they are, surprisingly enough, code and data $nd there's,
a variable mi% there To get a handle on the code that's going to be added, you
need to think about, "ell, ho" much code am I going to be "riting to support
this ne" feature# $nd fre!uently, that's something that you can estimate,
right# :ou can say, B9ell, ho" much code is it compared to other kinds of
things# In this pro+ect, "hat's the sort of bytes per line of code, typical ratio#B
&o I can do a variety of fairly straightfor"ard pen/and/paper e%ercises that "ill
help me to get a handle on ho" much code I intend to add Then I have to
.esigning 24T Class Libraries Page ,7 of ,5
6 '))7 Microsoft Corporation
think about, so that's the total code I'm going to run, right# Co" much of that
is running, and "here is it running# ;ecause "hen I'm measuring "orking set,
usually IAm not thinking about sort of universal "orking set I'm thinking about
in the conte%t of scenarios, like start/up or like pro+ect build or "hatever the
heck it is &o then you have to think, okay, "hat is the marginal impact of this
code that IAm "riting on start/up or on the build# Is most of the code not active
during that time, or did I have to sprinkle a lot of ne" sort of tests in the
e%isting code that are going to get dragged in kicking and screaming# &o again,
there might be a part of the cost that's not pay/for/play, so I pay the costs
"hether I use the feature or not There might be costs that are more pay/for/
play ;ut in any case, I can go right to the si=e of the code and say, B*kay,
here's kind of "hat my contribution is likely to be,B and that "ill give me good
guidance as to ho" my "orking set might be affected and from that, I can
make a plan that's substantially likely to hit the budget, "here I can say,
BCere's ho" much code IAm going to add :ou can see it's a total of//it's going
to be appro%imately ')) lines, so that turns into ')F of code, so and so many
pages, and so "e can afford that according to our budgetB That's a good
situation to be in &o once I have an idea "hat my budget is, then the ne%t
thing I can do is think about, "ell, "hat unit tests should I be putting in place to
gate those, and you kno", ho" should I trend that, and do I "ant to//you
kno", do I "ant to plug into some benchmarks that I already have or not, or
"hat have you# ;ut I can play a lot of games "ith that
*kay, so "orking set planning basically based on code si=e and then
symmetrically on data si=e $gain, "hen I do the data si=e, I can look at it in
t"o "ays, right# 9hat collateral damage am I going to have to do to the
e%isting data structures to fit in my ne" feature, and "hat ne" data is
completely uni!ue to my ne" feature, and "hat does that look like# $nd I
should be able to add up all those bytes, right# It's not so hard I can do
prototypes if necessary, but I should be able to get a very good handle on ho"
much data IAm going to have to add to support my ne" feature $nd at the end
of those t"o e%ercises, I'll have a pretty good idea of "hat my "orking set
impact is going to be
*kay, the second biggie is private pages &o ho" many people//people kne"
"hat "orking set "as Raise your hand if you kno" private page is *kay, less
people *kay, more like ,) percent of the people "hat a private page is *kay,
there are t"o kinds of memory//the kind of memory that can be shared "ith
other processes and the kind of memory that can't $nd actually, it doesn't
matter so much if it can be shared, but actually if it is shared because
sometimes "e have pages that are theoretically sharable but that "on't be
shared because there's only one instance of your program running &o the
shared pages tend to be things like the code If there are three, four, five
copies of your library in memory//you're running a library//all of the five copies
"ill share the very same physical memory to hold the code to the e%tent that
"e don't dirty it $nd so "e do//"ell, "e're trying I "as talking about that
earlier 9e're trying to be very good about not touching those code pages so
that they can be shared 9e like shared pages because shared pages, you kind
of amorti=e the cost over all five copies of your library, right# Like "hen the
second copy of your library starts running, it pays much less than the first copy
because all the shared pages are already resident, so "e don't have to hit the
disk for them 9e get lots of benefits &o "e like shared pages 9e don't like
private pages nearly so much &o the biggest source of private pages in a
managed application is, da da da da, the DC heap That's the one you control
.esigning 24T Class Libraries Page ,( of ,5
6 '))7 Microsoft Corporation
There are other sources of private pages, but they're for my team to s"eat
over, and "e do $nd I really "ould rather you not s"eat over those nearly so
much Think about "hat you're going to be putting on the heap, ho" big is it,
ho" often is it going to get touched $nd of the data structures you could be
building, "hich ones of them are pay/for/play# It's a recurring theme .o I
build those data structures all the time, or do I build them only "hen they're
needed# .id I have to make modifications to e%isting data structures and make
them bigger +ust on the off chance that this ne" feature is being used, or do the
e%isting data structures already handle the e%tension +ust fine, okay# &o to the
e%tent that you are adding ne" features that have very little cost in terms of
data that is associated "ith them "hen they're not in use, you're in very good
shape $nd "hen you have features that have to do a lot of damage to e%isting
structures or re!uire complicated e%tensions or generali=ations to the algorithm,
you're going to find yourself hurting a lot *kay, so here's some rules that talk
about//some tools you might consider using and ho" to measure it and so
forth ;ut, basically, this one is all about the DC heap, okay# &o think about
"hat data you're going to build, and think about the overall bytes, and you can
translate that to private pages $nd that's big goal number t"o
2umber three is start/up time $ lot of people talk about that one, too The
slide's even denser than the others There are t"o big cost drivers for start/up
time, and it turns out that most of the time the code that runs at start/up time
is fairly straightfor"ard code This isn't al"ays true, of course Remember, the
only thing that's al"ays true is measure and do your home"ork, right# ;ut
fre!uently, start/up code is fairly straightfor"ard It doesn't do a lot of
complicated looping, anything, and other kinds of +a== &o the main cost
associated "ith start/up code has to do "ith getting that code loaded $nd so
there are t"o kinds of costs
@or shared pages, or pages that are in the cache, you're going to pay a very
small penalty for loading those in $nd you can think about charging yourself
about one microsecond//it varies, but in round numbers, charge yourself about
a microsecond for every four F of data that you intend to bring in from the
cache $nd I said code, but actually, it's true of any data If you have
initiali=ation files, if you have registry data, if you have//"ell, "herever it is//
"herever it's coming from, if it's in the cache, charge yourself about a
microsecond a pop Think about ho" much computation you could do in a
microsecond, right# *n a modern processor running at a couple Digahert=,
that's several thousand instructions
$ll right, category t"o is the really e%pensive one If it's not in the cache, it's
going to cost you more like on the order of a millisecond for every four F you're
going to drag in $nd you can plan for that as "ell ;ut at four F, at one
millisecond for every four F, you could see ho" you could easily blo" a ,) or
')/millisecond start/up budget "ith very fe" pages $nd don't be fooled It's
not +ust the code, right# $ny IL* &o if you're looking at a config%ml file,
"ahoo, okay, that config%ml file might not be hot, especially if it's specific to
your application &o that means disk faults, and so that's going to eat up a lot
of your budget
&o think about "hat you're going to drag in to the process to get your code
started, and if you do have e%otic algorithms at start/up time, then, of course,
you're going to have to budget for those, too, in terms of e%ecution time ;ut
it's a pretty good appro%imation to start "ith "hat code am I going to drag in
.esigning 24T Class Libraries Page ,- of ,5
6 '))7 Microsoft Corporation
that's hot and "hat code am I going to drag in that's cold, or "hat pages am I
going to drag in that's hot and "hat pages am I going to drag in that's cold,
because, again, it doesn't matter "hether it's the code or "hether it's the data
$ll right &o that's start/up time, and those are kind of the big three $ fourth
one for us on the CLR is the cost per $pp.omain, and that one might be
relevant to you as "ell *n my blog, there's a more detailed version of this
document that talks about that goal as "ell
*kay, so +ust to "rap up, remember that you need to understand your goals If
you don't understand your goals, you can't possibly do a good +ob on them
Think about rule number one and rule number t"o Think about the cost of
everything you do, and facilitate your home"ork ?se performance culture//
performance culture//in doing your +ob ;udget, plan, and verify That's all I
have for you today, and I've got a little bit of time left over for !uestions
I think before I "rap up, though, I'd like to plug some of these links :ou can
find some good material on my blog that's out on M&.2 If you do nothing
else, go and look at M&.MMicrosoftcomLperf That's our performance and
scalability P$D, and especially chapter five is on managed code It's got all
kinds of information, detailed information, on good patterns for using our
classes, from the collection classes to synchroni=ation to +ust practically
everything you ever sa" &o I strongly recommend chapter five of that P$D,
and internally, devdivclrperf has good resources as "ell, including some videos
and other flavors of this talk, too, that have stressed other different points
.esigning 24T Class Libraries Page ,5 of ,5
6 '))7 Microsoft Corporation

You might also like