Jump to content
chemicalbliss

Custom functions in client extension dll?

Recommended Posts

I guys and gals, I've been working on learning sqf performance so I can write the fastest addoon possible (No db lag or sqf scheduler overruns). Please dont say anything about premature optimization etc, this is what I'm doing :).

 

So, I have noticed SQF functions in Arma 2 at least are quite.. shall I say.. Snail pace, compared to true compiled function eg c++. Now whether it is how it is compiled/gc'd or w/e that's not he point. I have noticed almost any function I make in arma 2 sqf is orders of magnitude slower than what I can write and use using an extension dll in c++. A simple substr function for example;

 

For example, in the C++ DLL extension:

		if (tPacket->Method == "substr")
		{
			size_t start = utilStringToInt_(tPacket->Data.substr(0, 5));
			size_t length = utilStringToInt_(tPacket->Data.substr(6, 5));
			std::string strData = tPacket->Data.substr(12);

			result = (length != 0)? strData.substr(start, length) : strData.substr(start);
		}

in SQF: (Limited knowledge of SQF commands this is the most efficient method I could create.)

	fnc_substr = {
		private["_string","_strArray","_stringsub","_start","_length", "_tempstr"];
		_string = _this select 0;
		_strArray  = toArray _string;
		_start = _this select 1;
		_length = _this select 2;
		
		if (_length == 0) then {
			_length = (count _strArray) - _start; // from start to end
		};
		_end = _start + _length;
		_stringsub = "";
		_tempstr = "";
		for "_i" from _start to _end-1 do { 
			_tempstr = toString [_strArray select _i];
			_stringsub = format ["%1%2", _stringsub, _tempstr]; 
		};
		_stringsub;
	};

The Test Code:

	// dll func bench
	_diagTickBegin = diag_tickTime;
	for "_t" from 0 to 10000 do {
		_temphandle = "cb_ff_dbi" callExtension "int|name";
		_directResp = "cb_ff_dbi" callExtension format ["fnc|substr|00013,00000,%1", _temphandle];
		//diag_log(format ["cb_ff_dbi name: %1", _directResp]);
		_temphandle = "cb_ff_dbi" callExtension "int|version";
		_directResp = "cb_ff_dbi" callExtension format ["fnc|substr|00013,00000,%1", _temphandle];
		//diag_log(format ["cb_ff_dbi version: %1", _directResp]);
		_temphandle = "cb_ff_dbi" callExtension "int|author";
		_directResp = "cb_ff_dbi" callExtension format ["fnc|substr|00013,00000,%1", _temphandle];
		//diag_log(format ["cb_ff_dbi author: %1", _directResp]);
	};
	_diagTickEnd = diag_tickTime;
	diag_log(format ["cb_ff_dbi::Controller->fnc_substr(): %1", _diagTickEnd - _diagTickBegin]);
	
	// sqf func bench
	_diagTickBegin = diag_tickTime;
	for "_t" from 0 to 10000 do {
		_temphandle = "cb_ff_dbi" callExtension "int|name";
		_ticketedata = [_temphandle, 13, 0] call fnc_substr;
		//diag_log(format ["cb_ff_dbi name: %1", _ticketedata]);
		_temphandle = "cb_ff_dbi" callExtension "int|version";
		_ticketedata = [_temphandle, 13, 0] call fnc_substr;
		//diag_log(format ["cb_ff_dbi version: %1", _ticketedata]);
		_temphandle = "cb_ff_dbi" callExtension "int|author";
		_ticketedata = [_temphandle, 13, 0] call fnc_substr;
		//diag_log(format ["cb_ff_dbi author: %1", _ticketedata]);
	};
	_diagTickEnd = diag_tickTime;
	diag_log(format ["sqf::main::fnc_substr(): %1", _diagTickEnd - _diagTickBegin]);

The Results:

20:39:51 "cb_ff_dbi::Controller->fnc_substr(): 2.132"
20:40:19 "sqf::main::fnc_substr(): 28.105"

So, now I want to write all my expensive sorting and expressive functions in a client DLL so the client can benefit from that performance gain. The thing is, BE Blocks DLL's it doesnt know (I'm assuming some kind of whitelist?). So my question is, how can I whitelist a DLL (and possible even variations on that DLL for updates). I wonder if CBA has such a white-listed helper dll like this.

 

Also, is this actually a good idea? (I dont care about players having to download dll or mod files etc or security, that is my own concern).

 

Thanks!

Share this post


Link to post
Share on other sites

 

Also, is this actually a good idea? (I dont care about players having to download dll or mod files etc or security, that is my own concern).

 

If you really have to loop thousands of times and call functions in it then maybe its worth to write those functions in c++. But writing functions in c++ that are only called few times is not worth it, IMO. You will not gain any significant speed improvement if single call to both sqf/c++ functions are ran < 0.1 seconds.

 

I also have to note that things like string management should already be hardcoded by BIS.

Share this post


Link to post
Share on other sites

The way this is setup includes the time it takes to call the extension. It would appear the evidence shows this is quicker than the sqf version even in single usage. I also cant find any standard BIS substr function (arma 2)?

 

Also cannot find any pre-written example of substr for arma 2, only methods using select with arma 3.

Share this post


Link to post
Share on other sites

The way this is setup includes the time it takes to call the extension. It would appear the evidence shows this is quicker than the sqf version even in single usage. I also cant find any standard BIS substr function (arma 2)?

 

Also cannot find any pre-written example of substr for arma 2, only methods using select with arma 3.

 

I did some math on the time results you posted and this is what I get for each single function call:

 

"cb_ff_dbi::Controller->fnc_substr()"

 

executed in: 0,0002132 seconds
 
"sqf::main::fnc_substr()"
 
executed in: 0,0028105 seconds
 
so you see there is not much difference with single calls the benefit of optimization comes only when you have long loops like that.
 
 
And there actually is substr equivalent for arma 3, it's select.
 
example: 
 
"test string" select [5,6];
 
= "string"

Share this post


Link to post
Share on other sites

Thanks gc but this is regarding arma 2. I mentioned the select method for a3 in my post maybe you didnt see. And for a game its all about performance. I cant see any downside atm. For ex, there will be other functions that may be sped up by this method. Will test more.

Share this post


Link to post
Share on other sites

Thanks gc but this is regarding arma 2. I mentioned the select method for a3 in my post maybe you didnt see. And for a game its all about performance. I cant see any downside atm. For ex, there will be other functions that may be sped up by this method. Will test more.

 

oh sorry my bad. thought this was for arma3. hmm maybe they will update the select for arma2...

Share this post


Link to post
Share on other sites

Well no offence, but if i look at the SQF code it could`ve been improved a lot more, appart from the fact, that a while do loop would`ve been better here.

Share this post


Link to post
Share on other sites

Well no offence, but if i look at the SQF code it could`ve been improved a lot more, appart from the fact, that a while do loop would`ve been better here.

 

Hi ViseVersa,

 

None taken at all - although I would appreciate some details about how it could be improved and why a while loop would of been better? I've sent a PM in case you don't see this message, sorry for necroing this topic but if there is something I'm doing wrong/inefficiently I would like to know what it is.

 

Thanks!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×