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
fix binaryFun #2700
fix binaryFun #2700
Conversation
enum cmpFunR(string op) = | ||
fun == parm2Name~ op ~parm1Name || | ||
fun == parm2Name~' '~op~' '~parm1Name; | ||
static if(anySatisfy!(cmpFunR, "<", ">", "==", "!=", "+")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: please add a space between if
and the opening parenthesis: if (...)
instead of if(...)
. Thanks!
rebased |
ditto |
import std.algorithm, std.conv, std.exception, std.math, std.range, std.string; | ||
auto binaryFun(ElementType1, ElementType2) | ||
(auto ref ElementType1 __a, auto ref ElementType2 __b) | ||
private static bool cmpFun(string op) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this function be put outside the binaryFun
template (as a private module function)? Otherwise, it will get instantiated for every string lambda, which is a lot of template bloat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function use template params parm1Name
and parm2Name
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, i have idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trick to CTFE functions, is that they can take "runtime" arguments from compile-time arguments. So you can just pass those compile-time arguments as "runtime" arguments to the function, they still get evaluated at compile-time by CTFE. :)
For CTFE see |
mixin("alias "~parm1Name~" = __a ;"); | ||
mixin("alias "~parm2Name~" = __b ;"); | ||
return mixin(fun); | ||
// "+" needs for optimization of compilation time of summation algorithms. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh? I don't understand what you are trying to say here. std.algorithm.sum
doesn't seem to use binaryFun
either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay, now I see what you are referring to, the reduce
call in the default implementation of sum
with seed. However, in that case, it would be enough to make sum
use a lambda, wouldn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reduce!"a + b"
is a common code. Also lambdas is not good stuff in library because compiler can not compare them.
With lambda
auto s1 = reduce!"a + b"(ar);
auto s1 = reduce!((a, b) => a + b)(ar);
auto s2 = sum(ar);
will produce three reduce functions. But with sum = reduce!"a + b"
only two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then make it a free (template) function. But in any case, that's beside the point here. I just wanted to point out that the comment is rather unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Free sum
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@klickverbot please explain what I should do. My english is bad :-(
Aliases removed. Rebased. |
@@ -181,6 +193,35 @@ unittest | |||
assert(!greater("1", "2") && greater("2", "1")); | |||
} | |||
|
|||
//CTFE | |||
private struct BinaryCmpFun |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be a crude pattern matcher rather then BinaryCmpFun
which to me sounds like binary comparator function, a kind of wrapper function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your variant please? ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MatchOperator
, at least Match*
part does make sense and operators are what we match against.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other version are like MatchSimpleExpr
, since this is what we handle for now.
1. Make binaryFun correct for integer comparison. 2. Remove import of half of phobos for 100% cases in phobos. `sort`, `cmp`, `find`, `startsWith`, `sum` and other algorithms would be compiled faster. fix safeOp fix safeOPs Fix new unittest fix binaryReverseArgs fix not fix new code fix spaces after if optimization 1. CTFE 2. Runtime (without inlining) change condition fix new code fix safeOp optimize CTFE CTFE optimization immutable -> enum remove aliases remove macros CTFE matching
debug info: unaryFun => (a & 1) == 0
unaryFun => sqrt(a): import std.*
unaryFun => a.sqrt: import std.*
unaryFun => .a+a: import std.*
unaryFun => _a+a: import std.*
unaryFun => a + 1
binaryFun => a < b
binaryFun => a > b
binaryFun => b+sqrt(a): import std.*
binaryFun => b+a.sqrt: import std.*
binaryFun => a[b*2]
binaryFun => a[b(a)**2]
binaryFun => a_b: import std.*
binaryFun => a.b: import std.*
binaryFun => -a < -b //added lately
binaryFun => a + b
unaryFun => a != 5
binaryFun => a != b
binaryFun => a == b
unaryFun => a
unaryFun => a * 2
unaryFun => a * 3
unaryFun => a * a
unaryFun => -a
unaryFun => to!(int)(a) + 1: import std.* Now matching works! |
private bool _ctfeMatchBinary(string fun, string name1, string name2) | ||
{ | ||
fun._ctfeSkipOp; | ||
while ((fun._ctfeSkipName(name1) + fun._ctfeSkipName(name2) + fun._ctfeSkipInteger == 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you can skip unary here ;)
And if you add separate stage for indexing as "primary expression" that is used in unary you'd get even further ahead in what it deduces as simple expressions such as -a < -b
.
Ultimately this gets us simplistic recursive descent parser, it's only a question of balance and taste to know where to stop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-a < -b
is deduced. See debug info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a[b(a)**2]
for example ). You can check -a < -b
with debug(std_functional)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added -a < -b
in debug unittest. 9il@de82d78
unittest added |
Why was this closed? Is there some blocking issue? I'd like to see this merged eventually, as it is a significant improvement IMO. |
I need to sleep and to think =) It will be reopened after few fixes. |
Also check how e.g. find special cases "a == b" or was it "a==b"... |
@quickfur You can review. |
Oh, I can not reopen this PR =( |
Please find new PR #2723 |
Hmm. Apparently you did a |
Heh, ninja'd. |
sort
,cmp
,find
,startsWith
,sum
and other algorithms would be compiled faster.See also: #2701, Issue 13253 - use more scoped imports in phobos