-
Notifications
You must be signed in to change notification settings - Fork 88
WIP fsst+ #4132
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
Draft
a10y
wants to merge
1
commit into
develop
Choose a base branch
from
aduffy/fsstplus
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP fsst+ #4132
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ mod ops; | |
| mod serde; | ||
| #[cfg(test)] | ||
| mod tests; | ||
| mod prefix; | ||
|
|
||
| pub use array::*; | ||
| pub use compress::*; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| //! An implementation of the FSST+ extension, original at https://github.com/cwida/fsst_plus/. | ||
| //! | ||
| //! FSST+ augments FSST with the addition. | ||
|
|
||
| #![allow(unused)] | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct SimilarityBlock { | ||
| start_idx: usize, | ||
| prefix_len: usize, | ||
| } | ||
|
|
||
| /// Maximum shared prefix length. | ||
| pub const MAX_PREFIX: usize = 128; | ||
|
|
||
| /// Find the longest-common-prefix between adjacent encoded texts. | ||
| pub fn longest_common_prefix(codes: &[&[u8]]) -> Vec<usize> { | ||
| // LCP for each pair of successive strings. | ||
| // LCP[i] is the length of longest common prefix between the i and i+1 string. | ||
| // For example, lcp(["abc", "abcd", "ab"]) -> [3, 2] | ||
| let mut longest_common_prefix = Vec::new(); | ||
|
|
||
| // Calculate the LCP of consecutive strings in the input. | ||
| for w in codes.windows(2) { | ||
| let s1 = w[0]; | ||
| let s2 = w[1]; | ||
|
|
||
| // Consecutive strings to evaluate LCP | ||
| let mut lcp = 0; | ||
| for (&a, &b) in s1.iter().zip(s2.iter()) { | ||
| if a == b { | ||
| lcp += 1; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| longest_common_prefix.push(lcp); | ||
| } | ||
|
|
||
| longest_common_prefix | ||
| } | ||
|
|
||
| /// Input: a vector of FSST-encoded strings. | ||
| /// Output: a vector of "similarity blocks". | ||
| /// | ||
| /// We first calculate the LCP between adjacent strings, then once that has been completed, we find | ||
| /// the optimal split points to create "similarity" blocks that all share a maximal prefix. | ||
| #[allow(clippy::needless_range_loop)] | ||
| pub fn chunk_by_similarity(strings: &[&[u8]]) -> Vec<SimilarityBlock> { | ||
| // Calculate LCP between all items first | ||
| let lcp = longest_common_prefix(strings); | ||
|
|
||
| // min_lcp[i][j] = min(LCP[i], LCP[i+1], ..., LCP[j]) | ||
| let mut min_lcp = vec![vec![0; strings.len()]; strings.len()]; | ||
|
|
||
| // ... the diagonals are just the LCP[i]'s | ||
| for i in 0..strings.len() { | ||
| // up to 128 is longest prefix we allow. | ||
| min_lcp[i][i] = strings[i].len().min(MAX_PREFIX); | ||
| for j in (i + 1)..lcp.len() { | ||
| min_lcp[i][j] = min_lcp[i][j - 1].min(lcp[j - 1]); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Cost is the total cost of the block split. | ||
| let mut cost = vec![usize::MAX; 1+strings.len()]; | ||
| cost[0] = 0; | ||
|
|
||
| let mut block = vec![0; 1+strings.len()]; | ||
| let mut prefix = vec![0; 1+strings.len()]; | ||
|
|
||
| // Estimate the cost for all strings instead here. | ||
| let estimate_cost = |i: u32, j: u32| min_lcp[i as usize][j as usize]; | ||
|
|
||
| // cum_len[k] = sum(strings[i].len() for i in 0..k) | ||
| let mut length_prefix_sum = Vec::new(); | ||
| length_prefix_sum.push(0); | ||
| for string in strings { | ||
| length_prefix_sum.push(length_prefix_sum.last().unwrap() + string.len()); | ||
| } | ||
|
|
||
| // cost[j] holds the cost up to j. | ||
| // block[j] holds the start of the block that j is a part of | ||
| // prefix[j] is the prefix length for the j-th string | ||
| for end in 1..=strings.len() { | ||
| for start in 0..end { | ||
| dbg!((start, end)); | ||
| let min_prefix = min_lcp[start][end - 1]; | ||
| dbg!(min_prefix); | ||
| for prefix_len in [0, min_prefix] { | ||
| dbg!(prefix_len); | ||
| let n = end - start; | ||
| dbg!(n); | ||
| let per_string_overhead = if prefix_len == 0 { 1 } else { 3 }; | ||
| dbg!(per_string_overhead); | ||
| let overhead = n * per_string_overhead; | ||
| dbg!(overhead); | ||
| let sum_len = length_prefix_sum[end] - length_prefix_sum[start]; | ||
| dbg!(sum_len); | ||
| let total_cost = cost[start] + overhead + sum_len - ((n - 1) * prefix_len as usize); | ||
| dbg!(total_cost); | ||
|
|
||
| if total_cost < cost[end] { | ||
| cost[end] = total_cost; | ||
| block[end] = start; | ||
| prefix[end] = prefix_len; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Traverse the blocks from end to start. | ||
| let mut blocks = Vec::new(); | ||
|
|
||
| let mut idx = strings.len(); | ||
| while idx > 0 { | ||
| let start_idx = block[idx]; | ||
| let prefix_len = prefix[idx]; | ||
|
|
||
| blocks.push(SimilarityBlock { | ||
| start_idx, prefix_len, | ||
| }); | ||
|
|
||
| // Advance backward. | ||
| idx = start_idx; | ||
| } | ||
|
|
||
| // Reverse the list of blocks so they're in order now. | ||
| blocks.reverse(); | ||
|
|
||
| blocks | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use fsst::Compressor; | ||
|
|
||
| use crate::prefix::{chunk_by_similarity, longest_common_prefix}; | ||
|
|
||
| #[test] | ||
| fn test_urls() { | ||
| let strings = vec![ | ||
| "reddit.com".as_bytes(), | ||
| "reddit.com/a".as_bytes(), | ||
| "reddit.com/a/b".as_bytes(), | ||
| "reddit.com/c".as_bytes(), | ||
| "reddit.com/c/d/d".as_bytes(), | ||
| "reddit.com/c/e".as_bytes(), | ||
| "google.com".as_bytes(), | ||
| "google.com/search?q=beans".as_bytes(), | ||
| "google.com/search?q=black+beans".as_bytes(), | ||
| "google.com/search?q=lima+beans".as_bytes(), | ||
| "google.com/search?q=taylor+swift".as_bytes(), | ||
| ]; | ||
|
|
||
| let compressor = Compressor::train(&strings); | ||
| let result = compressor.compress_bulk(&strings); | ||
|
|
||
| let lcps = longest_common_prefix(strings.as_slice()); | ||
| assert_eq!(lcps, vec![10, 12, 11, 12, 13, 0, 10, 21, 20, 20]); | ||
|
|
||
| let chunks = chunk_by_similarity(&strings); | ||
|
|
||
| dbg!(&chunks); | ||
|
|
||
| // Once we have calculate the adjacent_lcp, expand into a new LCP with a bunch of strings | ||
| // in a single block. | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The maximum prefix size should be 255 here :)
128 is the batch size, i.e. how many rows at a time the chunk_by_similarity() function would process.
But nice start, you guys are fast