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()
|]
@benblo
Copy link

benblo commented Jan 20, 2011

This is extremely interesting! it's injecting code before and after each method, right? at what time does it occur, pre-compilation? when assemblies are loaded?
Could this be written in C#, or does it have to be Boo? will it affect any method in the assembly, or only Boo methods?
Could it be used to modify the built-in Unity API? eg customize the Debug.Log behavior or whatever.
I'd really be super-interested in knowing more about the exact data-flow (or code-flow ;)...

@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