On Fri, 15 Jun 2007 14:39:49 -0000, <
[email protected]> wrote:
>However, I would prefer to put the reference in the mailbox and not to
>copy it. Once your generator puts an instance in the mailbox, have the
>top of your generate loop just new() to get a new instance.
Very interesting... I've been arguing with myself about this for
some considerable time. Eventually I have decided that I like
my "copy-on-post" approach best. My reasoning goes like this;
I'd be delighted to hear from anyone who disagrees or has a
different spin on it.
(1) The argument from robustness
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If I can be truly, positively, 100% sure that I have given
no-one else a reference to my object, and I will never
extend the code in my stimulus-generation loop to give
anyone such a reference, then it's obviously fine to do
as Mike suggests. But it worries me, because I know that
code grows and changes, and assumptions made today are
unlikely to hold tomorrow. If I copy-on-post then there
is _nothing_ I can do to the code that could break my trust
in the uniqueness of the reference that's on the mailbox.
What's more, the copy on the mailbox is then inviolable
and the client at the other end of the mailbox won't get
any nasty surprises because of someone else messing with it.
(2) The argument from code conciseness
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I agree that it's a tad clunky to be obliged to do
mb.put(thing.copy())
every time. But there's nothing to stop me extending
the mailbox class and overriding its put() method
(or providing an alternative, "copy_put") so that
the copy operation is encapsulated there.
(3) The argument from efficiency
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is worryingly expensive to do a copy every time you
want to post something on the mailbox. But this is, in
fact, a largely specious argument. In the simple case
Mike describes, the cost of a new() each time around
the loop is almost as large as the cost of the shallow
copy in my .copy() method; recall that new() in SV must
initialise all the new object's properties to default
values, so it is a modest overhead to initialise them
from an existing object instead. In more complex
cases, it's essential to clone the object *somewhere*
in any case.
(4) The argument from self-preservation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm a trainer by trade. It's important to me to be
able to offer methodologies and techniques that allow
others to create reliable code without them needing to
turn their brains inside out. I can offer this sane,
universally-applicable advice to people for whom this
stuff is novel, and I'll get fewer follow-up calls:
Whenever you're giving away a reference to an object
to some agent that might mess with the object at
some time in the future, then you better be sure that
at least one of the following things is true:
- The messing-with-the-object is being done on your
behalf, with your consent, and you will get to
be notified when it's done; OR
- You have a copper-bottomed guarantee that you, and
all your helper processes, are never going to look
at that object ever again.
By far the easiest way of ensuring the second of these
criteria is to arrange that the object that you gave
away is a freshly-minted copy of the object you care about.
Thoughts, anyone?
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
[email protected]
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.