Idea about memory management. Game related.
Chad J
gamerChad at _spamIsBad_gmail.com
Sun Jun 11 20:05:56 PDT 2006
MM wrote:
> In article <e5ql8i$2abs$2 at digitaldaemon.com>, Walter Bright says...
>
>>AgentOrange wrote:
>>
>>>But what about array
>>>operations? Should one avoid using D arrays as well?
>>
>>I don't see why one would avoid them.
>
>
> Yes, could anybody pls explain why one should avoid using arrays for 'real'-time
> threads?
> (Also interested in creating a game engine in D)
>
>
byte[] A = new byte[128];
byte[] B = new byte[64];
A ~= B;
During the above concatenation, A will need to have 64 more bytes
allocated for it to hold the extra elements from B. Walter has said
that when you allocate memory in D, with possible exceptions like that
of C's malloc, a garbage collection may happen. Therefore, a simple
concatenation such as this could cause a garbage collection that may
pause your game for seconds. Actual time may vary, but it could be
unacceptable.
You might be able to reduce the frequency of garbage collections by
overallocating arrays, perhaps by doing something like
A.length = neededLength;
or
A.length = A.length * 2;
before you run your real-time code. Then the copy of B would fit in A
and no alloc would be needed. You could eliminate the threat of a
collection entirely by ensuring that no operations requiring new memory
are called unless there is enough memory available to complete them
without allocation.
I think you should be fine using arrays as long as you are careful with
them.
More information about the Digitalmars-d
mailing list