SOA AOS 面向对象与面向数据

转载自 https://blog.csdn.net/debugconsole/article/details/17788979

SOA 数组的结构与AOS结构的数组,是面向数据和面向对象设计的区别之一。

在需要高频率(如渲染循环中)访问数据的时候,一般情况下SOA的效率高于AOS,因为将需要频繁访问的数据连续存放会大大提高访问速度。虽然AOS的结构可能更适合面向对象设计,但是在高度依赖效率的地方应该使用SOA。

引用大家都在说的一句话:一开始写程序用struct (c方式的编程,面向数据的编程) 后面开始用class (为了改善程序结构,OO方式的编程) 最后又开始用struct(为了性能!)

 

引用一篇文章来说明SOA 与 AOS(面向数据设计与面向对象设计) 两种方式的区别

My understanding of Data Oriented Design is that it is about organizing your data for efficient processing. Especially with respect to cache misses etc. Data Driven Design on the other hand is about letting data control a lot of your programs behavior

 

Say you have ball objects in your application with properties such as color, radius, bounciness, position etc. In OOP you would describe you balls like this:

class Ball {
  Point  pos;
  Color  color;
  double radius;
 
  void draw();
};
And then you would create a collection of balls like this:

vector<Ball> balls;
In Data Oriented Design however you are more likely to write the code like this:

class Balls {
  vector<Point>  pos;
  vector<Color>  color;
  vector<double> radius;
 
  void draw();
};
As you can see there is no single unit representing one Ball anymore. Ball objects only exist implicitly. I don't want to rewrite the article so I am not going to go into detail why one does it like this, but it can have many advantages performance wise. Usually we want to do operations on many balls at the same time. Hardware usually want large continuous chunks of memory to operate efficiently. Secondly you might do operations that affects only part of a balls properties. E.g. if you combine the colors of all the balls in various ways, then you want your cache to only contain color information. However when all ball properties are stored in one unit you will pull in all the other properties of a ball as well. Even though you don't need them.

Say a ball each ball takes up 64 bytes and a Point takes 4 bytes. A cache slot takes say 64 bytes as well. If I want to update the position of 10 balls I have to pull in 10*64 = 640 bytes of memory into cache and get 10 cache misses. If however I can work the positions of the balls as separate units, that will only take 4*10 = 40 bytes. That fits in one cache fetch. Thus we only get 1 cache miss to update all the 10 balls. These numbers are arbitrary I assume a cache block is bigger.

But it illustrates how memory layout can have severe effect cache hits and thus performance. This will only increase in importance as the difference between CPU and RAM speed widens.
 ———————————————— 
版权声明:本文为CSDN博主「凭谁问」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/debugconsole/article/details/17788979

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值