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

fix binaryFun #2700

Closed
wants to merge 6 commits into from
Closed

fix binaryFun #2700

wants to merge 6 commits into from

Conversation

9il
Copy link
Member

@9il 9il commented Nov 13, 2014

  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.

See also: #2701, Issue 13253 - use more scoped imports in phobos

enum cmpFunR(string op) =
fun == parm2Name~ op ~parm1Name ||
fun == parm2Name~' '~op~' '~parm1Name;
static if(anySatisfy!(cmpFunR, "<", ">", "==", "!=", "+"))
Copy link
Member

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!

@9il
Copy link
Member Author

9il commented Nov 13, 2014

rebased

@9il
Copy link
Member Author

9il commented Nov 13, 2014

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, i have idea.

Copy link
Member

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. :)

@9il
Copy link
Member Author

9il commented Nov 13, 2014

For CTFE see private struct BinaryCmpFun

mixin("alias "~parm1Name~" = __a ;");
mixin("alias "~parm2Name~" = __b ;");
return mixin(fun);
// "+" needs for optimization of compilation time of summation algorithms.
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Free sum?

Copy link
Member Author

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 :-(

@9il
Copy link
Member Author

9il commented Nov 13, 2014

Aliases removed. Rebased.

@@ -181,6 +193,35 @@ unittest
assert(!greater("1", "2") && greater("2", "1"));
}

//CTFE
private struct BinaryCmpFun
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Your variant please? ;-)

Copy link
Member

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.

Copy link
Member

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
@9il
Copy link
Member Author

9il commented Nov 14, 2014

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)
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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)

Copy link
Member Author

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

@9il
Copy link
Member Author

9il commented Nov 14, 2014

unittest added

@9il 9il closed this Nov 14, 2014
@quickfur
Copy link
Member

Why was this closed? Is there some blocking issue? I'd like to see this merged eventually, as it is a significant improvement IMO.

@9il
Copy link
Member Author

9il commented Nov 14, 2014

I need to sleep and to think =) It will be reopened after few fixes.

@DmitryOlshansky
Copy link
Member

Also check how e.g. find special cases "a == b" or was it "a==b"...

@9il
Copy link
Member Author

9il commented Nov 14, 2014

@quickfur You can review.
@DmitryOlshansky tests was added.

@9il
Copy link
Member Author

9il commented Nov 14, 2014

Oh, I can not reopen this PR =(

@9il
Copy link
Member Author

9il commented Nov 14, 2014

Please find new PR #2723

@quickfur
Copy link
Member

Hmm. Apparently you did a git push -f, and that broke GitHub's PR reopening function. Looks like you'll have to submit a new PR.

@quickfur
Copy link
Member

Heh, ninja'd.

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