I have the following logical structure of my project in F# (in files order):
Collection Interfaces
Base Implementations
----> Methods that work on interfaces
| -----> IMathProvider that works on Specialized Implementations is injected during runtime
Specialized Implementations that inherit base implementations.
MathProvider that knows about specialized implementations and implements IMathProvider
Note that MathProvider
depends on Specialized Implementations
but is used above inside Base Implementations
. F# file ordering doesn't allow me to use MathProvider
directly inside Base Implementations
, and I have to do a very dirty hack on this line.
(inside the static constructor of a base implementation this calls a method defined at the very bottom that knows about all implementations and returns the interface).
However, since MathProvider
is returned as an interface its methods are never candidates for JIT inlining, and recently I have learned that it is a very big deal in .NET for high-performance numeric code. Also, this reflection trick is so ugly that it makes me feel bad...
I need a sealed class implementation of MathProvider, which will be able to optimize for special cases. The major case is a data structure where keys and values are arrays (SortedMap<,>, similar to SCG.SortedList<,>). So I do not need to know about SortedMap<,>
, but I need access to the two arrays and the size. I want to define an internal interface at the very top level:
internal interface IArrayBasedMap<TKey, TValue> {
int length { get; }
TKey[] Keys { get; }
TValue[] Values { get; }
}
and make SortedMap<,>
to implement it. Then I could check if a collection implements this interface and apply optimizations to that interface members. Optimizations are mostly P/Invoke calls to a native math library, so I believe type-checking and P/Invoke call overheads will be amortized vs applying a scalar function to each value sequentially.
Do you think this is a correct approach to overcome the F# file ordering and top-down type dependency in this case (vs ugly reflection on startup)?
In general, what are other ways to achieve the same result instead of marker interfaces? Maybe it is possible to change the whole project architecture to avoid such issues?
type ... and ... and ... etc
is not idiomatic F# code.IMathProvider
?