2

I've got some code that takes a PythonFunction.

The standard way of invoking it seems to be:

engine.Operations.Invoke(myfunction, arg);

However, I've found experimentally that

((dynamic) myfunction)(arg)

Is very approximately twice as fast. I've been trying to figure out how exactly casting to dynamic can expose an invokable interface and where to see the code that runs under those circumstances.

Can someone point me to where in the PythonFunction code the implementation of dynamic invoke is?

Julian Birch
  • 2,605
  • 1
  • 21
  • 36
  • 1
    Without knowing the internals of Operations.Invoke but my guess would be that it is using reflection. The difference in performance might be explained by this answer of Eric Lippert: https://stackoverflow.com/a/4649596 – rene Oct 01 '22 at 12:58
  • The thing is, the model of "It does what it would have done if the type information was there" falls down when you hit Invoke. Because you can't invoke `myfunction` without first casting to `dynamic`. – Julian Birch Oct 01 '22 at 14:37

0 Answers0