Advertisement

[SUGGESTION] Responsibility of keeping 'this' alive in script methods?

Started by November 16, 2013 09:26 PM
2 comments, last by WitchLord 10 years, 6 months ago

I was trying to investigate why a trivial script method call was taking five times as long to execute as the equivalent trivial global function. It became obvious when I checked out the bytecode: every method starts with an AddRef() on the this pointer, and ends with a Release(). These are 'somewhat' expensive function calls handling atomics and such, and completely unnecessary in a large amount of cases.

It really feels like the caller should be responsible for keeping alive the object it's calling on, not the object itself. That way, the caller can make optimizations that would otherwise not be possible. Especially in our engine where many thousands of small methods are being called on script objects already held by the application code every frame, reducing the overhead of a method call seems like a useful thing to do.

After realizing that this behaviour was already in place for methods returning references, we tried to turn that behaviour on for all script method calls. The attached file is not so much a patch as this suggested change in behaviour. It's faster, better optimizable and more consistent - returning a reference no longer changes how the calling convention works. We've been using it for a little while.

It does mean the application needs to ensure objects it's calling methods on stay alive during those calls, which is added responsibility for application writers and a possible break in backwards compatibility, so not making this change is understandable as well. Maybe as an engine option?

Uploader was being annoying.

Advertisement

I will certainly look into this. Thanks for yet another patch.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've included this patch in revision 1941.

Rather than putting the responsibility on the application to remember to keep a reference I added that in to the context. I'm still thinking on making this optional by adding an additional argument to asIScriptContext::SetObject where the application can tell the context that it doesn't need to increment the refcount.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement