Tools to find boxing in code
Asked Answered
G

2

7

Background: I'm developing for the xbox and am at the optomising stage. I need to cut down on object allocations. One place to start is finding out where (un)boxing occurs.

I'm very new to IL (in that i've never looked at any!) and would like to avoid running DLLs through a dissassembler, hunting for the (un)box command then trying to work out what line of code it relates to.

Question: Are there any tools which will report this kind of thing (where (un)boxing occurs) for me?

EDIT: Made request below into its own question since it's fairly distinct from this one.

Many, many, bonus points for a way of tieing a GC heap dump back to lines of code where the object creations occured!!

Goldi answered 20/6, 2011 at 20:11 Comment(5)
"Many, many, bonus points for a way of tieing a GC heap dump back to lines of code where the object creations occured!!" Then set a bounty, otherwise there are no bonus points.Manned
@Jason, i will, but can't yet because i think the question needs to be around for a day or so.Goldi
Out of curiosity, what has led you to believe that object allocations are your main bottleneck? Memory allocation in the CLR is generally quite cheap and it's optimized for frequent allocation of small, temporary objects.Uredo
@Dan, it's common knowledge that when developing code for the xbox GC can kill performance (Compact framework GC isn't as good as desktop). Since GC occurs every 1MB (on the xbox) the idea is to minimise object allocations during gameplay.Goldi
@MGZero int i=1;object o = (object)i; /* boxing */ i = (int)o; //unboxingGoldi
B
5

This MSDN Magazine article details how to create an FxCop (Code Analysis) rule that detects boxing and unboxing and can present violations as a warning. The article is a little on the old side, but you should be able to adapt it to your needs.

Biplane answered 20/6, 2011 at 20:38 Comment(0)
R
4

There is a tool called BoxCop which does exactly that.

It is not really useful when trying to integrate the checks for boxing/unboxing into the build process. For that you would need some rule for FxCop.

Rick answered 22/11, 2012 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.