Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ZstdDataCompressor.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ZstdDataCompressor.cc (C) 2000-2025 */
9/* */
10/* Compression service using the 'zstd' library. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/IOException.h"
15#include "arcane/utils/Array.h"
16#include "arcane/utils/TraceInfo.h"
17#include "arcane/utils/IDataCompressor.h"
18
19#include "arcane/core/FactoryService.h"
20#include "arcane/core/AbstractService.h"
21#include "arcane/core/IDeflateService.h"
22
23#include <zstd.h>
24// Necessary for zstd versions prior to 1.5.6.
25#include <zstd_errors.h>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
39class ZstdDataCompressor
40: public AbstractService
41, public IDataCompressor
42{
43 public:
44
45 explicit ZstdDataCompressor(const ServiceBuildInfo& sbi)
46 : AbstractService(sbi)
47 , m_name(sbi.serviceInfo()->localName())
48 {
49 }
50
51 public:
52
53 void build() override {}
54 String name() const override { return m_name; }
55 Int64 minCompressSize() const override { return 512; }
56 void compress(Span<const std::byte> values, Array<std::byte>& compressed_values) override
57 {
58 size_t input_size = values.size();
59 size_t dest_capacity = ZSTD_compressBound(input_size);
60 compressed_values.resize(dest_capacity);
61
62 void* dest = compressed_values.data();
63 const void* source = values.data();
64 int compression_level = 1;
65 size_t r = ZSTD_compress(dest, dest_capacity, source, input_size, compression_level);
66 if (ZSTD_isError(r)) {
67 ZSTD_ErrorCode err_code = ZSTD_getErrorCode(r);
68 ARCANE_THROW(IOException, "IO error during compression r={0} msg={1}", err_code, ZSTD_getErrorString(err_code));
69 }
70 size_t dest_len = r;
71 if (input_size > 0) {
72 Real ratio = (static_cast<double>(dest_len) * 100.0) / static_cast<double>(input_size);
73 info(5) << "ZSTD compress r=" << r << " source_len=" << input_size
74 << " dest_len=" << dest_len << " ratio=" << ratio;
75 }
76 compressed_values.resize(dest_len);
77 }
78
79 void decompress(Span<const std::byte> compressed_values, Span<std::byte> values) override
80 {
81 void* dest = values.data();
82 size_t dest_len = values.size();
83
84 const void* source = compressed_values.data();
85 size_t source_len = compressed_values.size();
86
87 size_t r = ZSTD_decompress(dest, dest_len, source, source_len);
88 info(5) << "ZSTD decompress r=" << r << " source_len=" << source_len << " dest_len=" << dest_len;
89 if (ZSTD_isError(r)) {
90 ZSTD_ErrorCode err_code = ZSTD_getErrorCode(r);
91 ARCANE_THROW(IOException, "IO error during decompression r={0} msg={1}", err_code, ZSTD_getErrorString(err_code));
92 }
93 }
94
95 private:
96
97 String m_name;
98};
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103ARCANE_REGISTER_SERVICE(ZstdDataCompressor,
104 ServiceProperty("zstdDataCompressor", ST_Application | ST_CaseOption),
105 ARCANE_SERVICE_INTERFACE(IDataCompressor));
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110} // End namespace Arcane
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_SERVICE_INTERFACE(ainterface)
Macro to declare an interface when registering a service.
AbstractService(const ServiceBuildInfo &)
Constructor from a ServiceBuildInfo.
Base class for 1D data vectors.
void resize(Int64 s)
Changes the number of elements in the array to s.
const T * data() const
Access to the root of the array without any protection.
Interface of a service for compressing/decompressing data.
Exception when an input/output error is detected.
Definition IOException.h:34
virtual String localName() const =0
Local part of the service name.
Structure containing the information to create a service.
IServiceInfo * serviceInfo() const
Access to the associated IServiceInfo.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
TraceMessage info() const
Flow for an information message.
Int64 minCompressSize() const override
Minimum array size below which compression is not useful.
void build() override
Build-level construction of the service.
String name() const override
Algorithm name.
void decompress(Span< const std::byte > compressed_values, Span< std::byte > values) override
Decompresses the data compressed_values and stores it in values.
void compress(Span< const std::byte > values, Array< std::byte > &compressed_values) override
Compresses the data values and stores it in compressed_values.
#define ARCANE_REGISTER_SERVICE(aclass, a_service_property,...)
Macro for registering a service.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
@ ST_Application
The service is used at the application level.
@ ST_CaseOption
The service is used at the dataset level.
double Real
Type representing a real number.