[m-rev.] for review: efficiency improvement for string__hash

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Nov 21 20:03:12 AEDT 2002


On 21-Nov-2002, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> 
> library/string.m:
> 	Avoid memory allocation in string__hash.
> 
> Index: library/string.m
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/library/string.m,v
> retrieving revision 1.184
> diff -u -u -r1.184 string.m
> --- library/string.m	15 Nov 2002 04:50:37 -0000	1.184
> +++ library/string.m	21 Nov 2002 02:33:18 -0000
> @@ -1208,25 +1208,28 @@
>  
>  string__hash(String, HashVal) :-
>  	string__length(String, Length),
> -	string__to_int_list(String, CodeList),
> -	string__hash_2(CodeList, 0, HashVal0),
> +	string__hash_2(String, 0, Length, 0, HashVal0),
>  	HashVal = HashVal0 `xor` Length.
>  
> -:- pred string__hash_2(list(int), int, int).
> -:- mode string__hash_2(in, in, out) is det.
> +:- pred string__hash_2(string, int, int, int, int).
> +:- mode string__hash_2(in, in, in, in, out) is det.
>  
> -string__hash_2([], HashVal, HashVal).
> -string__hash_2([X | Xs], HashVal0, HashVal) :-
> -	string__combine_hash(HashVal0, X, HashVal1),
> -	string__hash_2(Xs, HashVal1, HashVal).
> +string__hash_2(String, Index, Length, HashVal0, HashVal) :-
> +	( Index < Length ->
> +		string__combine_hash(HashVal0,
> +			char__to_int(string__unsafe_index(String, Index)),
> +			HashVal1),
> +		string__hash_2(String, Index + 1, Length, HashVal1, HashVal)
> +	;
> +		HashVal = HashVal0
> +	).

I'd be tempted to use string__foldl.
But this looks fine.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list