[m-rev.] Improved array error messages

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Jan 21 18:02:53 AEDT 2002


On 16-Jan-2002, Peter Ross <peter.ross at miscrit.be> wrote:
> I suggest printing out the type of the array which is similar to what is
> done for map lookup failures.

I don't think that is a good idea, because it could be bad for performance:
using the type name could prevent the compiler from optimizing
away the construction of the type_info in the caller,
because it would prevent unused argument elimination.
Performance is important here, because array__set and array__lookup
are likely to be used in the inner loops of performance-critical
applications.

Hence the following change.

----------

Estimated hours taken: 0.45

library/array.m:
	- Don't include the array type name in the error message,
	  since doing so could inhibit optimization.
	- Move the call to throw/1 into the body of out_of_bounds_error/3,
	  to improve code locality.
	- Improve the error message: say explicitly "index ... not in range".

Workspace: /home/earth/fjh/ws-earth4/mercury
Index: library/array.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/array.m,v
retrieving revision 1.102
diff -u -d -r1.102 array.m
--- library/array.m	20 Jan 2002 07:32:18 -0000	1.102
+++ library/array.m	21 Jan 2002 05:43:47 -0000
@@ -897,7 +897,7 @@
 
 array__lookup(Array, Index, Item) :-
 	( bounds_checks, \+ array__in_bounds(Array, Index) ->
-		throw(out_of_bounds_error("array__lookup", Array, Index))
+		out_of_bounds_error(Array, Index, "array__lookup")
 	;
 		array__unsafe_lookup(Array, Index, Item)
 	).
@@ -935,7 +935,7 @@
 
 array__set(Array0, Index, Item, Array) :-
 	( bounds_checks, \+ array__in_bounds(Array0, Index) ->
-		throw(out_of_bounds_error("array__set", Array0, Index))
+		out_of_bounds_error(Array0, Index, "array__set")
 	;
 		array__unsafe_set(Array0, Index, Item, Array)
 	).
@@ -1612,18 +1612,24 @@
 
 %------------------------------------------------------------------------------%
 
-    % out_of_bounds_error(OpName, Array, Index) = IndexOutOfBounds.
-    %
-:- func out_of_bounds_error(string, array(T), int) = index_out_of_bounds.
+	% throw an exception indicating an array bounds error
+:- pred out_of_bounds_error(array(T), int, string).
+:- mode out_of_bounds_error(array_ui, in, in) is erroneous.
+:- mode out_of_bounds_error(in, in, in) is erroneous.
 
-out_of_bounds_error(Op, A, I) =
-    index_out_of_bounds(
-        string__format(
-            "%s: array type %s, bounds [%d, %d], index %d",
-    	    [s(Op), s(type_name(type_of(A))),
-             i(array__min(A)), i(array__max(A)), i(I)]
-	)
-    ).
+	% Note: we deliberately do not include the array element type name
+	% in the error message here, for performance reasons:
+	% using the type name could prevent the compiler from optimizing
+	% away the construction of the type_info in the caller,
+	% because it would prevent unused argument elimination.
+	% Performance is important here, because array__set and array__lookup
+	% are likely to be used in the inner loops of performance-critical
+	% applications.
+out_of_bounds_error(Array, Index, PredName) :-
+	array__bounds(Array, Min, Max),
+	throw(array__index_out_of_bounds(
+		string__format("%s: index %d not in range [%d, %d]",
+			[s(PredName), i(Index), i(Min), i(Max)]))).
 
 %------------------------------------------------------------------------------%
 %------------------------------------------------------------------------------%
-- 
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