feat(compress): move trace record construction to Rust

Keep the compression context extraction and the weak trace hook adapter in C,
where the private CCtx layout and platform-specific tracing declarations remain
available. Project only the scalar trace inputs and callback through an explicit
ABI-checked boundary so Rust owns the enabled check, stable record layout,
version field, compression-only null fields, and end-of-trace policy. The C
entry point still clears traceCtx after Rust returns, preserving the old
single-use lifecycle and keeping trace-disabled builds free of weak-hook calls.

Test Plan:
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo check --manifest-path rust/Cargo.toml --tests
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/cli/Cargo.toml --all-targets -- -D warnings
- ulimit -v 41943040; CARGO_BUILD_JOBS=1 make -j1
- ulimit -v 41943040; make -j1 -C tests test
This commit is contained in:
2026-07-21 19:09:02 +02:00
parent 42e6662a6d
commit 8c20f95010
2 changed files with 163 additions and 14 deletions
+63 -14
View File
@@ -7284,23 +7284,72 @@ size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel)
}
typedef void (*ZSTD_rust_traceEnd_f)(
void* context, unsigned long long traceCtx, const void* trace);
typedef struct {
void* callbackContext;
unsigned long long traceCtx;
int streaming;
unsigned dictionaryID;
size_t dictionarySize;
size_t uncompressedSize;
size_t compressedSize;
const void* params;
const void* cctx;
ZSTD_rust_traceEnd_f traceEnd;
} ZSTD_rust_traceEndState;
typedef char ZSTD_rust_trace_end_state_layout[
(offsetof(ZSTD_rust_traceEndState, callbackContext) == 0
&& offsetof(ZSTD_rust_traceEndState, traceCtx) == sizeof(void*)
&& offsetof(ZSTD_rust_traceEndState, streaming)
== sizeof(void*) + sizeof(unsigned long long)
&& offsetof(ZSTD_rust_traceEndState, dictionaryID)
== sizeof(void*) + sizeof(unsigned long long) + sizeof(int)
&& offsetof(ZSTD_rust_traceEndState, dictionarySize)
== sizeof(void*) + sizeof(unsigned long long) + 2 * sizeof(int)
&& offsetof(ZSTD_rust_traceEndState, uncompressedSize)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + sizeof(size_t)
&& offsetof(ZSTD_rust_traceEndState, compressedSize)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + 2 * sizeof(size_t)
&& offsetof(ZSTD_rust_traceEndState, params)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + 3 * sizeof(size_t)
&& offsetof(ZSTD_rust_traceEndState, cctx)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + 4 * sizeof(size_t)
&& offsetof(ZSTD_rust_traceEndState, traceEnd)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + 5 * sizeof(size_t)
&& sizeof(ZSTD_rust_traceEndState)
== offsetof(ZSTD_rust_traceEndState, dictionarySize) + 6 * sizeof(size_t))
? 1 : -1];
void ZSTD_rust_traceEnd(const ZSTD_rust_traceEndState* state);
#if ZSTD_TRACE
static void ZSTD_rust_traceEnd_callback(void* context,
unsigned long long traceCtx, const void* trace)
{
(void)context;
if (ZSTD_trace_compress_end != NULL) {
ZSTD_trace_compress_end((ZSTD_TraceCtx)traceCtx,
(ZSTD_Trace const*)trace);
}
}
#endif
void ZSTD_CCtx_trace(ZSTD_CCtx* cctx, size_t extraCSize)
{
#if ZSTD_TRACE
if (cctx->traceCtx && ZSTD_trace_compress_end != NULL) {
int const streaming = cctx->inBuffSize > 0 || cctx->outBuffSize > 0 || cctx->appliedParams.nbWorkers > 0;
ZSTD_Trace trace;
ZSTD_memset(&trace, 0, sizeof(trace));
trace.version = ZSTD_VERSION_NUMBER;
trace.streaming = streaming;
trace.dictionaryID = cctx->dictID;
trace.dictionarySize = cctx->dictContentSize;
trace.uncompressedSize = cctx->consumedSrcSize;
trace.compressedSize = cctx->producedCSize + extraCSize;
trace.params = &cctx->appliedParams;
trace.cctx = cctx;
ZSTD_trace_compress_end(cctx->traceCtx, &trace);
}
ZSTD_rust_traceEndState state;
state.callbackContext = cctx;
state.traceCtx = cctx->traceCtx;
state.streaming = cctx->inBuffSize > 0 || cctx->outBuffSize > 0
|| cctx->appliedParams.nbWorkers > 0;
state.dictionaryID = cctx->dictID;
state.dictionarySize = cctx->dictContentSize;
state.uncompressedSize = cctx->consumedSrcSize;
state.compressedSize = cctx->producedCSize + extraCSize;
state.params = &cctx->appliedParams;
state.cctx = cctx;
state.traceEnd = ZSTD_rust_traceEnd_callback;
ZSTD_rust_traceEnd(&state);
cctx->traceCtx = 0;
#else
(void)cctx;