Skip to content

Re-organising and cleaning up hist.rs#279

Open
cruzzil wants to merge 10 commits intotrifectatechfoundation:mainfrom
cruzzil:hist_cleanup
Open

Re-organising and cleaning up hist.rs#279
cruzzil wants to merge 10 commits intotrifectatechfoundation:mainfrom
cruzzil:hist_cleanup

Conversation

@cruzzil
Copy link
Contributor

@cruzzil cruzzil commented Jan 31, 2026

Should review the commits in order.

  • Reorganised the code layout to match C
  • Added sve stubs. TODO implement
  • Added back original comments
  • Added asserts
  • Various cleanups and using slices instead of raw pointers

@codecov
Copy link

codecov bot commented Jan 31, 2026

Codecov Report

❌ Patch coverage is 70.98765% with 47 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/compress/hist.rs 77.34% 29 Missing ⚠️
lib/compress/zstd_compress.rs 62.50% 9 Missing ⚠️
lib/compress/zstd_compress_superblock.rs 0.00% 6 Missing ⚠️
lib/compress/zstd_preSplit.rs 0.00% 3 Missing ⚠️
Flag Coverage Δ
test-aarch64-apple-darwin 33.02% <70.98%> (+0.07%) ⬆️
test-aarch64-unknown-linux-gnu 31.62% <70.98%> (-0.05%) ⬇️
test-i686-unknown-linux-gnu 31.69% <70.98%> (-0.04%) ⬇️
test-x86_64-unknown-linux-gnu 33.36% <70.98%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
lib/compress/huf_compress.rs 82.00% <100.00%> (ø)
lib/compress/zstd_preSplit.rs 65.58% <0.00%> (ø)
lib/compress/zstd_compress_superblock.rs 0.00% <0.00%> (ø)
lib/compress/zstd_compress.rs 38.36% <62.50%> (+0.13%) ⬆️
lib/compress/hist.rs 73.70% <77.34%> (-0.69%) ⬇️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +171 to +174
// Split workspace into 4 counting tables of 256 u32 each
let (Counting1, remainder) = workSpace.split_at_mut(256);
let (Counting2, remainder) = remainder.split_at_mut(256);
let (Counting3, Counting4) = remainder.split_at_mut(256);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like this can work here, keeps the size in the type

let ([Counting1, Counting2, Counting3, Counting4], &[]) = workSpace.as_chunks::<256>() else { 
    unreachable!();
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about this one, great suggestion. Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm getting a test failure on this, likely because the workSpace is significantly larger than the requirement. Will investigate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead of a slice, I could use a fixed array size for workSpace since it only really needs to be 1024 u32's long, everything else is ignored.

let mut cSymbolTypeSizeEstimateInBits = 0;
let mut max = maxCode;

//SAFETY: workspace must be 4 bytes aligned and >= HIST_WKSP_SIZE in bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must justify the unsafety here, not list the requirements. So why is it properly aligned, and why is it of the required size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are pre-existing requirements of the C code. Maybe SAFETY should be changed to NOTE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clarified with a note that this was an existing requirement of the C implementation.

sourceSize: size_t,
check: HIST_checkInput_e,
workSpace: *mut u32,
workSpace: &mut [u32],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general note

  • is this code well-tested?
  • how can this code be benchmarked

you're introducing bounds checks, and while those are mostly harmless, sometimes they cause serious performance regressions. At least on my machine the rust and C code are currently about the same in terms of performance (no significant differences), and I'd like to not regress that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benchmark script that I can run to compare commit performance?

ip = ip.add(1);
let fresh1 = &mut (*count.offset(*fresh0 as isize));
*fresh1 = (*fresh1).wrapping_add(1);
pub unsafe fn HIST_add(count: &mut [c_uint], src: *const c_void, srcSize: size_t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move this to zstd_preSplit.rs, it's not used here at all. Also generally it's nicer to create the slice at the call site if that means you can make this function safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes probably sits better in zstd_preSplit.rs but kept here just for consistency vs C code whilst the cleanup is being done. I can leave a TODO if that helps.

Comment on lines +217 to +220
for s in 0..256 {
Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
if (Counting1[s] > max) {
max = Counting1[s];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this code is hot at all we can probably tweak this a bit to get better assembly.

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.

2 participants