Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research how to implement Qt style property bindings #132

Open
YarikTH opened this issue Sep 4, 2023 · 2 comments
Open

Research how to implement Qt style property bindings #132

YarikTH opened this issue Sep 4, 2023 · 2 comments

Comments

@YarikTH
Copy link
Owner

YarikTH commented Sep 4, 2023

Description

Property bindings is an approach intended to minimize duplication in property/signal lifting. Ureact used overloaded operators to dedup expressions, but unfortunately it comes with a cost with complex expressions. It's faster to wrap all expression in a lambda, than writing it as expression:

signal<int> x;
signal<int> y;
signal c = x * x + y * y;
signal d = lift( with(x, y), []( int x, int y ){ return x * x + y * y; } );

Compilation times and runtime speed of d is better. But we have to repeat x and y three times instead of one. Property bindings can solve it:

signal<int> x;
signal<int> y;
signal e = bind( [=](){ return x.get() * x.get() + y.get() * y.get(); } );
signal f = bind( [=](){ return std::sqrt( x.get() * x.get() + y.get() * y.get() ); } );

It might improve not only lift, but synced versions of transform, filter etc too.

The problem is - I have no idea how to implement it. So I need to make some research.


See https://doc.qt.io/qt-6/bindableproperties.html

QProperty<QString> firstname("John");
QProperty<QString> lastname("Smith");
QProperty<int> age(41);

QProperty<QString> fullname;
fullname.setBinding(
    [&]() {
        return firstname.value() 
             + " "
             + lastname.value()
             + " age: "
             + QString::number(age.value());
    });

As I understand, Qt has some lambda capture mechanism that allows to see which QProperty are captured and used. Not only types, but specific values.

Alternative approach is - run lambda once and detect which QProperty::value() were called. But it is not the case for Qt. I checked it on if( flag.value ) return a.value(); else return b.value(); and Qt still captured all 3 variables.


Similar approach is implemented in https://github.com/theartful/bindable_properties:

property<int> x;
property<int> y;

property<int> z;
z.set_binding([=]() { return x.value() + y.value(); });
using namespace bindable_properties;

property<int> x = 20;
property<int> y = 21;
property<double> hypotenuse; // Represents the hypotenuse of a right triangle

// Binding: hypotenuse = sqrt(x^2 + y^2)
hypotenuse.set_binding(
    [&] { return std::sqrt(x * x + y * y); }
);
@theartful
Copy link

Hi, author of bindable_properties here. First of all, great work you've done here. I wish I found this library sooner.

Qt runs the lambda when the binding is first set, and then stores all properties whose "value" function was called. I'm using the same technique. I'm very surprised by your example not showing this.

#include <QProperty>
#include <assert.h>

int main() {
    QProperty<int> prop1 { 3 };
    QProperty<int> prop2 { 4 };

    QProperty<int> prop3;

    bool prop3_bound_to_prop1 = true;

    prop3.setBinding([&]() {
        if (prop3_bound_to_prop1) {
            return prop1.value();
        } else {
            return prop2.value();
        }
    });

    prop1.setValue(5);
    assert(prop3.value() == 5);

    prop3_bound_to_prop1 = false;
    prop2.setValue(6);

    assert(prop3.value() == 6); // this fails
}

I think it's a really neat trick, but it has the unintended problem you mentioned of not capturing properties in code branches that didn't run. AFAIK Qt doesn't warn about this (neither do I).

@YarikTH
Copy link
Owner Author

YarikTH commented Sep 5, 2023

#include <QtCore/QDebug>
#include <QtCore/QProperty>

#define LOGGED_EXPR(_EXPR_)                                                    \
  qDebug() << "===================\n" << #_EXPR_;                              \
  _EXPR_

void condition_qproperty() {
  QProperty<int> left(-1);
  QProperty<int> right(1);
  QProperty<bool> is_right(true);

  QProperty<int> result;

  result.setBinding([&]() {
    qDebug() << "result is calculated";
    if (is_right.value())
      return right.value();
    else
      return left.value();
  });

  auto wtf = result.subscribe(
      [&]() { qDebug() << "result is notified: " << result.value(); });

  LOGGED_EXPR(right = 2);
  LOGGED_EXPR(left = -2);
  LOGGED_EXPR(is_right = false);
  LOGGED_EXPR(left = 3);
}

void condition_bool() {
  QProperty<int> left(-1);
  QProperty<int> right(1);
  bool is_right(true);

  QProperty<int> result;

  result.setBinding([&]() {
    qDebug() << "result is calculated";
    if (is_right)
      return right.value();
    else
      return left.value();
  });

  auto wtf = result.subscribe(
      [&]() { qDebug() << "result is notified: " << result.value(); });

  LOGGED_EXPR(right = 2);
  LOGGED_EXPR(left = -2);
  LOGGED_EXPR(is_right = false);
  LOGGED_EXPR(left = 3);
}

int main() {
  qDebug() << "\n------- condition_qproperty ------\n";
  condition_qproperty();
  qDebug() << "\n--------- condition_bool ---------\n";
  condition_bool();
}

It seems that dependency of non-property condition (condition_bool) breaks the system (but it is bad practice in general, at least because non-property has no ways to notify property to update it. And yes, there is no any way to detect and warn it from code.

Also, it seems that used properties are detected not only on the first calculation, but on each of them. In the condition_qproperty example, left is not used for the first time, but become used after we changed condition. Moreover, when we changed condition, then after recalculation, result no longer recalculated after right has changed.
It is pretty neat in terms that value can depend on tens of properties, but be recalculated only in response to few of them that match current conditions.

It is more or less easy to implement. I feared, that it requires some black magic, when I saw BindingFunctionVTable

P.S. But my idea of using property bindings for synced algorithms is not viable. To do things properly, we need to change topology and recalculate lambda. But synced algorithms are not reenter able (for example take and drop... and even fold).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants