pchampin / sophia_rs

Sophia: a Rust toolkit for RDF and Linked Data
Other
210 stars 23 forks source link

Allow more optimizations with `TermMatcher` #154

Open pchampin opened 6 months ago

pchampin commented 6 months ago

This issue emerged from #153 .

The method TermMatcher::constant offers implementers a way to optimize their processing of TermMatchers: instead of naively generating all triples and filtering them using TermMatcher::matches, they can use the constant term to only generate the necessary triples.

This is very well for some implementations, but it makes it impossible to optimize on more complex matchers, which some implementations could do. It might be interesting to extend TermMatcher::constant into a more complex method(call it optimizationHint for example), which would return an enum as suggested below. Different implementations would be able to take advantage of different kinds of hints...

Proposal:

enum TermMatcherHint {
    NoHint, // you need to call  the match method...
    Constant(Term), // matches a unique term
    List(Vec[Term]), // matches a pre-determined list of terms
    Range(Term, Term) // matches any term in the given range (given some total order on terms)
    // ... anything else?
}

NB: Any would still return NoHint, and that's OK, because calling Any::match, which is implemented as { return true }, will be optimized away anyway.

pchampin commented 6 months ago

NB: Any would still return NoHint, and that's OK, because calling Any::match, which is implemented as { return true }, will be optimized away anyway.

Rectification: Graph::triples_matching implementations will generally not need a specific hint for Any but implementations of MutableGraph::remove_matching and MutableGraph::retain_matching could benefit from such a hint.