使用资源文件 (.resx) 时的性能开销 [英] Performance Overheads when Using Resource Files (.resx)

查看:51
本文介绍了使用资源文件 (.resx) 时的性能开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我知道有关此主题的以下问题:

  1. 有没有表现资源 (.resx) 文件存在问题或警告?

  2. 是否保留字符串资源 (.resx) 属性记忆?

等.但是,我觉得这些问题的答案都不令人满意(不够具体).

我也知道 MSDN 有关此主题的页面,但这些页面似乎也忽略了有关使用资源文件的开销的技术信息.

<小时>

我的困境是我们即将着手对一个相当大的 WinForms 应用程序进行本地化.例如,我在此阶段关注的是从嵌套循环中访问 .resx 文件中的资源的性能.目前,对于我们本地化的一小部分代码(DataGridView 等的列名称、行标题等),我们将资源兑现为相关类的全局变量并使用这些资源.

如何访问 .resx 中的资源(它们是否在编译时包含在程序集中?)以及将这些资源兑现并使用全局变量进行访问是否有性能优势?>

解决方案

字符串资源缓存在内存中.查看在 "Resources.Designer.cs" 中生成的代码.

它使用 System.Resources.ResourceManager,这会缓存字符串.

另请注意此 ResourceManager 构造函数.它提到您可以更改缓存策略:

<块引用>

此构造函数使用系统提供的 ResourceSet 实现.要使用自定义资源文件格式,您应该从ResourceSet 类,覆盖 GetDefaultReader 和 GetDefaultWriter方法,并将该类型传递给 ResourceManager(String, Assembly,类型)构造函数.使用自定义资源集对于很有用控制资源缓存政策或支持您自己的资源文件格式,但通常不是必需的.

(我的重点)

ResourceSet 的文档明确说明:

<块引用><块引用>

ResourceSet 类枚举 IResourceReader,加载每个名称和值,并将它们存储在 Hashtable 中

所以我们知道默认情况下您将获得的确切缓存策略.

因为你似乎不相信我!:)

(1)查看构造函数的文档ResourceManager(string baseName,Assembly assembly).它指出:

<块引用><块引用>

此构造函数使用系统提供的 ResourceSet 实现.

(2) 现在查看 ResourceSet 文档.它指出:

<块引用><块引用>

ResourceSet 类枚举 IResourceReader,加载每个名称和值,并将它们存储在 Hashtable 中.

因此,这种缓存行为确实记录在 MSDN 中,此外您可以通过使用 Resharper 检查实现来验证这是发生了什么.

Note, I am aware of the following questions on this topic:

  1. Are there any performance issues or caveats with resource (.resx) files?

  2. Are string resources (.resx) properties kept in memory?

et al. However, I don't find any of the answers in these questions satisfactory (they are not concrete enough).

I am also aware of the MSDN pages on this topic, but these also seem to skimp on the technical information regarding the overheads of using resource files.


My predicament is that we are about to embark on the localisation of a reasonably large sized WinForms application. My concern at this stage is about the performance of accessing resources from a .resx file from within a nested loop for example. Currently for the small part of the code we have localised (Column Names, Row Headers etc. for DataGridView etc.) we are cashing the resources in global variables of the relevant class and using those.

How are resources from the .resx accessed (are they included in the assembly at compile-time?) and is there a performance benefit from cashing those resources and using global variables for access?

解决方案

String resources are cached in memory. Look at the code that's generated in "Resources.Designer.cs".

It uses a System.Resources.ResourceManager, and this does caching of the strings.

Also note this ResourceManager constructor. It mentions that you can change caching strategy:

This constructor uses the system-provided ResourceSet implementation. To use a custom resource file format, you should derive from the ResourceSet class, override the GetDefaultReader and GetDefaultWriter methods, and pass that type to the ResourceManager(String, Assembly, Type) constructor. Using a custom ResourceSet can be useful for controlling resource caching policy or supporting your own resource file format, but is generally not necessary.

(my emphasis)

The documentation for ResourceSet explicitly says:

The ResourceSet class enumerates over an IResourceReader, loading every name and value, and storing them in a Hashtable

So we do know the exact caching strategy that you'll get by default.

[EDIT] Since you don't seem to believe me! :)

(1) Look at the documentation for the constructor ResourceManager(string baseName,Assembly assembly). It states:

This constructor uses the system-provided ResourceSet implementation.

(2) Now look at the documentation for ResourceSet. It states:

The ResourceSet class enumerates over an IResourceReader, loading every name and value, and storing them in a Hashtable.

Therefore this caching behaviour is indeed documented in MSDN, and additionally you can verify that this is what is happening by using Resharper to inspect the implementation.

这篇关于使用资源文件 (.resx) 时的性能开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