如何防止拆箱 - 读取任意 sql 行时装箱内存开销
我正在编写一个类来表示 SQL 查询中的一行。我希望通过类的索引器属性访问字段数据。如果我将数据加载到内部对象列表中,这就足够简单了。我已经尝试过这个,并且对原始的拳击不满意。拳击将内存需求增加了 20%。我想将基元作为基元存储在类中。 DataTable 类通过为从 IDataReader 返回的架构中的每一列创建数组来存储基元。我以这种方式实现了一个类,但我更喜欢将数据与行对象一起存储,而不是存储在行内部引用的列中。
有什么想法可以实现这一点吗?
I'm writing a class to represent a row from a SQL query. I want the field data to be accessed via the indexer property of the class. This is straightforward enough if I load the data into an internal List of Object. I've already tried this and am not happy with the boxing for the primitives. Boxing increases the memory requirement by 20%. I would like to store the primitives as primitives in the class. The DataTable class stores primitives by creating arrays for each column in the schema returned from IDataReader. I implemented a class this way but I would prefer the data be stored with the row object rather than in a column that is referenced internally by the row.
Any ideas on accomplishing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以生成结构类型来表示行。它可以动态地完成,并且可以做到这样就不会有任何装箱。我不确定这是否值得付出努力
You can generate structure type to represent a row. It can be done dynamically and it is possible to do it so there will not be any boxing. I am not sure it is worthy the effort though
只有20%的开销?你很幸运!我刚刚清理了一些代码,其中性能下降了四十倍(不计算额外内存的影响)!无论如何,防止使用
object
的典型方法是开始使用泛型。这是一个简化的示例:Only 20% overhead? You're lucky! I just cleaned up some code where it added a fourty-fold performance decrease (not counting the impact of the extra memory)! Anyway, the typical way to prevent using
object
is by starting using generics. Here's a simplified example: