Jump to content
  • Advertisement
Sign in to follow this  
FDsagizi

asEP_ALLOW_UNSAFE_REFERENCES and performance

This topic is 2471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How to tell the performance of the use of this flag?

And I do not understand what it is not safe?

Can you see an example of code where the program would be taken off.

Share this post


Link to post
Share on other sites
Advertisement
The unsafe part comes from the fact that the caller may pass a reference to an object that is destroyed during the execution of the called function, which may make the called function access an invalid pointer. Of course, this is no more unsafe than parameter references in C++, so if you trust your script writers to write the appropriate code you should be perfectly fine.

Here's an example where an unsafe reference is accessed incorrectly:


array<int> g_arr = {0};

void main()
{
func(g_arr[0]); // Pass a reference to an element of the array
}

void func(int &unsafeRef)
{
// Resize the global array, thus reallocating the internal buffer
g_arr.resize(10);

// Now the parameter reference is invalid, as it was referencing an element in the old internal buffer
// and the result with reading from or updating the reference is most likely not what was expected
unsafeRef = 10;
}


By allowing the use of unsafe references in the script, you may avoid unnecessary implicit copies of objects and values, and thus improve the performance. However, chances are that you will not notice this performance differences.

Regards,
Andreas

Share this post


Link to post
Share on other sites

Vec3 mul( Quat q, Vec3 v ){ return q * v; }
Vec3 mulL( Quat &q, Vec3 &v ){return q * v;}

void startGame( string @param )
{

float time = MilliSecs();

Vec3 v;
Quat q;

for( int i =0; i < 100000; i++ )
v = mul( q, v );

output( "time: " + ( MilliSecs() - time ) );



time = MilliSecs();

for( int i =0; i < 100000; i++ )
v = mulL( q, v );

output( "timeL: " + ( MilliSecs() - time ) );
}


time: ~115
timeL: ~20


Well then I'll use them - because it's no problem really.

Removing an object is always to be done carefully.

Thank you! I am more and more like AngelScript!

Share this post


Link to post
Share on other sites
The following code should give a similar result without the need to turn on asEP_ALLOW_UNSAFE_REFERENCES.


Vec3 mul( const Quat &in q, const Vec3 &in v ){ return q * v; }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!