/***
	\file "mem-01.hac"
	$Id: mem-01.hac,v 1.8 2006/11/27 23:19:50 fang Exp $
REMARKS:
For large arrays, unrolling dies running out of memory, need to investigate...
Known to choke on 40 x 40

DIAGNOSIS:
	Too much copied instance collection information.

Priority: HIGH

Solution (proposed): use instance placeholders, and refactor out base info.
***/

defproc globals (
	bool Vdd, GND, vdd, gnd, _pReset, _sReset
	)
{
	vdd=Vdd;
	GND=gnd;

	prs {
		~_pReset -> vdd+
		vdd -> gnd-
	}
}


defproc gadfly (bool d[2]; bool t,f)
{
	d[0]=f;
	d[1]=t;
}

defproc ch1of2 (bool d[2]; bool t,f)
{
	d[0]=f;
	d[1]=t;
}
defproc e1of2 (bool d[2]; bool t,f,e)
{
	d[0]=f;
	d[1]=t;
}
defproc snicker (gadfly pb; ch1of2 b; bool w)
{
	prs {
		pb.t | pb.f -> b.t-
		pb.t | pb.f -> b.f-
		pb.t | pb.f -> w-
	}
}

defproc doodle (
	snicker tusk;
	snicker puppy;
	snicker copy[4]
) { }


defproc knickknack(
	snicker IMc1[4];
	snicker IMc0[3];
	snicker IMd[4][4]
) {}
	

defproc flubflub(
	snicker imp[5];
	snicker demon[5];
	snicker devil[4];
	snicker BSD[8][4]
) {}


defproc ickypoo (
	doodle sniffle[8][4];
	knickknack whack;
	flubflub flam;
	snicker wor[3];
	snicker x[2], y[6];
	snicker cracker;
	snicker dee[8][5], dum[8][4]
) { }

defproc hohoho (
	globals gl;
	e1of2 in[8][4];
	e1of2 out[16];
	e1of2 cin, cout;
	e1of2 book, worm;
	ickypoo p
) {
	e1of2 l[8][4][4];
	e1of2 a[4][4], b[4][4], c[4][4], d[4][4];
	e1of2 e[4][4], f[4][4], g[4][4], h[4][4];
	l[0] = a;
}

// enlarge to test memory

// The following tests are benchmarked on a 2.5 GHz dual G5, apple-gcc-3.3.
// 10x10 take 1G+ of memory, 
// ~20 sec to unroll, ~30 s to create, ~35 s to alloc.
hohoho rum[10][10];

