
Hello, I sometimes wonder just how far I should go in obfuscating code by manually implementing trivial optimisations that should (IMHO) be implemented by the compiler, but may not be. So I have a couple of specific questions.. The first is.. Does the compiler keep a unique copy of expressions which consist of just a single zero arity constructor (eg. [],True,Nothing..) as a CAF which is referenced each time the constructor appears in an expression, or does it duplicate the constructor (expression) each time it's used. Maybe I should define my own CAF at the top level and use it instead? (or perhaps they're unboxed somehow?) The second is.. If, following pattern matching, the matched pattern appears in an expression, is it shared or duplicated.. e.g. (a:as) -> f (a:as) Maybe I should write something like this.. x@(a:as) -> f x (I know that sometimes the type checker won't allow you to do this) Thanks -- Adrian Hey

On Fri, 7 Dec 2001, Adrian Hey wrote:
The first is.. Does the compiler keep a unique copy of expressions which consist of just a single zero arity constructor (eg. [],True,Nothing..) as a CAF which is referenced each time the constructor appears in an expression, or does it duplicate the constructor (expression) each time it's used. Maybe I should define my own CAF at the top level and use it instead? (or perhaps they're unboxed somehow?)
Really idle curiosity... why would having a single copy of a zero arity constructor be more efficient than have multiple copies? Wouldn't they fit into a `cell' which wouldn't be larger than the one that would (IIRC) be used for the indirection to the CAF? (I can understand a larger CAF being a win, but one this small?) ___cheers,_dave_________________________________________________________ www.cs.bris.ac.uk/~tweed/|`...heat generated by its microprocessors will email:tweed@cs.bris.ac.uk|slope upward exponentially, reaching the power work tel:(0117) 954-5250 |density of a nuclear reactor before 2010'-Intel

On Friday 07 December 2001 2:27 pm, D. Tweed wrote:
On Fri, 7 Dec 2001, Adrian Hey wrote:
The first is.. Does the compiler keep a unique copy of expressions which consist of just a single zero arity constructor (eg. [],True,Nothing..) as a CAF which is referenced each time the constructor appears in an expression, or does it duplicate the constructor (expression) each time it's used. Maybe I should define my own CAF at the top level and use it instead? (or perhaps they're unboxed somehow?)
Really idle curiosity... why would having a single copy of a zero arity constructor be more efficient than have multiple copies? Wouldn't they fit into a `cell' which wouldn't be larger than the one that would (IIRC) be used for the indirection to the CAF? (I can understand a larger CAF being a win, but one this small?)
Well I suppose if it's necessary to create a new indirection heap record for each reference, then there's not really any point in having a single copy of the value itself. But I don't see why that should be so. Even if it is indirected for some reason it should still be possible to share the indirection record I think. Maybe CAF is the wrong word to use here since there's no application involved. What I mean is.. are zero arity constructors referenced the same way as a top level constant would be? (Not that I know how that's done, but I presume there's only 1 copy of top level constants in memory at any one time.) I was thinking of data types like binary trees. If the tree was balanced, and a new copy of a leaf (zero arity empty tree) was constructed on the heap every time a function returned this value, then this would double the No. of heap records associated with the tree. This would waste memory and slow down garbage collection. I just wanted to make sure I don't need to use any weird programming style to ensure this doesn't happen. Regards -- Adrian Hey
participants (2)
-
Adrian Hey
-
D. Tweed