How to embed a readonly table in dll
My dll file requires some codepage conversion maps. Some strings need to be converted char by char. Currently I created several huge Dictionaries that hold these maps. These Dictionaries are so large that I had to split them up into 8 Dictionaries else C# didn't allow me to compile. Now it takes several seconds to load the application and it uses too much memory.
I would like this to be a bit more efficient. Maybe I can embed a (or several) table in my dll and query it with sql or linq or something. Now I know a dll is not for storing data but my clients will appreciate a single .dll file. The table would only need to be readonly and hold ~62000 rows.
Additional information:
I have 10 codepages I need to convert to unicode AND back.
6 of these codepages are single byte codepages and not a big deal. (6 * 256 * 2 = 3072 ite开发者_运维问答ms)
4 of these are double byte codepages, CP932, CP950, CP936 AND CP949, makes ~62000 * 2 = 124000 items
I could leave out the first 128 chars of any charset, but that doesn't make much sense.
If I could save this in a queryable something I only would have to save ~62000 + 1536 records and use them for converting both ways.
Looking up chars in the current dictionaries is quite fast. The memory usage does not matter to much, the startup time bothers me most.
The lookup has to be fast, the worst case scenario is that I have to convert about 20 sentences a second. Delay is not desirable, it has to keep up.
Is it possible and what would be the best way to achieve this?
Embedding the table in a resource file does not help with memory consumption since when an assembly is accessed, it has to be loaded onto memory - although not always the whole file is loaded but as soon as you access the resource, it all gets loaded.
Best solution that comes to my mind is a Sqlite or Sql Server Compact file which is sent to the customer along with DLL and contains the data. Querying the local db file will reduce your memory consumption.
You could use the resource file for storing you data when you will compile application will build in a single dll msdn
I hope I'm not misunderstanding your needs, but maybe you should consider using a big array instead of a dictionary. Assuming 16-bit unicode code points are all that you'd need to handle (surrogate pairs throws a wrench in everything), just create a 65,536 char table.
Such a table would take 128K if you're mapping from a char to a char, or more if you need to map to other information. However, you won't be paying for overhead for internal dictionary data structures, and you can declare the array declaratively:
private char[] mymap = new char[65536] { ... // 65536 things }
I believe this should load far quicker than loading a ton of data into a dictionary.
Again, if this doesn't meet your needs, please provide more context on how you're currently converting, and I'll gladly either edit or delete.
精彩评论