Destructive Operations and Maintaining Identity 
Author Message
 Destructive Operations and Maintaining Identity

A question about coding style.

The result of add!(sequence, new-value) sometimes is guaranteed to be ==
the sequence argument and sometimes it is not. For example, if sequence
is a <deque> then the result is ==, if the sequence is a <list> it is
not.  So in order for code to be easily changed between different
subclasses of <sequence>, it should always look like this

    my-sequence := add!(my-sequence, new-value);

and not like this

   add!(my-sequence, new-value);

However, the additional assignment is often just wasted CPU cycles.  So
I was wondering, is it possible to write a compiler optimization that
would skip the unnecessary assignment if the add! generic function had
proper sealing?

Another solution might be to write a macro that would skip the
assignment if a maintains-identity?(sequence) generic function returns
true.  Future sequences could specialize maintains-identity? so this
solution is extensible.  I'm presuming that you would have to write a
separate macro for each destructive function, i.e. add!(), remove!(),
reverse!(), replace-elements!(), etc.  This solution has the
disadvantage that most code would be written to use DRM standard
interfaces and not the more efficient macros.

Are there any other approaches to this problem?  Do you agree that the
current situation of choosing between performace and portabity is
unsatisfactory?  From my limited experience with Dylan, it has been so
nice to see such an emphasis on abstraction and portable interfaces.
Dylan has struck me as a "I'll have my cake and I'll eat it too"

--== Sent via Deja.com http://www.*-*-*.com/
---Share what you know. Learn what you don't.---



Sun, 04 Nov 2001 03:00:00 GMT  
 Destructive Operations and Maintaining Identity

Quote:

>     my-sequence := add!(my-sequence, new-value);

> and not like this

>    add!(my-sequence, new-value);

> However, the additional assignment is often just wasted CPU cycles.  So
> I was wondering, is it possible to write a compiler optimization that
> would skip the unnecessary assignment if the add! generic function had
> proper sealing?

Using Gwydion Dylan's d2c compiler,
  my-sequence := add!(my-sequence, new-value);
actually produces smaller object code than
  add!(my-sequence, new-value);

Using some sample code which adds 10 items to a stretchy vector, the
following table depicts the size in bytes of the files.  

                no     assign
              assign

 seq.dylan      523      683
 seq.c         5585     6989
 seq.o        11236     9960
 seq        1883959  1883287

I'm not sure about performance.

--------------------------------------------------------------------

Dylan... the high-performance dynamic language
 * open-source Unix version:  http://www.gwydiondylan.org/
 * free Win32 version: http://www.harlequin.com/products/ads/dylan/
---------------------------------------------------------------------



Sun, 04 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Destructive list operation

2. Destructive Operations

3. math operation and bits operations

4. Message Operations and Meta-operations

5. Are "destructive" functions really destructive?

6. is "destructive" function really destructive?

7. non destructive conditionals ?

8. what is destructive update?

9. ADT (monadic) approach to destructive update

10. destructive functions (eg add! )

11. destructive functions (eg add! )

12. destructive functions (eg add! ) [longish]

 

 
Powered by phpBB® Forum Software