Implement initial version of lambda comparison#7484
Conversation
|
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:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
c481019 to
edf5d0c
Compare
|
What's the use case motivating this? |
|
@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 |
A good example of my head is this recent PR which makes {min,max}Element faster dlang/phobos#4265 |
|
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. |
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. |
The semantic analysis of the |
|
@jacob-carlborg The |
551f5d2 to
e6a74ee
Compare
e6a74ee to
edb2bec
Compare
|
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. |
3e2e559 to
1eb0aa4
Compare
57727d2 to
2a293ea
Compare
src/dmd/traits.d
Outdated
| { | ||
| if (auto t = isDsymbol(oarg)) | ||
| { | ||
| if(auto td = t.isTemplateDeclaration()) |
2a293ea to
126a56e
Compare
|
Will pull this, please add to the spec appropriately. |
|
There are no comments! |
|
And no changelog entry / spec PR :( |
| @@ -0,0 +1,227 @@ | |||
| module dmd.lambdacomp; | |||
There was a problem hiding this comment.
No ddoc header - please have a look at the header files.
| StringTable arg_hash; | ||
| Scope* sc; | ||
| ExpType et; | ||
| Dsymbol d; |
| auto fparam = Parameter.getNth(tf.parameters, i); | ||
| if (fparam.ident !is null) | ||
| { | ||
| auto key = fparam.ident.toString().ptr; |
There was a problem hiding this comment.
FWIW .ptr is deprecated in @safe code: https://dlang.org/changelog/pending.html#ptr-safe-end-of-deprecation
| OutBuffer value; | ||
| value.writestring("arg"); | ||
| value.print(i); | ||
| arg_hash.insert(key, strlen(key), value.extractString); |
There was a problem hiding this comment.
You already call toString above and know the length of key
| auto o1 = (*e.args)[0]; | ||
| auto o2 = (*e.args)[1]; | ||
|
|
||
| FuncLiteralDeclaration isLambda(RootObject oarg) |
|
And no C++ header updates. |
|
A new field was added to FuncLiteralDeclaration. https://github.com/dlang/dmd/pull/7484/files#diff-ca782fb8b1ce12422a29fed42ffe0cadR2921 |
|
Doc update : dlang/dlang.org#2146 |
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.