Toward a possible specialization scheme #1816
kyouko-taiga
started this conversation in
Language design
Replies: 1 comment
|
I think it makes sense if an algorithm defines a closed set of specializations. The standard library should define the most common traits that can make algorithms more or less efficient, and it's likely that new algorithms won't need to add new abstractions. I think it's a good practice to ask the implementer of an algorithm to think about the optimization opportunities, and to specify or create the necessary abstractions. If somebody has an idea for an even more efficient version, given some different abstractions, they can either
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
In the last team meeting we sketched a possible scheme to handle algorithm specialization that sounds worth exploring. We noted that to fit the constraints of Hylo's type system, we should perhaps take inspiration from dynamic dispatch and rely on partial evaluation to compute as many choices as possible ahead of time.
Looking at the documentation of many algorithms in the STL (e.g., std::partition), we can observe that many specialization opportunities can be determined at the point where an algorithm is defined. In a fully dynamic system, one way to take advantage of these opportunities would be to simply test whether or not a value satisfies the conditions necessary to apply a specialization. For example, in Python:
The test
isinstance(xs, RandomAccessCollection)is essentially expressing a specialization option. If the test succeeds, we know that we can specialize the implementation to compute a result more efficiently than using the "slow path".We could try to apply the same idea to Hylo:
The construct
inline if T is RandomAccessCollection { ... } else { ... }(tentative syntax) expresses a "dynamic" test on the conformances ofT. It is a little special, though, because Hylo doesn't gather all possible conformances witness of a generic parameter. So the fact that the condition isinlinealso informs the compiler that a witness ofRandomAccessCollectioncould be used. Because it doesn't appear in the signature, however, this witness is not necessary to type check the function. In other words, we can callsuffixwith an instance of a type that only satisfiesCollection. If that type additionally satisfiesRandomAccessCollection, then the test will succeed and the specialization will be applied.From there we can imagine using partial evaluation with monomorphization to determine at compile-time whether or not the test will indeed succeed and drop the unspecialized branch.
I can see two interesting advantages of this approach:
There's no free lunch, though. This mechanism won't specialize an algorithm based on "unforeseen" opportunities, i.e., opportunities that were not explicitly stated somewhere in the definition.
All reactions