Repository navigation
Support Zstandard (zstd) binary data array compression in mzML - #10287
Conversation
…n mzML Co-authored-by: jpfeuffer <[email protected]>
|
@copilot building zstd from source in CI fails on Windows. Fix it |
Co-authored-by: jpfeuffer <[email protected]>
Fixed in b0a0972, not yet run on Windows CI. The zstd 1.5.7 tarball contains symlinks under |
|
@copilot resolve the merge conflicts in this pull request |
…ard-compression # Conflicts: # CHANGELOG Co-authored-by: jpfeuffer <[email protected]>
Merged |
|
@copilot Of course.. still doesn't work on windows.. |
Co-authored-by: jpfeuffer <[email protected]>
Sorry, my previous fix extracted too little. On Windows the zstd CMake build also needs |
timosachsenberg
left a comment
There was a problem hiding this comment.
The zstd codec itself looks solid:
- Byte-shuffle and dictionary output are byte-identical to mzd.cpp's writer at 1–1000 and 65534–65537 distinct values.
- Fuzzing the decoders under ASan/UBSan (truncated, concatenated and skippable frames, unknown sizes, random dictionary buffers) found no crashes and no bad input that was silently accepted.
- Every writer path pairs its compression term with the matching encoding, and the zlib paths are unchanged.
- MS:1003780–85 are in the shipped
psi-ms.obounder MS:1000572, with the same names the writer uses. - ProteoWizard returns early on an empty
<binary/>, so empty zstd arrays written here read fine there.
Two things I'd settle before merging (details inline):
- Build: zstd becomes a required dependency, but contrib doesn't provide it. Contrib-based builds stop at configure: the documented Windows build, probably the devcontainer, and Jenkins packaging if it uses contrib.
- Interop: the dictionary decoder can't read mzdata-written arrays with exactly 256 or 65536 distinct values, and a single such array fails the whole file.
Design question: Arrow is already required, and OpenMS's Parquet writers default to zstd, so they already assume Arrow has it. Using arrow::util::Codec (Compression::ZSTD, with MakeDecompressor() for streaming) for compress/decompress would avoid the new direct dependency and the CI and packaging changes. The cost is that mzML zstd support would depend on how Arrow was built. The byte-shuffle and dictionary transforms would stay as they are.
Also inline: a 256 MB allocation driven by an untrusted frame header, a zstd context created for every array (about 16 µs fixed cost each), opentims' separate zstd lookup, and a few nits and test gaps.
Generated by Claude Code
| capacity = static_cast<size_t>(std::min<unsigned long long>(std::max<unsigned long long>(content_size, 1), 1ull << 28)); | ||
| } | ||
|
|
||
| std::unique_ptr<ZSTD_DCtx, DCtxDeleter> dctx(ZSTD_createDCtx()); |
There was a problem hiding this comment.
Optional (performance): every array decode creates and frees a zstd decompression context, then copies the data through three zero-filled buffers: the decompressed string, the unshuffled string, and the std::vector in decodeZstdNumeric.
I ran a quick micro-benchmark of this path (byte-shuffled doubles, level 3, zstd 1.5.5). The alternative reuses a thread_local context, decompresses in one shot into an exact-size buffer, and unshuffles straight into the vector:
| Doubles per array | As in this PR | Reused context + exact buffer |
|---|---|---|
| 200 | 17 µs | 1.1 µs |
| 2000 | 25 µs | 7.7 µs |
| 20000 | 114 µs | 85 µs |
So small MS2 spectra pay about 16 µs of fixed cost per array. OpenMS and ProteoWizard always record the content size, so a fast path using ZSTD_decompressDCtx into an exact-size buffer would cover almost all files, with the streaming loop kept as the fallback. On the write side, ZSTD_compress likewise builds a new compression context for every array.
Generated by Claude Code
There was a problem hiding this comment.
This is optional, so this PR leaves it as is. A reused thread_local context with an exact-size fast path would make a good follow-up.
Generated by Claude Code
| Base64::decodeSingleString(bindata.base64, compressed, false); | ||
| std::string decoded; | ||
| ZstdCompression::decode(compressed.data(), compressed.size(), bindata.zstd_transform, 1, decoded); | ||
| // split at null bytes (skipping empty strings, identical to Base64::decodeStrings) |
There was a problem hiding this comment.
Nit: this re-implements the null-byte splitting of Base64::decodeStrings, and the zstd branch of encodeStringArray re-implements the joining in Base64::encodeStrings. If one side changes, for example to keep empty strings, zlib and zstd string arrays would decode differently without any error. One shared split/join helper that works on an already-decoded buffer would keep them in sync.
Generated by Claude Code
There was a problem hiding this comment.
This is an optional nit, so it stays as is here; a shared split/join helper is a possible follow-up.
Generated by Claude Code
| } | ||
| } | ||
|
|
||
| void MzMLHandlerHelper::encodeNumericArray(std::vector<Int32>& in, const PeakFileOptions& opt, std::string& out) |
There was a problem hiding this comment.
Nit: the four encodeNumericArray overloads are identical apart from calling Base64::encode or encodeIntegers, and this Int32 one has no caller, because the writers widen integer arrays to Int64. A single template with if constexpr (std::is_integral_v<T>) would do, and the unused overload could be dropped from the installed header.
Generated by Claude Code
There was a problem hiding this comment.
This is an optional nit, so it stays as is. Merging the overloads and dropping the unused Int32 one changes the installed header, which is a possible follow-up.
Generated by Claude Code
| } | ||
| END_SECTION | ||
|
|
||
| START_SECTION([EXTRA] load and store zstd compressed binary data arrays) |
There was a problem hiding this comment.
Test gaps worth closing:
- MS:1003784 (PIC + zstd) is never exercised.
- Numpress + zstd (MS:1003783/85) is only written and read by OpenMS itself, so a misreading of the format that is the same on both sides would still pass. A vector generated by another implementation (mzd.cpp, mzdata or msconvert) would pin it down.
- There is no dictionary vector from another implementation at the index-width boundaries (see the comment on
ZstdCompression.cpp). MzMLFile_zstd.mzMLcould also be read throughOnDiscMSExperiment/MzMLSpectrumDecoder. That path uses the same helper but isn't covered by a test.
Generated by Claude Code
There was a problem hiding this comment.
39f5052 adds a PIC + zstd (MS:1003784) round-trip to MzMLFile_test and hand-built dictionary buffers at the 255/256 boundaries. The remaining gaps are left as possible follow-ups: vectors from another implementation (mzd.cpp, mzdata or msconvert) and the OnDiscMSExperiment/MzMLSpectrumDecoder path. Leaving this thread open for those.
Generated by Claude Code
Jenkins is no longer used. Remove the old packaging instructions and OS/compiler matrix to avoid suggesting that this infrastructure is active.
|
Hmm yeah I'm not sure if we should depend on arrow or just look for a standalone zstd. I think I tend towards finding standalone again. Just in case we ever move away from arrow or they stop shipping zstd. Also the code would be more portable then. |
|
I thought we are 100% vcpkg already on all platforms? |
|
No working on it right now (pyopenms is missing on some platforms) |
This comment was marked as resolved.
This comment was marked as resolved.
Resolve the conflict in .github/workflows/pyopenms-wheels-cibuildwheel.yml in favour of develop. Since #10331 and #10333 the Linux and Windows wheels build OpenMS with vcpkg instead of the contrib, and vcpkg builds zstd as a dependency of Arrow. The libzstd and libzstd-devel packages in the contrib manylinux jobs and the Windows zstd source build into contrib/ are therefore no longer needed. The macOS job already installs zstd with Homebrew. tools/jenkins/ is already gone on develop (#10325). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GaXNuGJpQjG9mo63hQnB9B
This comment was marked as resolved.
This comment was marked as resolved.
…ndex width, decode allocation)
- vcpkg.json: list zstd as a direct dependency instead of relying on
Arrow's transitive copy.
- opentims (FetchContent build): link ${OPENMS_ZSTD_TARGET} instead of a
separate find_package(zstd) with a fallback to the bundled
zstddeclib.c, and drop the enable_language(C) that only served that
fallback.
- Dictionary-encoded zstd (MS:1003782): take the index width from the
index region size divided by the array length (arrayLength, threaded
through from MzMLHandlerHelper) when that yields 1, 2, 4 or 8 bytes
adjacent to the specification's width. This reads mzdata's u8 indices
for 256 values and u16 indices for 255 values (mzd.cpp reader).
- uncompressData(): cap the initial output allocation at the expected
decoded size (or 16x the input size if unknown) rather than trusting
the frame header's content size alone; the buffer still grows as
needed.
- Tests: hand-built dictionary buffers at the 255/256 boundaries,
expected-size handling, and a numpress PIC + zstd (MS:1003784)
round-trip in MzMLFile_test.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017gpru7cpwhZCABTWXpD1Vd
… comments Avoids confusion with the deprecated mzData XML format. Comment-only change. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017gpru7cpwhZCABTWXpD1Vd
…10348) * [DOC] Document zstd compression of the mzML peak data The pyOpenMS mzML page gains a Zstandard section: enabling it with PeakFileOptions.setZstdCompression, the cvParam it writes, reading such a file back, and how it combines with zlib and numpress. The OpenMS file-formats page describes FileConverter -zstd_compression and FileFilter -peak_options:zstd_compression. Both follow #10287. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YC1T2K9HQEUk2kQMZqC8h7 * [DOC] CHANGELOG: known issues of the bundled engines and OpenSSL Comet embeds zlib 1.2.11 and expat 2.2.9 and MaRaCluster zlib 1.2.3; both engines ship with 3.6.0 and will be rebuilt afterwards. The packages and wheels carry OpenSSL 3.6.4, whose CVE-2026-84782 concerns only DTLS, which OpenMS does not use. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YC1T2K9HQEUk2kQMZqC8h7 * [DOC] Known issues: OpenSSL 3.6.4 is in every artifact; name the Low advisories The Windows installer and wheel link OpenSSL 3.6.4 statically, so the note covers all installers and wheels, not only Linux and macOS. Besides the High CVE-2026-84782 (DTLS only), twelve Low advisories published with 3.6.5 affect 3.6.4; CVE-2026-35189 is the one that reaches OpenMS, through HTTPS downloads. The pyOpenMS zstd section limits byte shuffling to arrays without numpress encoding, which use their own numpress-plus-zstd terms. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YC1T2K9HQEUk2kQMZqC8h7 * [DOC] CHANGELOG: date the 3.6.0 heading; Sage's SQLite as a known issue The heading says "Released September 2026" instead of "under development". Sage 0.14.6 embeds SQLite 3.41.2 (with FTS5); like the Comet and MaRaCluster copies, it ships with 3.6.0 and will be rebuilt afterwards. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YC1T2K9HQEUk2kQMZqC8h7 * Update CHANGELOG for OpenMS 3.6.0 release Added details about the OpenMS 3.6.0 release, including new features and improvements. --------- Co-authored-by: Claude <[email protected]>
Adds reading and writing of zstd-compressed mzML binary data arrays: the PSI-MS terms MS:1003780–MS:1003785 from the dia-ims-1.0 spec. The implementation follows the spec and the mzd.cpp reference.
Codec: new
ZstdCompressionclass (FORMAT/)arrayLength× element size, passed down fromMzMLHandlerHelper, or an upper bound for dictionary payloads. When no size is known, the cap is 16× the input size. 256 MB is the absolute limit. So a malformed header cannot force a huge allocation, and the buffer grows as needed. Truncated or invalid input throwsException::ConversionError.u64offset, au64 n_values, the byte-shuffled sorted unique values, then the byte-shuffled indices.arrayLength, if that is 1, 2, 4 or 8 bytes and next to the spec's width. Otherwise it uses the spec's width. It accepts all three conventions.mzML I/O (
MzMLHandlerHelper,MzMLHandler)PeakFileOptions::setZstdCompression(), which takes precedence over zlib:MzMLHandlerHelper::encode{Numeric,String,Numpress}Arrayhelpers, so the zlib/zstd choice lives in one place.Base64.h'sinvertEndianess.Options, tools and bindings
PeakFileOptions::setZstdCompression()/getZstdCompression(), default off.FileConverter -zstd_compressionandFileFilter -peak_options:zstd_compression.Build and dependencies
find_package(zstd CONFIG)with whichever ofzstd::libzstd,_sharedor_staticexists, and falls back to a plain header/library search. The result isOPENMS_ZSTD_TARGET.zstdis listed as a direct dependency invcpkg.jsonrather than relying on Arrow's transitive copy. The CI vcpkg cache key hashes that file, so the first CI run rebuilds the cache.${OPENMS_ZSTD_TARGET}. Its ownfind_package(zstd)is gone, and so is the fallback to its bundledzstddeclib.c, which could have put a second zstd decoder into libOpenMS. Theenable_language(C)that only served that fallback is dropped too.libzstd-dev/libzstd1.libzstd/libzstd-develin the manylinux jobs and a zstd source build intocontrib/on Windows.Tests
ZstdCompression_test:MzMLFile_zstd.mzML, generated externally, with arrays in modes 1003780/81/82 across float, int, string and empty arrays, plus a chromatogram.MzMLFile_testchecks:PeakFileOptions_testand a pyOpenMS test.Review notes
$Maintainer:$/$Authors:$fields in the new files are placeholders.