LWN.net Logo

Sometimes C++ is faster

Sometimes C++ is faster

Posted Jun 18, 2008 20:00 UTC (Wed) by flewellyn (subscriber, #5047)
In reply to: Sometimes C++ is faster by ncm
Parent article: Converting GCC to C++

Try doing that with C macros, or even Lisp macros.

You wouldn't use macros for that in (Common) Lisp, that's not what they're for. You'd use CLOS generic functions, which would dispatch on the argument types. Then you get a nice, natural, functional interface with multiple dispatch as a free bonus.


(Log in to post comments)

Sometimes C++ is faster

Posted Jun 18, 2008 21:00 UTC (Wed) by ncm (subscriber, #165) [Link]

Sorry, f., we're talking here about speed, hence compile-time dispatching.  If you don't care
about runtime cost, all kinds (not to say "sorts") of notational convenience are easy to
support.

Sometimes C++ is faster

Posted Jun 18, 2008 21:29 UTC (Wed) by flewellyn (subscriber, #5047) [Link]

In CL, if you want compile-time dispatching, you just declare the types of the method
arguments using DECLARE.  Then the compiler sees that and "hard-wires" the method dispatch.

Sometimes C++ is faster

Posted Jun 18, 2008 21:51 UTC (Wed) by ncm (subscriber, #165) [Link]

Does CL support overloading?  Or would you need to give the functions DECLAREd with different
argument types different names?

Sometimes C++ is faster

Posted Jun 18, 2008 21:58 UTC (Wed) by flewellyn (subscriber, #5047) [Link]

You would DECLARE the types of the arguments in the calling function.  Then (in some
implementations, anyway) the compiler would see that the arguments to the generic function are
of a particular type, and do the dispatch at compile-time.  No changes to the called GF
required.

Sometimes C++ is faster

Posted Jun 18, 2008 21:10 UTC (Wed) by ncm (subscriber, #165) [Link]

I guess I should mention that this sort of compile-time dispatching is natural, without
macros, in ML variants and in Haskell.  Such languages can be good for coding compilers, given
an implementation with a GC that respects cache locality.  Recoding Gcc in Eager Haskell, it
might take years before you got anything useful, but you'd learn a lot, and have a better
sense of what the successor to Haskell should look like.

Sometimes C++ is faster

Posted Jun 18, 2008 21:31 UTC (Wed) by flewellyn (subscriber, #5047) [Link]

Mmh, well, I was hardly recommending that anybody rewrite GCC in Common Lisp.  While I'm sure
someone could do it, the result would be a hell of a lot harder to bootstrap.  :-)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds