Skip to content

Instantly share code, notes, and snippets.

@bamboo
Created January 19, 2011 20:12
Show Gist options
  • Save bamboo/786773 to your computer and use it in GitHub Desktop.
Save bamboo/786773 to your computer and use it in GitHub Desktop.
Wraps every method with Profiler.BeginSample/Profiler.EndSample calls.
import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
class DeepProfilingAttribute(AbstractAstAttribute):
"""
Wraps every method with Profiler.BeginSample/Profiler.EndSample calls.
Save it to the Plugins folder of your Unity project and enable it on a per script basis using:
@script DeepProfiling()
Or for the whole project using:
@assembly DeepProfiling()
"""
override def Apply(node as Node):
node.Accept(SamplingWrapper())
class SamplingWrapper(FastDepthFirstVisitor):
override def OnMethod(node as Method):
node.Body = [|
Profiler.BeginSample($(node.Name))
try:
$(node.Body)
ensure:
Profiler.EndSample()
|]
@bamboo
Copy link
Author

bamboo commented Jan 20, 2011

Hi benblo, it's injecting code before and after each visited method, yes. It happens during compilation, http://is.gd/UgjW8Z for more information. It could be written in any .net language but with boo you get to use quasi-quotation ([| code |]) which makes it much easier to express and understand the code transformations. This technique currently only works with boo and javascript.

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