How to override opCmp when two different sorts are needed?
Regan Heath
regan at netmail.co.nz
Wed Aug 15 03:02:35 PDT 2007
Deewiant wrote:
> Regan Heath wrote:
>> C. Dunn wrote:
>>> It's not just a matter of elegance. C++ std::sort() is way, way
>>> faster than qsort in cstdlib because the comparison function gets
>>> inlined. Sorting is one place where inlining is critical.
>> I believe the intention for D is that the optimizer will automagically
>> inline everything it can.
>>
>
> I'd imagine it's quite hard to inline a delegate or function pointer. In any
> case, the current optimizer is a joke in many ways.
True about delegates/fpointers I reckon.
I don't know why people keep hating on the optimizer. Isn't it the same
optimizer used in DMC, a C compiler with 25 odd years of history? Sure,
it's not 'optimized' for D specifically but it still holds it's own in
shootout results in the general sense, right?
> Functions, on the other hand, can be inlined, which is why std::sort and D's
> built-in sort beat C's qsort (assuming they use approximately the same
> algorithm): they're hard-coded to use "a < b", which is either a "cmp" x86
> instruction or an inlineable call to a.opCmp(b) which usually translates to "int
> < int" and thus a "cmp" anyway.
>
> This is why I recently suggested
> (http://www.dsource.org/projects/tango/ticket/571) that Tango provide for
> something like:
>
> enum Dir : bool { Ascending, Descending }
>
> // I forget the correct syntax, sorry if this is wrong
> template sort(T, Dir D = Dir.Ascending) {
> bool compare(T a, T b) {
> static if (D == Dir.Ascending)
> return a < b;
> else
> return a > b;
> }
>
> void sort(T[] a) {
> // sort using compare for comparing
> }
> }
>
> This allows the user to choose whether to sort in ascending or descending order,
> and improves speed noticeably. Currently Tango uses a functor struct:
>
> struct IsLess(T) {
> bool opCall(T a, T b) {
> return a < b;
> }
> }
>
> Which is not inlined. A "call" instruction is generated for every comparison.
Instead of limiting it to opCall why not define an Interface 'Sortable'
and define a compare method which takes a mode flag and "Sortable rhs"
or similar.
Regan
More information about the Digitalmars-d
mailing list