for review: speeding up the browser

Zoltan Somogyi zs at cs.mu.OZ.AU
Tue Feb 23 20:38:02 AEDT 1999


Avoid a two-three second delay in the printing of (the top levels of) large
data structures such as module_infos in the debugger.

browser/browse.m:
	Instead of computing the exact size of the term when we check whether
	we can print it in full, compute the size only up to the maximum,
	and skip the rest. This lets us avoid traversing 99+% of typical
	module_infos.

Zoltan.

cvs diff: Diffing .
Index: browse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browse.m,v
retrieving revision 1.6
diff -u -u -r1.6 browse.m
--- browse.m	1998/12/28 03:38:45	1.6
+++ browse.m	1999/02/23 09:32:29
@@ -98,9 +98,9 @@
 	% we use portray_fmt(..., flat).
 	%
 	{ get_term(State, Univ) },
-	{ term_size(Univ, Size) },
 	{ max_print_size(MaxSize) },
-	( { Size =< MaxSize } ->
+	{ term_size_left_from_max(Univ, MaxSize, RemainingSize) },
+	( { RemainingSize >= 0 } ->
 		io__write_univ(Univ),
 		io__nl
 	;
@@ -126,6 +126,35 @@
 	AddSizes = (pred(X::in, Y::in, Z::out) is det :- Z = X + Y),
 	list__foldl(AddSizes, ArgSizes, Arity * 2, TotalArgsSize),
 	TotalSize = TotalArgsSize + FunctorSize.
+
+	% Estimate the total term size, in characters,
+	% We count the number of characters in the functor,
+	% plus two characters for each argument: "(" and ")"
+	% for the first, and ", " for each of the rest,
+	% plus the sizes of the arguments themselves.
+	% This is only approximate since it doesn't take into
+	% account all the special cases such as operators.
+	%
+	% This predicate returns not the estimated total term size,
+	% but the difference between the given maximum size the caller
+	% is interested in and the estimated total term size.
+	% This difference is positive if the term is smaller than the
+	% maximum and negative if it is bigger. If the difference is
+	% negative, term_size_left_from_max will return a negative difference
+	% but the value will usually not be accurate, since in such cases
+	% by definition the caller is not interested in the accurate value.
+:- pred term_size_left_from_max(univ::in, int::in, int::out) is det.
+term_size_left_from_max(Univ, MaxSize, RemainingSize) :-
+	( MaxSize < 0 ->
+		RemainingSize = MaxSize
+	;
+		deconstruct(Univ, Functor, Arity, Args),
+		string__length(Functor, FunctorSize),
+		PrincipalSize = FunctorSize + Arity * 2,
+		MaxArgsSize = MaxSize - PrincipalSize,
+		list__foldl(term_size_left_from_max,
+			Args, MaxArgsSize, RemainingSize)
+	).
 
 %---------------------------------------------------------------------------%
 %



More information about the developers mailing list