Python 子对象 inplace 升级成父对象,有啥高性能但更好懂的方法吗?

2016-10-25 21:57:14 +08:00
 yupbank

Hi ,我来请教一个问题呗。

class Old(object):
    def  __init__(self):
        self.current = {}
        self.new_added = []

    def keep_history(self, key):
        if key in self.filed:
            self.current[key] = self.field[key]
        else:
            self.new_added.append(key)
    def born(self):
        self.son = Normal(self._fields)
        self._fields = None
        return self.son


class Normal(object):
    def __init__(self, field=None):
        self.filed = filed or {}

    def mutate(self, key, value):
        self.aging()
        self.keep_history(key)
        self._filed[key] = value
        
        return self.born()
    
    def aging(self):
         self.__class__ = Old
         self.__init__()

现在是这种设计,想 Normal 对象在 mutate 的时候,保留一份 最老-》比较老-》年轻 的记录

当前据说考虑 GC 的顺序,是老对象指向新对象,不因为最新对象而阻碍了老对象回收。

没用 weak ref 是因为 weak ref 开销大。。。

但这样的代码确实不美观。。 我在想如何写的性能又高,代码又好读。。

请各位大神指点。。。

1643 次点击
所在节点    Python
1 条回复
yupbank
2016-10-25 22:42:28 +08:00
use case of Normal and Old.
to retain change history and able to mutate a dictionary base object in rdd

```python
data = [dict(sound=1, counting=2, c=4) for _ in xrange(1000000)]

data_rdd = sc.parallelize(map(Normal, data))
#sc is pyspark context

def function(normal):
new_normal = normal.mutate('counting', 3).mutate('sound', 2)
#still do some calculation with normal [ 1 ]
#even do something with the change history through normal.son.current. normal.son.son.current [ 2 ]
return new_normal

data_rdd.map(function).collect()
```
----
但有些 use case 不会发生[ 1 ],[ 2 ]两种情况。 那样 gc 直接回收 old 。 因为在 spark 集群跑,所以一点点的性能优势可以放大好多。。

我想改进代码可读性。。毕竟直接 inplace 的改变对象的__class__ 好粗暴

(之前的设计不是我搞得。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/315445

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX