# 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.

## 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 on other sites
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 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 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; } 

1. 1
2. 2
Rutin
31
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633647
• Total Posts
3013108
×