/** FILE SIZES as of HACKT-00-01-04-main-00-80-03
-rw-r--r-- 1 user      1568 Aug 24 16:09 mem-01.hac
-rw-r--r-- 1 user     82862 Aug 24 16:09 mem-01.haco
-rw-r--r-- 1 user 159202524 Aug 24 16:16 mem-01.haco-a
-rw-r--r-- 1 user 145855624 Aug 24 16:15 mem-01.haco-c
-rw-r--r-- 1 user 137365327 Aug 24 16:10 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-03-instance-00-38
// near completion of placeholder feature
-rw-r--r-- 1 user     82352 Oct 16 23:19 mem-01.haco
-rw-r--r-- 1 user 155955547 Oct 16 23:21 mem-01.haco-a
-rw-r--r-- 1 user 142608647 Oct 16 23:20 mem-01.haco-c
-rw-r--r-- 1 user 134118350 Oct 16 23:19 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-03-instance-01-08
// use of dense port formal arrays to replace sparse collections
// minor 10% speedup: 18/27/30 s
// peak memory (using top) about the same
-rw-r--r-- 1 user     82380 Oct 23 10:12 mem-01.haco
-rw-r--r-- 1 user 155298463 Oct 23 10:14 mem-01.haco-a
-rw-r--r-- 1 user 141951563 Oct 23 10:13 mem-01.haco-c
-rw-r--r-- 1 user 133615282 Oct 23 10:13 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-44
// but configured with --disable-object-align-checking
// conclusion: doesn't save much because of header entry overhead
// per heap-allocated object
-rw-r--r-- 1 user     80220 Oct 24 17:10 mem-01.haco
-rw-r--r-- 1 user 151814027 Oct 24 17:12 mem-01.haco-a
-rw-r--r-- 1 user 138467127 Oct 24 17:11 mem-01.haco-c
-rw-r--r-- 1 user 130130846 Oct 24 17:10 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-45-instance-02-05
// having replaced port (formal) collections with lightweight 
// port_actual_collections using back-references.  
// significant speedup: 11/22/26 s
// peak memory reduced about 10%, closer to 1G each
// benefits attributed to reduced number of heap-allocated objects
-rw-r--r-- 1 user     82380 Oct 29 15:50 mem-01.haco
-rw-r--r-- 1 user 130072574 Oct 29 15:51 mem-01.haco-a
-rw-r--r-- 1 user 116725674 Oct 29 15:51 mem-01.haco-c
-rw-r--r-- 1 user 108389393 Oct 29 15:50 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-45-instance-02-11
// after omitting back-ref pointers from instance_alias to container:
// speedup due to size reduction: 11/21/24 s
// didn't check peak memory usage
-rw-r--r-- 1 user     82380 Oct 31 15:57 mem-01.haco
-rw-r--r-- 1 user 128230038 Oct 31 15:58 mem-01.haco-a
-rw-r--r-- 1 user 114883138 Oct 31 15:58 mem-01.haco-c
-rw-r--r-- 1 user 106546857 Oct 31 15:58 mem-01.haco-u
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-45-instance-02-21
// heap-allocated footprints, and pool-allocated collections per footprint
// This is what we've been waiting for:
// speedup due to size reduction: 2/5/4 s (NOT a typo)
-rw-r--r-- 1 user    82893 Nov  5 18:22 mem-01.haco
-rw-r--r-- 1 user 23939803 Nov  5 18:22 mem-01.haco-a
-rw-r--r-- 1 user 11138503 Nov  5 18:22 mem-01.haco-c
-rw-r--r-- 1 user  8612588 Nov  5 18:22 mem-01.haco-u
// NOTE the (near) order of magnitude improvement in size and speed.  
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-45-instance-02-23
// so previous revision was screwed up... missing scope alias info.
// after fix, we now get:
// times: 2/7/7 s
-rw-r--r-- 1 user    82893 Nov  7 00:33 mem-01.haco
-rw-r--r-- 1 user 43220832 Nov  7 00:34 mem-01.haco-a
-rw-r--r-- 1 user 30419532 Nov  7 00:33 mem-01.haco-c
-rw-r--r-- 1 user  8612588 Nov  7 00:33 mem-01.haco-u
// If we can find a way to regenerate alias information upon reconstruction
// or a way of auto-caching, then we could save that much space again...
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-60
// after discovering that subinstances' (actual-collections') aliases
// were getting counted multiple times redundantly, we decided to fix it.
// times: 2/6/6 s (on i686-apple-darwin)
-rw-r--r-- 1 user    82893 Nov 27 02:12 mem-01.haco
-rw-r--r-- 1 user 30677641 Nov 27 02:13 mem-01.haco-a
-rw-r--r-- 1 user 17876341 Nov 27 02:13 mem-01.haco-c
-rw-r--r-- 1 user  8612588 Nov 27 02:13 mem-01.haco-u
// Wow, there goes a factor of 2!
// next: enable scope-alias regeneration at load-time
**/

/** FILE SIZES as of HACKT-00-01-04-main-00-80-62
// after enabling scope-alias regeneration upon loading
// little time-space tradeoff
// times: 2/6/6 s
// times: 2/5/8 s (on i686-apple-darwin)
-rw-r--r--   1 user     85019 Nov 27 13:32 mem-01.haco
-rw-r--r--   1 user  25083625 Nov 27 13:33 mem-01.haco-a
-rw-r--r--   1 user  12282325 Nov 27 13:32 mem-01.haco-c
-rw-r--r--   1 user   8614606 Nov 27 13:32 mem-01.haco-u
// saves about 25% in space, at the cost of re-doing some work
**/


