Skip to content

Reduce the amount of unnecessary heap allocations while parsing string tables and fields - #34

Open
Ovahlord wants to merge 3 commits into
wowdev:masterfrom
Ovahlord:master
Open

Reduce the amount of unnecessary heap allocations while parsing string tables and fields#34
Ovahlord wants to merge 3 commits into
wowdev:masterfrom
Ovahlord:master

Conversation

@Ovahlord

Copy link
Copy Markdown

Right now DBCD is quite horrendous when it comes to memory usage as about 100mb DBC data can easily bloat into 400mb+ RAM usage during runtime.

Part of if is because of excessive use of reflection, some because of unnecessary long living objects.

However, my current focus was on the speed part as I have noticed that the garbage collector goes nuts while loading storages as there were lots of unneeded heap allocations which triggered Gen0 quite frequently.

This PR focuses on string tables and field parsing. Using modern .NET features, we now stack allocate temporary buffers to read bytes. Additionally, we no longer double-allocate strings when loading the string tables, which eases up the GC pressure a bit.

var curOfs = 0;
var decoded = Encoding.UTF8.GetString(reader.ReadBytes(stringTableSize));
foreach (var str in decoded.Split('\0'))
Span<byte> stringTableBytes = stackalloc byte[stringTableSize];

@Fabi Fabi Jul 31, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not stackalloc such possibly big arrays. if you are crazy you can go up to 1MB (and if you are really insane 4MB), but otherwise stay under it. That is not an option here.

Comment thread DBCD.IO/Extensions.cs Outdated
var curOfs = 0;
var decoded = Encoding.UTF8.GetString(reader.ReadBytes(stringTableSize));
foreach (var str in decoded.Split('\0'))
Span<byte> stringTableBytes = stackalloc byte[stringTableSize];

@Fabi Fabi Jul 31, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above and more at the bottom

@Marlamin

Copy link
Copy Markdown
Contributor

@Ovahlord

Ovahlord commented Aug 1, 2026

Copy link
Copy Markdown
Author

Will refactor the string table stack alloc to use ArrayPool instead later so we re-use arrays then

…lloc to prevent possible stack overflows when parsing gigantic amounts of strings at once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants