Jump to page: 1 2
Thread overview
Non-recursive maxSizeOf
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Ali Çehreli
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Ali Çehreli
Aug 06, 2020
Adam D. Ruppe
Aug 06, 2020
lithium iodate
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Adam D. Ruppe
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Adam D. Ruppe
Aug 06, 2020
H. S. Teoh
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Per Nordlöw
Aug 06, 2020
Adam D. Ruppe
Aug 06, 2020
H. S. Teoh
August 06, 2020
Is it possible to implement

template maxSizeOf(T...)
{
    static if (T.length == 1)
        enum size_t maxSizeOf = T[0].sizeof;
    else
    {
        enum size_t firstSize = T[0].sizeof;
        enum size_t maxSizeRest = maxSizeOf!(T[1 .. $]);
        enum size_t maxSizeOf = firstSize >= maxSizeRest ? firstSize : maxSizeRest;
    }
}

in a non-recursive way?
August 05, 2020
On 8/5/20 5:58 PM, Per Nordlöw wrote:
> Is it possible to implement
> 
> template maxSizeOf(T...)
> {
>      static if (T.length == 1)
>          enum size_t maxSizeOf = T[0].sizeof;
>      else
>      {
>          enum size_t firstSize = T[0].sizeof;
>          enum size_t maxSizeRest = maxSizeOf!(T[1 .. $]);
>          enum size_t maxSizeOf = firstSize >= maxSizeRest ? firstSize : maxSizeRest;
>      }
> }
> 
> in a non-recursive way?

Boring in D. :p

template maxSizeOf(T...) {
  enum maxSizeOf = compute();

  auto compute() {
    size_t result;
    static foreach (t; T) {
      if (t.sizeof > result) {
        result = t.sizeof;
      }
    }
    return result;
  }
}

void main() {
  pragma(msg, maxSizeOf!(double, char, string));
}

Ali

August 06, 2020
On Thursday, 6 August 2020 at 00:58:39 UTC, Per Nordlöw wrote:
> Is it possible to implement
>
> in a non-recursive way?

It is very easy too... just write an ordinary function:

size_t maxSizeOf(T...)() {
        size_t max = 0;
        foreach(t; T)
                if(t.sizeof > max)
                        max = t.sizeof;
        return max;
}

pragma(msg, maxSizeOf!(int, char, long));
August 06, 2020
On Thursday, 6 August 2020 at 01:13:41 UTC, Adam D. Ruppe wrote:
> size_t maxSizeOf(T...)() {
>         size_t max = 0;
>         foreach(t; T)
>                 if(t.sizeof > max)
>                         max = t.sizeof;
>         return max;
> }
>
> pragma(msg, maxSizeOf!(int, char, long));

more love for phobos pls

template maxSizeOf(T...)
{
    template sizeOf(T) { // doesn't exist in phobos?
        enum sizeOf = T.sizeof;
    }
    enum size_t maxSizeOf = maxElement([staticMap!(sizeOf, T)]);
}
August 06, 2020
On Thursday, 6 August 2020 at 01:13:28 UTC, Ali Çehreli wrote:
> Boring in D. :p
>
> template maxSizeOf(T...) {
>   enum maxSizeOf = compute();
>
>   auto compute() {
>     size_t result;
>     static foreach (t; T) {
>       if (t.sizeof > result) {
>         result = t.sizeof;
>       }
>     }
>     return result;
>   }
> }

I forgot to say that I was looking for a solution that used `static foreach` but doesn't trigger CTFE...
August 06, 2020
On Thursday, 6 August 2020 at 01:17:51 UTC, lithium iodate wrote:
> more love for phobos pls
>
> template maxSizeOf(T...)
> {
>     template sizeOf(T) { // doesn't exist in phobos?
>         enum sizeOf = T.sizeof;
>     }
>     enum size_t maxSizeOf = maxElement([staticMap!(sizeOf, T)]);
> }

`std.meta.staticMap` is defined recursively....
August 06, 2020
On Thursday, 6 August 2020 at 01:13:41 UTC, Adam D. Ruppe wrote:
> It is very easy too... just write an ordinary function:
>
> size_t maxSizeOf(T...)() {
>         size_t max = 0;
>         foreach(t; T)
>                 if(t.sizeof > max)
>                         max = t.sizeof;
>         return max;
> }
>
> pragma(msg, maxSizeOf!(int, char, long));

How does the memory usage and speed of this code compare to the variant that uses template instantiations? In general? Any experiences on this?
August 06, 2020
On Thursday, 6 August 2020 at 01:23:33 UTC, Per Nordlöw wrote:
> How does the memory usage and speed of this code compare to the variant that uses template instantiations?

I haven't tested this specifically, but similar tests have come in at like 1/10th the memory and compile time cost.

There is one template instance here per argument list though, and that template includes the implementation function. The eponymous version Ali posted will perform a bit better than my version because the compiler can see it will never need code generation.

Still for small argument lists the Phobos one might do better, but for larger ones, the ctfe version should be much better. So on average Ali's version is almost certain to win in my experience.
August 06, 2020
On Thursday, 6 August 2020 at 01:17:51 UTC, lithium iodate wrote:
> more love for phobos pls

That would add a lot to the cost and bring no real benefit....
August 06, 2020
On Thursday, 6 August 2020 at 01:13:28 UTC, Ali Çehreli wrote:
> Boring in D. :p
>
> template maxSizeOf(T...) {
>   enum maxSizeOf = compute();
>
>   auto compute() {
>     size_t result;
>     static foreach (t; T) {
>       if (t.sizeof > result) {
>         result = t.sizeof;
>       }
>     }
>     return result;
>   }
> }

Thanks. I'm gonna benchmark this against my templated solution.
« First   ‹ Prev
1 2