Does the C ++ standard provide an implementation for pooling resources?

I watched a video by Jonathan Blow Ideas for a new programming language for games , in which he discusses a general scheme in games, which he calls "joint distribution. The idea is that you have a class with several members that are dynamically distributed arrays ( It can be std::vector , but since they are fixed in size, more like the proposed std::dynarray ), you pre-allocate enough memory to store the entire data file and execute only one allocation large enough for All arrays, rather than for each array.

It offers direct language support for this template, which made me wonder if the C ++ standard allows implementations this way? It seems to me that this will require a heroic effort from the compiler to actually implement it as an optimization, but I see no obvious reason why this cannot be done in principle. Does anyone know if this will not be allowed by the standard, or even if there are already implementations that do this optimization in practice?

+5
source share
1 answer

Yes, the standard allows you to combine distributions (C ++ 14):

5.3.4 New [expr.new]

10 Implementation allows to omit the call of the shift global distribution function (18.6.1.1, 18.6.1.2). When this is done, the storage is instead provided by the implementation or provided by expanding the allocation of another new expression. An implementation can expand the allocation of a new expression e1 to provide storage for a new expression e2 if the following were true: the distribution was not expanded:

  • score e1 sequenced to score e2 and
  • e2 is evaluated whenever e1 gets memory, and
  • both e1 and e2 call the same replaceable global distribution function, and
  • if the allocation function called by e1 and e2 throws, any exceptions caused by evaluating either e1 or e2 will first fall into the same handler, and
  • the pointer values โ€‹โ€‹generated by e1 and e2 are operands for the evaluated exception expressions and
  • the evaluation of e2 sequenced before the evaluation of the delete expression, whose operand is the pointer value created by e1 .

C ++ 11 did not allow such distributions to be coalesced or omitted.

+10
source

Source: https://habr.com/ru/post/1206742/


All Articles