Re: [webkit-dev] Compile error in WebGL2RenderingContext.cpp
On Thu, Jan 17, 2019 at 2:27 AM, Salisbury, Mark <mark.salisbury@hp.com> wrote:
Thanks Tim!
I didn’t know about C++ variable length arrays. That makes perfect sense now.
Looks like people have been requesting Visual Studio add support for VLAs; there are no indications they will be supported.
We shouldn't be using VLAs in WebKit. It's not standard C++, and even if it was, it's not safe. The code should use std::array or perhaps WTF::Vector. Michael
Vector’s inline capacity feature was originally created as an alternative to variable length arrays for most of the purposes people would want to put them. Imagine, for example, that you need a variable length buffer of characters that is almost always going to be less then 32 bytes. You write this: Vector<char, 32> buffer; You can then grow the buffer to whatever size you need. If it’s less than 32 bytes then it uses space on the stack, and if more than 32 bytes it uses space on the heap. The reason this is better than variable length arrays is that stack size often has a inflexible total limit, so it’s not OK in general to use an arbitrary amount of stack. The vector inline capacity allows us to easily use up to a fixed amount of stack, but then still correctly handle unusually large requests by spilling go the heap. For that WebGL2 code the idiom would be something like this: Vector<GC3Dint, 8> parameters; parameters.grow(numValues); Not sure we have good Vector inline capacity documentation, but that’s a basic primer. — Darin
On Thu, Jan 17, 2019 at 11:12 AM, Darin Adler <darin@apple.com> wrote:
Vector’s inline capacity feature was originally created as an alternative to variable length arrays for most of the purposes people would want to put them.
Any advantages of this over std::array (which is widely-used in WebKit)?
On Jan 17, 2019, at 10:15 AM, Michael Catanzaro <mcatanzaro@igalia.com> wrote:
On Thu, Jan 17, 2019 at 11:12 AM, Darin Adler <darin@apple.com> wrote:
Vector’s inline capacity feature was originally created as an alternative to variable length arrays for most of the purposes people would want to put them.
Any advantages of this over std::array (which is widely-used in WebKit)?
std::array is fixed size. So you can’t do Darin’s described trick of using the stack for small enough cases but falling back to the heap for large cases, for a buffer whose size is not known at compile time. std::array seems fine to use for cases that are known to be fixed size at compile time. I would imagine it saves space by not having to separately track size and capacity. I don’t know if std::array holds its values inline or in an external heap buffer always. If it always uses a heap buffer, then it’s probably worse than a normal C array for strictly local buffers. It could still be ok for fixed-size arrays stored in objects. Regards, Maciej
participants (3)
-
Darin Adler
-
Maciej Stachowiak
-
Michael Catanzaro