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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native support for min/max #3130

Merged
merged 1 commit into from Jul 21, 2023
Merged

Conversation

Guiguiprim
Copy link
Contributor

@Guiguiprim Guiguiprim commented Jul 21, 2023

The generated code was itching me, so I'm suggesting this. I won't take it badly if you do not like it 馃槢 .

Adds Expression::MinMax

  • Avoid usage of Expression::Condition => cleaner code
  • Open optimization possibility => Expression::MinMax can be seen as const if sub expressions are (see example below)
  • Use new enum instead of char in compiler code for operation selection

After this we could try to optimize min/max during constant optimization.

Slint source file

export component App inherits Window {
    in property f1;
    in property f2;
    out property f_max: max(f1, f2);
    out property f_min: min(f1, f2);
in property<int> i1;
in property<int> i2;
out property<int> i_max: max(i1, i2);
out property<int> i_min: min(i1, i2);

in property<physical-length> pl1;
in property<physical-length> pl2;
out property<physical-length> pl_max: max(pl1, pl2);
out property<physical-length> pl_min: min(pl1, pl2);

in property<length> l1;
in property<length> l2;
out property<length> l_max: max(l1, l2);
out property<length> l_min: min(l1, l2);

in property<duration> d1;
in property<duration> d2;
out property<duration> d_max: max(d1, d2);
out property<duration> d_min: min(d1, d2);

in property<angle> a1;
in property<angle> a2;
out property<angle> a_max: max(a1, a2);
out property<angle> a_min: min(a1, a2);

in property<percent> p1;
in property<percent> p2;
out property<percent> p_max: max(p1, p2);
out property<percent> p_min: min(p1, p2);

out property<int> f_max_opt: max(2, 3);
out property<int> f_min_opt: min(2, 3);

}

Generated LLR on master

     a-max: { minmax_lhs62 = root-1.a1; minmax_rhs62 = root-1.a2; if ((minmax_lhs62 > minmax_rhs62)) { minmax_lhs62 } else { minmax_rhs62 }; };
   a-min: { minmax_lhs63 = root-1.a1; minmax_rhs63 = root-1.a2; if ((minmax_lhs63 < minmax_rhs63)) { minmax_lhs63 } else { minmax_rhs63 }; };
   d-max: { minmax_lhs64 = root-1.d1; minmax_rhs64 = root-1.d2; if ((minmax_lhs64 > minmax_rhs64)) { minmax_lhs64 } else { minmax_rhs64 }; };
   d-min: { minmax_lhs65 = root-1.d1; minmax_rhs65 = root-1.d2; if ((minmax_lhs65 < minmax_rhs65)) { minmax_lhs65 } else { minmax_rhs65 }; };
   f-max: { minmax_lhs66 = root-1.f1; minmax_rhs66 = root-1.f2; if ((minmax_lhs66 > minmax_rhs66)) { minmax_lhs66 } else { minmax_rhs66 }; };
   f-max-opt: ({ minmax_lhs67 = 2; minmax_rhs67 = 3; if ((minmax_lhs67 > minmax_rhs67)) { minmax_lhs67 } else { minmax_rhs67 }; }/* as int */);
   f-min: { minmax_lhs68 = root-1.f1; minmax_rhs68 = root-1.f2; if ((minmax_lhs68 < minmax_rhs68)) { minmax_lhs68 } else { minmax_rhs68 }; };
   f-min-opt: ({ minmax_lhs69 = 2; minmax_rhs69 = 3; if ((minmax_lhs69 < minmax_rhs69)) { minmax_lhs69 } else { minmax_rhs69 }; }/* as int */);
   i-max: { minmax_lhs70 = root-1.i1; minmax_rhs70 = (root-1.i2/* as float */); if ((minmax_lhs70 > minmax_rhs70)) { minmax_lhs70 } else { minmax_rhs70 }; };
   i-min: { minmax_lhs71 = root-1.i1; minmax_rhs71 = (root-1.i2/* as float */); if ((minmax_lhs71 < minmax_rhs71)) { minmax_lhs71 } else { minmax_rhs71 }; };
   l-max: { minmax_lhs72 = root-1.l1; minmax_rhs72 = root-1.l2; if ((minmax_lhs72 > minmax_rhs72)) { minmax_lhs72 } else { minmax_rhs72 }; };
   l-min: { minmax_lhs73 = root-1.l1; minmax_rhs73 = root-1.l2; if ((minmax_lhs73 < minmax_rhs73)) { minmax_lhs73 } else { minmax_rhs73 }; };
   p-max: { minmax_lhs74 = root-1.p1; minmax_rhs74 = (root-1.p2 * 0.01); if ((minmax_lhs74 > minmax_rhs74)) { minmax_lhs74 } else { minmax_rhs74 }; };
   p-min: { minmax_lhs75 = root-1.p1; minmax_rhs75 = (root-1.p2 * 0.01); if ((minmax_lhs75 < minmax_rhs75)) { minmax_lhs75 } else { minmax_rhs75 }; };
   pl-max: { minmax_lhs76 = root-1.pl1; minmax_rhs76 = root-1.pl2; if ((minmax_lhs76 > minmax_rhs76)) { minmax_lhs76 } else { minmax_rhs76 }; };
   pl-min: { minmax_lhs77 = root-1.pl1; minmax_rhs77 = root-1.pl2; if ((minmax_lhs77 < minmax_rhs77)) { minmax_lhs77 } else { minmax_rhs77 }; };
  
Generated LLR on this branch

   a-max: max(root-1.a1, root-1.a2);
   a-min: min(root-1.a1, root-1.a2);
   d-max: max(root-1.d1, root-1.d2);
   d-min: min(root-1.d1, root-1.d2);
   f-max: max(root-1.f1, root-1.f2);
   f-max-opt: (max(2, 3)/* as int */)/*const*/;
   f-min: min(root-1.f1, root-1.f2);
   f-min-opt: (min(2, 3)/* as int */)/*const*/;
   i-max: max(root-1.i1, (root-1.i2/* as float */));
   i-min: min(root-1.i1, (root-1.i2/* as float */));
   l-max: max(root-1.l1, root-1.l2);
   l-min: min(root-1.l1, root-1.l2);
   p-max: max(root-1.p1, (root-1.p2 * 0.01));
   p-min: min(root-1.p1, (root-1.p2 * 0.01));
   pl-max: max(root-1.pl1, root-1.pl2);
   pl-min: min(root-1.pl1, root-1.pl2);
  

For the given example:

  • before: crate build time 639.42 millis, generated file is 50_629 octets for 1_105 formatted lines
  • after: crate build time 632.26 millis, generated file is 43_193 octets for 1_009 formatted lines

Disclaimer: I did not try the C++ generated code, hopefully the CI will.

Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!
On the one hand, i wish to keep the llr as simple as possible.
On the other hand, this is indeed a great way to simplify the generated code, so let's go for it.

internal/compiler/generator/cpp.rs Outdated Show resolved Hide resolved
internal/compiler/generator/cpp.rs Outdated Show resolved Hide resolved
@Guiguiprim
Copy link
Contributor Author

On the one hand, i wish to keep the llr as simple as possible.

Yes I though this might be your answer.

@ogoffart ogoffart merged commit ef8ddaa into slint-ui:master Jul 21, 2023
26 checks passed
@ogoffart
Copy link
Member

Thanks!

@Guiguiprim Guiguiprim deleted the better-min-max branch July 24, 2023 15:53
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

Successfully merging this pull request may close these issues.

None yet

2 participants