[gdal-dev] Starting a discussion on style and coding guidelines

Andrew Bell andrew.bell.ia at gmail.com
Thu May 5 13:56:34 PDT 2016


On Thu, May 5, 2016 at 10:19 AM, Kurt Schwehr <schwehr at gmail.com> wrote:

> Thanks all for your comments!  I will try to write up a similar proposal
> for C++11 based on what people have written.
>
> This local stack storage change is for a real need, so it's probably good
> that I add a use case that explains why this change is important.  And I
> >>believe<< (but am not 100% sure) that using std::vector is the strongest
> solution for the majority of instances of large buffers on the stack in
> gdal.
>
> Use case:  (note that I can only share generals)
>
> At Google, we have jobs that use GDAL running with thousands of cores and
> >20 threads per process that run for days.  The overhead of having to allow
> for bumping the stack size adds substantial overhead to all those jobs.
> GDAL is the only thing driving the requirement for the larger stack.  For
> people using GDAL on a desktop machine, the larger stack is unlikely to be
> a real issue.  For anyone running large multithreaded jobs in the cloud,
> the large stack is a real cost issue.  The overhead of vector and heap
> allocation in all these cases I've looked at in GDAL so far is far smaller
> than the cost from the stack size overhead.
>

I misinterpreted this thread because of the title.  It seems this isn't
really about code style or C++ 11.  It's really about Google wanting to use
heap over stack because of its particular use of the library.  There is no
need for C++ 11 for that and it would seem that doing a compile-time
policy-based array isn't too hard (proof of concept below).  Perhaps Google
could flush something like this out to its liking and replace current stack
allocations that cause it issues.  C++ 11 and code style seem another
conversation.

======

#include <iostream>

struct Stack
{};

struct Heap
{};

template <typename TYPE, std::size_t SIZE, typename AllocType>
struct Arr
{
};

template <typename TYPE, std::size_t SIZE>
struct Arr<TYPE, SIZE, Heap>
{
    Arr() : m_instance(new TYPE[SIZE ? SIZE : 1])
    {}

    ~Arr()
    {
        delete [] m_instance;
    }

    TYPE *m_instance;

    TYPE& operator[](std::size_t idx)
        { return *(m_instance + idx); }
};

template <typename TYPE, std::size_t SIZE>
struct Arr<TYPE, SIZE, Stack>
{
    TYPE m_instance[SIZE ? SIZE : 1];

    TYPE& operator[](std::size_t idx)
        { return m_instance[idx]; }
};


#ifdef HEAPALLOC
template <typename T, std::size_t SIZE>
struct Array : public Arr<T, SIZE, Heap>
{};
#else
template <typename T, std::size_t SIZE>
struct Array : public Arr<T, SIZE, Stack>
{};
#endif

int main()
{
    Array<int, 25> a;

    a[10] = 15;
    std::cout << "A[10] = " << a[10] << "!\n";

    return 0;
}

-- 
Andrew Bell
andrew.bell.ia at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20160505/515d92a5/attachment-0001.html>


More information about the gdal-dev mailing list