高度可变域中的不可变功能对象 [英] Immutable functional objects in highly mutable domain

查看:78
本文介绍了高度可变域中的不可变功能对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在正在Scala工作中学习功能性编程,我有一个空闲的新手问题。我可以看到有不可变对象的优雅做一些像计算Haar小波变换的东西 - 即当数据本身由对象表示时不会改变。



但是我看到一个有一个小游戏的博客作为展示不变性的例子。如果一个生物对象收到伤害,它没有改变它的状态 - 它返回一个新的生物对象与新的命中点和一个新的攻击X标志。但是如果我们设计像MMORPG这样的东西,魔兽世界说。战场上的一百名玩家可能会以不同的方式影响他们的成千上万的攻击和抛光/削弱法术效果。使用完全不变的对象来设计系统还是可行的吗?对我来说,似乎会有一个巨大的新例子,每个tick。为了获得目前有效的对象实例,所有的客户端都会不断地经历某种中央的gameworld对象,或者???????

或者这是最好的工具,最好的工具,可能不是不可变的的情况?

解决方案


<对我来说,似乎每个tick都会有一个很大的新例子。


的确,这是案件。我有一个Haskell应用程序读取市场数据Feed(在六个小时交易日的大约五百万条消息,对于我们感兴趣的数据),并为各种事情保持当前状态,例如最新的报价和报价价格和数量的仪器,我们的模型如何适应市场等等。相当可怕的是模拟​​一个记录的饲料在分析模式下运行,并观察它的分配和GC接近288在其运行的前500秒内,GB的内存(或接近我机器RAM的大小的50,000倍)。 (没有分析,这个数字会相当高,因为分析不仅会减慢应用程序的运行速度,还会迫使所有人运行在一个核心上。)



但是请记住,纯语言实现中的垃圾收集器针对这种行为进行了优化。我对我的应用程序的总体速度感到非常高兴,我认为这是相当苛刻的,因为我们必须从市场资源中分析每秒数百条消息,做一些相当广泛的计算来构建我们的模型,并使用它模型,以尽快生成订单去交换。


I'm currently learning functional programming in my spare time with Scala, and I have an idle newbie question.

I can see the elegance of having immutable objects when doing something like calculating a Haar wavelet transform - i.e. when the data itself being represented by the objects doesn't change.

But I saw a blog where someone had a small game as an example when demonstrating immutability. If a creature object recieved damage, it didn't change its state - it returned a new creature object with the new hitpoints and a new "aggro towards X" flag. But if we were to design something like a MMORPG, World of Warcraft say. A hundred players in a battleground... possibly thousands of attacks and buffing/debuffing spell effects affecting them in different ways. Is it still possible to design the system with completely immutable objects? To me it would seem like there would be a ginormous swarm of new instances each 'tick'. And to get the currently valid instance of objects, all clients would constantly have to go through some sort of central "gameworld" object, or?

Does functional programming scale for this, or is this a case of "best tool for best job, probably not immutable here"?

解决方案

To me it would seem like there would be a ginormous swarm of new instances each 'tick'.

Indeed, that is the case. I have a Haskell application that reads a market data feed (about five million messages over the course of a six-hour trading day, for the data in which we're interested) and maintains "current state" for various things, such as the most recent bid and offer prices and quantities for the instruments, how well our model fits the market, etc. etc. It's rather frightening to simulate a run of this program against a recorded feed in profiling mode and watch it allocate and GC close to 288 GB of memory (or close to 50,000 times the size of my machine's RAM) in the first 500 seconds of its run. (The figure would be considerably higher without profiling, since profiling not only slows down the application, but also forces it all to run on one core, as well.)

But keep in mind, the garbage collector in pure language implementations is optimized for this sort of behavior. I'm quite happy with the overall speed of my application, and I think that it's fairly demanding, in that we have to parse several hundred messages per second from the market feed, do some fairly extensive calculations to build our model, and use that model to generate orders to go to the exchange as quickly as possible.

这篇关于高度可变域中的不可变功能对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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