Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime customization of byte-frequency table - Default type param version #119

Closed
wants to merge 2 commits into from

Conversation

sentrip
Copy link
Contributor

@sentrip sentrip commented Feb 9, 2023

No description provided.

BurntSushi pushed a commit that referenced this pull request Jul 11, 2023
This makes it possible for the caller to provide their own ranking
function for individual bytes. This can potentially speed up searches if
one has a better guess than the default for the frequency distribution
of bytes in a particular haystack.

There is a lot of ceremony here, and it basically boils down to
supporting this in no-std no-alloc configurations. I was tempted to
just require alloc for this sort of thing and ask for something like
`Arc<dyn Fn(u8) -> u8>`, but that would require some ceremony of its
own internally to deal with in the no-alloc case. And forcing an
allocation for every searcher construction that uses a customer ranker
feels like bad juju to me.

Another choice would be to just ask for a `fn(u8) -> u8`, but this makes
the case of "I analyzed a haystack at runtime to build my ranker" more
difficult. Not impossible. But annoying.

Yet another choice was to add the trait as in this commit, and then add
it as a new type parameter to `FinderBuilder`. I believe this would
work, but it requires complicating the public API even more and imposes
constraints on the trait (for example, it would want to be `Clone` at
least in order to avoid backwards incompatible changes in the
`FinderBuilder` API). There's also just generally more ceremony with
having to add a type parameter everywhere. Since we only need the
ranking function at searcher construction time, we can ask for it at the
time of construction and then get rid of it, thus avoiding it infecting
everything else.

Fixes #117, Closes #118, Closes #119
BurntSushi pushed a commit that referenced this pull request Jul 11, 2023
This makes it possible for the caller to provide their own ranking
function for individual bytes. This can potentially speed up searches if
one has a better guess than the default for the frequency distribution
of bytes in a particular haystack.

There is a lot of ceremony here, and it basically boils down to
supporting this in no-std no-alloc configurations. I was tempted to
just require alloc for this sort of thing and ask for something like
`Arc<dyn Fn(u8) -> u8>`, but that would require some ceremony of its
own internally to deal with in the no-alloc case. And forcing an
allocation for every searcher construction that uses a customer ranker
feels like bad juju to me.

Another choice would be to just ask for a `fn(u8) -> u8`, but this makes
the case of "I analyzed a haystack at runtime to build my ranker" more
difficult. Not impossible. But annoying.

Yet another choice was to add the trait as in this commit, and then add
it as a new type parameter to `FinderBuilder`. I believe this would
work, but it requires complicating the public API even more and imposes
constraints on the trait (for example, it would want to be `Clone` at
least in order to avoid backwards incompatible changes in the
`FinderBuilder` API). There's also just generally more ceremony with
having to add a type parameter everywhere. Since we only need the
ranking function at searcher construction time, we can ask for it at the
time of construction and then get rid of it, thus avoiding it infecting
everything else.

Fixes #117, Closes #118, Closes #119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant