如何防止拆箱 - 读取任意 sql 行时装箱内存开销

发布于 2024-08-13 14:06:54 字数 242 浏览 2 评论 0原文

我正在编写一个类来表示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2024-08-20 14:06:54

您可以生成结构类型来表示行。它可以动态地完成,并且可以做到这样就不会有任何装箱。我不确定这是否值得付出努力

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

凉风有信 2024-08-20 14:06:54

只有20%的开销?你很幸运!我刚刚清理了一些代码,其中性能下降了四十倍(不计算额外内存的影响)!无论如何,防止使用 object 的典型方法是开始使用泛型。这是一个简化的示例:

class RowRepresenter<T>
{
    //....
    public T this[int index] {get; set;}  // implementation left out
}

// initialize, suppose the properties (indexers) should be ints:
RowRepresenter<int> myInstance = new RowRepresenter<int>();
myInstance.LoadData();

// get data (i.e., your indexer)
int somefield = myInstance[2];   // no overhead, no casting required

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:

class RowRepresenter<T>
{
    //....
    public T this[int index] {get; set;}  // implementation left out
}

// initialize, suppose the properties (indexers) should be ints:
RowRepresenter<int> myInstance = new RowRepresenter<int>();
myInstance.LoadData();

// get data (i.e., your indexer)
int somefield = myInstance[2];   // no overhead, no casting required
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文