我试图了解如何构建我的程序以在高性能问题中使用 RX。我的应用程序在 3D 世界中有一个对象向量。每个对象占据一个盒子,并有一个“命中”流,代表鼠标悬停在它上面。我想到了如何构建的两种选择:
选项 1
struct object_t
{
string name_;
box bounding_box_;
observable<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {/* ... */}, {}};
}
object_t& add(object_t o)
{
int object_index = objects_.size();
o.hit_ = hit_
.map([=](int index){ return index == object_index; })
.distinct_until_changed();
objects_.push_back(o);
return objects_.back();
}
//! given a stream of mouse points,
//! calculate on which object index(in objects_) the mouse is hover over.
//! 0 if its over the background.
observable<int> hit(observable<point> mouse);
using objects_t = std::vector<object_t>;
objects_t objects_;
observable<int> hit_
};
选项 2
struct object_t
{
string name_;
box bounding_box_;
void signal_hit(boot is_hit) { hit_.get_observer().on_next(is_hit); }
observable<bool> hit() const { return hit_.get_observable(); }
private:
subject<bool> hit_;
};
struct scene_t
{
scene_t(observable<point> mouse) : hit_(hit(mouse))
{
add({"background", {/* ... */}, {}};
hit_
.start_with(0)
.buffer(2, 1) // take two hits together, the current and the previos
.subscribe([this](std::vector<int> indices) {
objects_[indices[1]].signal_hit(false); // we leave this one
objects_[indices[0]].signal_hit(true); // and entering this one
});
}
object_t& add(object_t o)
{
objects_.push_back(o);
return objects_.back();
}
//! ... as above
};
现在的问题是如何将 hit 函数的结果链接到 object_t::hit 流。我看到两种方式:
注:
实现是在 rxcpp 中,但它对于我们在其中使用 RX 的任何语言或通用 FRP 范式都是通用的,这就是我标记 rxjs\rx.net\frp 等的原因。
提前致谢 :-)
最佳答案
如果有一个源 observable 和 N 个订阅者,那么每次源发出时都必须至少进行 N 次计算。没有办法绕过我能想到的。
关于rxjs - RX - 如何以高性能的方式使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260398/