Code generation tricks

JS js.mdnq at gmail.com
Mon Jul 22 17:20:22 PDT 2013


On Monday, 22 July 2013 at 21:04:42 UTC, John Colvin wrote:
> On Sunday, 21 July 2013 at 17:24:11 UTC, JS wrote:
>> This seems to be a somewhat efficient string splitter
>>
>> http://dpaste.dzfl.pl/4307aa5f
>>
>> The basic idea is
>>
>> for(int j = 0; j < s.length; j++)
>> 	{
>> 		mixin(ExpandVariadicIf!("??Cs[j]??s[j..min(s.length-1, j + 
>> %%L)]::", "d", "
>> 			if (r.length <= i) r.length += 5;
>> 			if (j != 0)
>> 			{
>> 				r[i++] = s[oldj..j];
>> 				oldj = j + %%L;
>> 			}
>> 			else
>> 				oldj = %%L;
>> 		 j += %%L; continue;", T));
>> 		
>> 	}
>>
>> ExpandVariadicIf creates a series of if's for each variadic 
>> argument. There is some strange formatting(just some crap I 
>> threw together to get something working) but it boils down to 
>> generating compile time code that minimizes computations and 
>> lookups by directly using the known compile time literals 
>> passed.
>>
>> IMO these types of functions seem useful but ATM are just 
>> hacks. Hopefully there is a better way to do these sorts of 
>> things as I find them pretty useful.
>>
>> One of the big issues not being able to pass a variadic 
>> variable to a template directly which is why the formatting 
>> string is necessary(You can pass the typetuple to get the 
>> types and size but not the compile time values if they exist.
>>
>> I think int this case a variadic alias would be very useful.
>>
>> alias T... => alias T0, alias T1, etc....
>> (e.g. T[0] is an alias, T.length is number of aliases, etc...)
>>
>> In any case, maybe someone has a good way to make these things 
>> easier and more useful. Being able to handle variadic types 
>> and values in a consistent and simple way will make them 
>> moreful.
>
> How does this perform compared to naive/phobos splitting?

I don't know... probably not a huge difference unless phobo's is 
heavily optimized.

With just one delim, there should be no difference. With 100 
delim literals, it should probably be significant, more so when 
chars are used. If the compiler is able to optimize slices of 
literal strings then it should be even better.


Heres my test code that you might be able to profile if you want:

http://dpaste.dzfl.pl/2f10d24a

The code has a bunch of errors on it but compiles fine on mine. 
Must be some command line switch or something.

The Expand templates simply allow one to expand the variadic args 
into compile time expressions. e.g., we can do if (a == b) with 
normal args but not with variargs... the templates help 
accomplish that. (I'm sure there are better ways... think of the 
code as proof of concept).



More information about the Digitalmars-d-learn mailing list