try find the fastest way to convert a group string into index?

learnfirst1 learnfirst1 at gmail.com
Sun Sep 16 11:30:11 UTC 2018


On Sunday, 16 September 2018 at 10:14:24 UTC, Vladimir Panteleev 
wrote:
> On Sunday, 16 September 2018 at 10:04:09 UTC, learnfirst1 wrote:
>> how to make this more fast like with one loop and get the 
>> results.


thanks for reply, minimal perfect hashing seems good for this 
task, I will do more test for confirm.

I try to storage the value into a static array, so when  diff 
middleware try access the key it will reused the array offset 
without need to run hash on key string. (all middleware can 
access the key by static enum name).

with Perfect Hash Function, I can translate the hash number into 
static enum and use them.

The code also need provide string get(string key) method, in this 
case the header key may not on the static preset arrays,  in this 
case it should search a smaller array (save on the parse 
process).   with this solution I has to run hash for each of 
request headers , with less than 127 items (maybe around 16 at 
max) I am not sure it is fast then direct string compare. for 
example:

foreach(int header_index, ref header; new_request.headers) {
    int hash = mpfh(header.key);
    auto enum_index = static_eum[ hash ];
    new_request.header[enum_index] = header_index  ;
}




the simple static switch is not the best way as I know here.   
with compiled PCRE match it will "Context-Type", 
"Context-Encoding" in once ( and maybe there is some SSE4 to pass 
16 byte for each loop).

>> Diederik de Groot

please ignore the Header.data assign part. which just for the 
demo. in the real app it will only parse once and storage in pre 
-alloc request/response object.




More information about the Digitalmars-d-learn mailing list