[m-rev.] diff: layout_out loops

Zoltan Somogyi zs at csse.unimelb.edu.au
Tue May 24 11:55:10 AEST 2011


compiler/layout_out.m:
	Some layout tables can get very big. When we print them in grades that
	do not permit tail recursion, we can run out of stack. This diff avoids
	that by switching to a two-level iteration that allows us to free
	pretty much all the stack space we are using every thousand iterations.

	Reduce the cost of this two level loop by avoiding the repetition of
	the same test over and over again in every iteration.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/extra
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/extra
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/libatomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/doc
cvs diff: Diffing boehm_gc/libatomic_ops/src
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/armcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/gcc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/hpc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/ibmc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/icc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/msftc
cvs diff: Diffing boehm_gc/libatomic_ops/src/atomic_ops/sysdeps/sunc
cvs diff: Diffing boehm_gc/libatomic_ops/tests
cvs diff: Diffing boehm_gc/libatomic_ops-1.2
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/doc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/tests
cvs diff: Diffing boehm_gc/m4
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/layout_out.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/layout_out.m,v
retrieving revision 1.117
diff -u -b -r1.117 layout_out.m
--- compiler/layout_out.m	23 May 2011 05:08:04 -0000	1.117
+++ compiler/layout_out.m	24 May 2011 01:50:16 -0000
@@ -565,7 +565,13 @@
         being_defined, !IO),
     io.format("[%d] = {", [i(NumVarNums)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), VarNums, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(VarNums, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(VarNums, 0, !IO)
+    ),
     io.write_string("\n};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -584,7 +590,13 @@
         being_defined, !IO),
     io.format("[%d] = {", [i(NumShortLocns)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), ShortLocns, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(ShortLocns, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(ShortLocns, 0, !IO)
+    ),
     io.write_string("\n};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -603,7 +615,13 @@
         being_defined, !IO),
     io.format("[%d] = {", [i(NumLongLocns)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), LongLocns, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(LongLocns, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(LongLocns, 0, !IO)
+    ),
     io.write_string("\n};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -1268,7 +1286,13 @@
         being_defined, !IO),
     io.format("[%d] = {", [i(NumHeadVarNums)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), HeadVarNums, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(HeadVarNums, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(HeadVarNums, 0, !IO)
+    ),
     io.write_string("\n};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -1287,7 +1311,13 @@
         being_defined, !IO),
     io.format("[%d] = {", [i(NumVarNames)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), VarNames, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(VarNames, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(VarNames, 0, !IO)
+    ),
     io.write_string("\n};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -1306,7 +1336,13 @@
         being_defined, !IO),
     io.format("[%d] = {\n", [i(NumBytecodes)], !IO),
     AutoComments = Info ^ lout_auto_comments,
-    list.foldl2(output_number_in_vector(AutoComments), Bytecodes, 0, _, !IO),
+    (
+        AutoComments = yes,
+        output_numbers_in_vector_ac(Bytecodes, 0, !IO)
+    ;
+        AutoComments = no,
+        output_numbers_in_vector_noac(Bytecodes, 0, !IO)
+    ),
     io.write_string("};\n\n", !IO).
 
 %-----------------------------------------------------------------------------%
@@ -3024,8 +3060,13 @@
     ;
         LineNumbers = [_ | _],
         AutoComments = Info ^ lout_auto_comments,
-        list.foldl2(output_number_in_vector(AutoComments), LineNumbers,
-            0, _, !IO)
+        (
+            AutoComments = yes,
+            output_numbers_in_vector_ac(LineNumbers, 0, !IO)
+        ;
+            AutoComments = no,
+            output_numbers_in_vector_noac(LineNumbers, 0, !IO)
+        )
     ),
     io.write_string("\n};\n", !IO),
     decl_set_insert(decl_layout_id(LayoutName), !DeclSet).
@@ -3138,29 +3179,96 @@
     io.write_string(")\n", !IO).
 
 %-----------------------------------------------------------------------------%
-%-----------------------------------------------------------------------------%
 %
-% Utility predicates.
+% Print out vectors of numbers.
+%
+% We print ten numbers per line to make it easy to locate a particular element
+% in a vector.
+%
+% We do a two level loop to limit the number of stack frames we consume
+% in grades that do not permit tail recursion. The idea is that after printing
+% 1000 numbers, we free up all the stack space that printing consumed.
+% When printing N numbers, we use only about (N / 1000) + (N mod 1000) frames.
 %
+% Each predicate has two versions. The _ac version assumes auto-comment,
+% the _noac version assumes no auto-comment.
+%
+
+:- pred output_numbers_in_vector_ac(list(int)::in, int::in,
+    io::di, io::uo) is det.
+
+output_numbers_in_vector_ac([], _, !IO).
+output_numbers_in_vector_ac(VarNums @ [_ | _], !.Slot, !IO) :-
+    output_upto_n_numbers_in_vector_ac(VarNums, 1000, LeftOverVarNums,
+        !Slot, !IO),
+    output_numbers_in_vector_ac(LeftOverVarNums, !.Slot, !IO).
+
+:- pred output_numbers_in_vector_noac(list(int)::in, int::in,
+    io::di, io::uo) is det.
+
+output_numbers_in_vector_noac([], _, !IO).
+output_numbers_in_vector_noac(VarNums @ [_ | _], !.Slot, !IO) :-
+    output_upto_n_numbers_in_vector_noac(VarNums, 1000, LeftOverVarNums,
+        !Slot, !IO),
+    output_numbers_in_vector_noac(LeftOverVarNums, !.Slot, !IO).
+
+:- pred output_upto_n_numbers_in_vector_ac(list(int)::in, int::in,
+    list(int)::out, int::in, int::out, io::di, io::uo) is det.
+
+output_upto_n_numbers_in_vector_ac([], _, [], !Slot, !IO).
+output_upto_n_numbers_in_vector_ac([VarNum | VarNums], N, LeftOverVarNums,
+        !Slot, !IO) :-
+    ( N > 0 ->
+        output_number_in_vector_ac(VarNum, !Slot, !IO),
+        output_upto_n_numbers_in_vector_ac(VarNums, N - 1, LeftOverVarNums,
+            !Slot, !IO)
+    ;
+        LeftOverVarNums = [VarNum | VarNums]
+    ).
+
+:- pred output_upto_n_numbers_in_vector_noac(list(int)::in, int::in,
+    list(int)::out, int::in, int::out, io::di, io::uo) is det.
+
+output_upto_n_numbers_in_vector_noac([], _, [], !Slot, !IO).
+output_upto_n_numbers_in_vector_noac([VarNum | VarNums], N, LeftOverVarNums,
+        !Slot, !IO) :-
+    ( N > 0 ->
+        output_number_in_vector_noac(VarNum, !Slot, !IO),
+        output_upto_n_numbers_in_vector_noac(VarNums, N - 1, LeftOverVarNums,
+            !Slot, !IO)
+    ;
+        LeftOverVarNums = [VarNum | VarNums]
+    ).
 
-:- pred output_number_in_vector(bool::in, int::in, int::in, int::out,
+:- pred output_number_in_vector_ac(int::in, int::in, int::out,
     io::di, io::uo) is det.
 
-output_number_in_vector(AutoComments, VarNum, !Slot, !IO) :-
+output_number_in_vector_ac(VarNum, !Slot, !IO) :-
     ( !.Slot mod 10 = 0 ->
-        (
-            AutoComments = yes,
             io.format("\n/* slots %d+ */ ", [i(!.Slot)], !IO)
         ;
-            AutoComments = no,
+        io.write_string(" ", !IO)
+    ),
+    io.format("%d,", [i(VarNum)], !IO),
+    !:Slot = !.Slot + 1.
+
+:- pred output_number_in_vector_noac(int::in, int::in, int::out,
+    io::di, io::uo) is det.
+
+output_number_in_vector_noac(VarNum, !Slot, !IO) :-
+    ( !.Slot mod 10 = 0 ->
             io.nl(!IO)
-        )
     ;
         io.write_string(" ", !IO)
     ),
     io.format("%d,", [i(VarNum)], !IO),
     !:Slot = !.Slot + 1.
 
+%-----------------------------------------------------------------------------%
+%
+% Utility predicates.
+%
+
 :- pred output_layout_name_in_vector(string::in, layout_name::in,
     io::di, io::uo) is det.
 
cvs diff: Diffing compiler/notes
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/base64
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/fixed
cvs diff: Diffing extras/gator
cvs diff: Diffing extras/gator/generations
cvs diff: Diffing extras/gator/generations/1
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_allegro
cvs diff: Diffing extras/graphics/mercury_allegro/examples
cvs diff: Diffing extras/graphics/mercury_allegro/samples
cvs diff: Diffing extras/graphics/mercury_allegro/samples/demo
cvs diff: Diffing extras/graphics/mercury_allegro/samples/mandel
cvs diff: Diffing extras/graphics/mercury_allegro/samples/pendulum2
cvs diff: Diffing extras/graphics/mercury_allegro/samples/speed
cvs diff: Diffing extras/graphics/mercury_cairo
cvs diff: Diffing extras/graphics/mercury_cairo/samples
cvs diff: Diffing extras/graphics/mercury_cairo/samples/data
cvs diff: Diffing extras/graphics/mercury_cairo/tutorial
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/log4m
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/monte
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/mopenssl
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/net
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/posix/samples
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/windows_installer_generator
cvs diff: Diffing extras/windows_installer_generator/sample
cvs diff: Diffing extras/windows_installer_generator/sample/images
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/standalone_c
cvs diff: Diffing samples/concurrency
cvs diff: Diffing samples/concurrency/dining_philosophers
cvs diff: Diffing samples/concurrency/midimon
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/java_interface
cvs diff: Diffing samples/java_interface/java_calls_mercury
cvs diff: Diffing samples/java_interface/mercury_calls_java
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/solver_types
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing ssdb
cvs diff: Diffing tests
cvs diff: Diffing tests/analysis
cvs diff: Diffing tests/analysis/ctgc
cvs diff: Diffing tests/analysis/excp
cvs diff: Diffing tests/analysis/ext
cvs diff: Diffing tests/analysis/sharing
cvs diff: Diffing tests/analysis/table
cvs diff: Diffing tests/analysis/trail
cvs diff: Diffing tests/analysis/unused_args
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/par_conj
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/stm
cvs diff: Diffing tests/stm/orig
cvs diff: Diffing tests/stm/orig/stm-compiler
cvs diff: Diffing tests/stm/orig/stm-compiler/test1
cvs diff: Diffing tests/stm/orig/stm-compiler/test10
cvs diff: Diffing tests/stm/orig/stm-compiler/test2
cvs diff: Diffing tests/stm/orig/stm-compiler/test3
cvs diff: Diffing tests/stm/orig/stm-compiler/test4
cvs diff: Diffing tests/stm/orig/stm-compiler/test5
cvs diff: Diffing tests/stm/orig/stm-compiler/test6
cvs diff: Diffing tests/stm/orig/stm-compiler/test7
cvs diff: Diffing tests/stm/orig/stm-compiler/test8
cvs diff: Diffing tests/stm/orig/stm-compiler/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/bm2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/stmqueue
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test10
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test11
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par/test9
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test1
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test2
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test3
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test4
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test5
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test6
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test7
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test8
cvs diff: Diffing tests/stm/orig/stm-compiler-par-asm_fast/test9
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/trailing
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list