Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded resources - resx - performance - C#

Embedded resources is only used to compile a file as binary part?

Is it a good idea to use embedded resources for performances reasons?

This question concerns

  • XML files saved as embedded resources
  • Resx files (strings)

In the two cases, is there same performance benefit: some caching strategy, or some thing like that (since it's could be managed like sources code/assemblies).

For exemple instead of the resx, I can use a hash-table (lately created). In my case that hashtable could be so big to stay forever in memory. So Resx is it a help with some cache strategy ?

I have the same problem for a tree object. Using it from an XML embedded-file couldn't help me, or should I implement all the cache-strategy?

THINKS

like image 537
minchiya Avatar asked Oct 28 '25 00:10

minchiya


1 Answers

The embedded resource feature was added primarily because of performance reasons. There is no way to do it faster on a demand-page virtual memory operating system like Windows. You get the full benefit of a memory-mapped file to read the resource content.

That is not massively better than reading a separate file but you don't pay for having to find the file. Which is usually the costly operation on small resources and heavily affects the cold-start time of an app. Not having a large blob of files to deploy is very much a practical advantage.

They do occupy memory of course but it is virtual memory. Just numbers to the processor, one each for every 4096 bytes. Also the cheap kind of virtual memory, it is backed by the executable file instead of the paging file. You don't actually pay and start to use RAM until you access the resource in your program. That RAM will usually be released again soon unless you repeatedly use the resource. It does set an upper limit on the amount of resource data you can embed, peters out at 2 gigabytes.

That they start as an XML resource only matters at build time, the Resgen.exe tool turns it into a binary blob before it is embedded in the executable file. The advantage of XML is that it plays nice with the IDE and makes it easy to recover the original resource after you lost track of the original art work.

like image 193
Hans Passant Avatar answered Oct 29 '25 13:10

Hans Passant