Skip to content

Comments

Implement initial version of lambda comparison#7484

Merged
dlang-bot merged 1 commit intodlang:masterfrom
RazvanN7:Lambda_comparison
Jan 29, 2018
Merged

Implement initial version of lambda comparison#7484
dlang-bot merged 1 commit intodlang:masterfrom
RazvanN7:Lambda_comparison

Conversation

@RazvanN7
Copy link
Contributor

This PR implements the first draft of the lambda comparison project. For the moment, it is possible to compare lambda functions :

-> which use in their body only parameters, IntegerExps and enums (no runtime variables)
-> that have user defined types as parameter types

A lambda function which has runtime variables or function calls in its body will be considered uncomparable for the moment. I plan on working on that case.

The current PR is just a proof of concept and I am sure that there are a lot of corner cases that I've missed and if you identify such cases please report them. I plan an adding some tests in the next commit and also try to see if function calls can be supported by putting the mangled name of the function in the lambda serialization.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.

Some tips to help speed things up:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

Bear in mind that large or tricky changes may require multiple rounds of review and revision.

Please see CONTRIBUTING.md for more information.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@dlang-bot dlang-bot added the Review:WIP Work In Progress - not ready for review or pulling label Dec 21, 2017
@JinShil
Copy link
Contributor

JinShil commented Dec 21, 2017

What's the use case motivating this?

@RazvanN7
Copy link
Contributor Author

@JinShil In phobos there are a lot of algorithms which may apply some optimizations if the given range is sorted. In order to check whether the range is sorted with a certain predicate a lambda comparison operation is required which is currently achieved by comparing string lambda's; this technique is buggy since it only does string comparison. By adding support in the compiler for lambda comparison this will enable safe comparison of predicates

@wilzbach
Copy link
Contributor

What's the use case motivating this?

A good example of my head is this recent PR which makes {min,max}Element faster dlang/phobos#4265
For an identity map function, the algorithm is 6x faster with LDC.
So how do we check for identity?
Currently the only ways are (a) string lambdas (enum isIdentity = map == "a";) or (b) using many overloads (that's what we did with {min,max}Element eventually).
I really look forward to use the new trait for such optimizations :)

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented Dec 21, 2017

Does anyone have any idea why this particular line [1] makes compiling druntime impossible?

[1] https://github.com/dlang/dmd/pull/7484/files#diff-b601037010863a0a6d136649d1e4a5d6R195

Is there another way to obtain the mangled name of a Dsymbol which does not crash druntime?

I figured it out: it seems that in order to do the mangling, semantic3 must have been performed and for some situations that is not the case.

@wilzbach
Copy link
Contributor

What's the use case motivating this?

Oh there's also the movement to phase out string lambdas, see e.g. this old NG post and IIRC comparability was one of the last blocking points.

@jacob-carlborg
Copy link
Contributor

I figured it out: it seems that in order to do the mangling, semantic3 must have been performed and for some situations that is not the case.

The semantic analysis of the .mangleof property is done in the first semantic phase, as far as I understand.

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented Dec 22, 2017

@jacob-carlborg The mangleof property is present only for types and expressions. For functions there is the mangleString field which can be interogated, but for Dsymbols (or AggregateDeclarations to be more specific) I haven't anything but the mangleToBuffer function.

@wilzbach
Copy link
Contributor

Nope. All CIs compile DMD from source before running the testsuite. So the error is on your side.

Closing and opening for SemaphoreCI to appear.

@wilzbach wilzbach closed this Jan 15, 2018
@wilzbach wilzbach reopened this Jan 15, 2018
@RazvanN7 RazvanN7 force-pushed the Lambda_comparison branch 2 times, most recently from 3e2e559 to 1eb0aa4 Compare January 22, 2018 09:56
@RazvanN7 RazvanN7 changed the title [WIP] Add initial implementation of lambda comparison Implement initial version of lambda comparison Jan 22, 2018
@andralex
Copy link
Member

cc @WalterBright

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

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

nice work please fix style

src/dmd/traits.d Outdated
{
if (auto t = isDsymbol(oarg))
{
if(auto td = t.isTemplateDeclaration())
Copy link
Member

Choose a reason for hiding this comment

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

space after if

@andralex
Copy link
Member

Will pull this, please add to the spec appropriately.

@andralex andralex added Merge:auto-merge and removed Review:WIP Work In Progress - not ready for review or pulling labels Jan 29, 2018
@dlang-bot dlang-bot merged commit c048abb into dlang:master Jan 29, 2018
@UplinkCoder
Copy link
Member

There are no comments!

@wilzbach wilzbach added Review:Needs Changelog A changelog entry needs to be added to /changelog Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org labels Jan 29, 2018
@wilzbach
Copy link
Contributor

And no changelog entry / spec PR :(

@@ -0,0 +1,227 @@
module dmd.lambdacomp;
Copy link
Contributor

Choose a reason for hiding this comment

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

No ddoc header - please have a look at the header files.

StringTable arg_hash;
Scope* sc;
ExpType et;
Dsymbol d;
Copy link
Contributor

Choose a reason for hiding this comment

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

private?

auto fparam = Parameter.getNth(tf.parameters, i);
if (fparam.ident !is null)
{
auto key = fparam.ident.toString().ptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

OutBuffer value;
value.writestring("arg");
value.print(i);
arg_hash.insert(key, strlen(key), value.extractString);
Copy link
Contributor

Choose a reason for hiding this comment

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

You already call toString above and know the length of key

Copy link
Contributor Author

@RazvanN7 RazvanN7 Jan 30, 2018

Choose a reason for hiding this comment

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

oh, ok thanks

auto o1 = (*e.args)[0];
auto o2 = (*e.args)[1];

FuncLiteralDeclaration isLambda(RootObject oarg)
Copy link
Contributor

Choose a reason for hiding this comment

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

static

@ibuclaw
Copy link
Member

ibuclaw commented Jan 29, 2018

And no C++ header updates.

@andralex
Copy link
Member

@RazvanN7 and I discussed this during our regular meeting. @RazvanN7 in addition to the spec update, please also add a changelog entry. @ibuclaw what C++ header updates are needed?

@ibuclaw
Copy link
Member

ibuclaw commented Jan 29, 2018

A new field was added to FuncLiteralDeclaration.

https://github.com/dlang/dmd/pull/7484/files#diff-ca782fb8b1ce12422a29fed42ffe0cadR2921

@RazvanN7
Copy link
Contributor Author

Doc update : dlang/dlang.org#2146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merge:auto-merge Review:Needs Changelog A changelog entry needs to be added to /changelog Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants