[C++-sig] optimizing away calls to the python runtime -- was [detecting if a function is written in python or C++]
Mathieu Lacage
Mathieu.Lacage at sophia.inria.fr
Tue Jan 17 09:42:29 CET 2006
On Mon, 2006-01-16 at 19:24 -0500, David Abrahams wrote:
> Your example is much, much bigger than it needs to be. Forget about
> methods; just demonstrate it for regular functions. Stop exporting
done.
> the "Real" class twice. Stop doing needless dynamic allocation in
yes, this is a remnant of some testing I did to see what would happen if
I exported a class/method twice. removed.
> store_and_invoke_callback.
right. It is not needed in this example. removed.
>
> And stop using double underscores in C++ identifiers; that's reserved
> to the compiler ;-)
I thought it was starting and ending underscores. removed.
>
> If I sift out all the stuff in your code that seems extraneous, I
> still can't tell what you're trying to accomplish, but maybe you just
> want to use
>
> boost::function<void()>
>
> inside which you can store any object invokable with zero arguments?
My wrapped c++ code is not going to use boost::function but a similar
template-based thing which will end up generating classes such as
Callback1 in my example. The () operators of these classes will have a
non-zero number of arguments and will have return values (the current
sample code demonstrate no return value but it should be extended to
this case easily).
> P.S. there's little reason to do anything special for bound methods,
> since if
>
> >>> x.f()
>
> is legal Python, you can always store x.f and invoke it later.
Yes and if you remove the python statements from the try in the
make_callback python function, this is what the code will do.
But, it is my impression that doing so from c++ would require calling
into the python interpreter rather than access directly the c++ bound
method. I want to access directly the c++ bound method from c++ which is
the whole point of this testcase.
updated testcase (reworked as per your requests):
http://spoutnik.inria.fr/yans/releases/test-python.tar.gz
[mlacage at chronos test-python]$ time -p ./test.py --slow
real 12.68
user 12.54
sys 0.01
[mlacage at chronos test-python]$ time -p ./test.py
real 0.32
user 0.23
sys 0.00
Which shows that special-casing bound methods makes them slightly faster
which is exactly what I was after. Now, clearly, I am not too happy with
the way I achieved this.
Mathieu
PS: no attention was paid to any form of memory management here.
--
More information about the Cplusplus-sig
mailing list