Class: MultiCompress::Deflater

Inherits:
Object
  • Object
show all
Defined in:
ext/multi_compress/multi_compress.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
# File 'ext/multi_compress/multi_compress.c', line 2010

static VALUE deflater_initialize(int argc, VALUE *argv, VALUE self) {
    VALUE opts;
    rb_scan_args(argc, argv, "0:", &opts);
    reject_algorithm_keyword(opts);

    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);

    VALUE algo_sym = Qnil, level_val = Qnil, dict_val = Qnil;
    if (!NIL_P(opts)) {
        algo_sym = opt_get(opts, sym_cache.algo);
        level_val = opt_get(opts, sym_cache.level);
        dict_val = opt_get(opts, sym_cache.dictionary);
    }

    d->algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
    d->level = resolve_level(d->algo, level_val);
    d->closed = 0;
    d->finished = 0;

    dictionary_t *dict = NULL;
    if (!NIL_P(dict_val)) {
        if (d->algo == ALGO_LZ4) {
            rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
        }
        dict = opt_dictionary(dict_val);
        dictionary_ivar_set(self, dict_val);
    }

    switch (d->algo) {
    case ALGO_ZSTD: {
        d->ctx.zstd = ZSTD_createCStream();
        if (!d->ctx.zstd)
            rb_raise(eMemError, "zstd: failed to create stream");

        if (dict) {
            ZSTD_CCtx_reset(d->ctx.zstd, ZSTD_reset_session_only);
            ZSTD_CCtx_setParameter(d->ctx.zstd, ZSTD_c_compressionLevel, d->level);
            size_t r = ZSTD_CCtx_loadDictionary(d->ctx.zstd, dict->data, dict->size);
            if (ZSTD_isError(r))
                rb_raise(eError, "zstd dict load: %s", ZSTD_getErrorName(r));
        } else {
            size_t r = ZSTD_initCStream(d->ctx.zstd, d->level);
            if (ZSTD_isError(r))
                rb_raise(eError, "zstd init: %s", ZSTD_getErrorName(r));
        }
        break;
    }
    case ALGO_BROTLI: {
        d->ctx.brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL);
        if (!d->ctx.brotli)
            rb_raise(eMemError, "brotli: failed to create encoder");
        if (!BrotliEncoderSetParameter(d->ctx.brotli, BROTLI_PARAM_QUALITY, d->level)) {
            BrotliEncoderDestroyInstance(d->ctx.brotli);
            d->ctx.brotli = NULL;
            rb_raise(eError, "brotli: failed to set quality parameter");
        }
        if (dict) {
            BrotliEncoderPreparedDictionary *pd =
                BrotliEncoderPrepareDictionary(BROTLI_SHARED_DICTIONARY_RAW, dict->size, dict->data,
                                               BROTLI_MAX_QUALITY, NULL, NULL, NULL);
            if (!pd) {
                BrotliEncoderDestroyInstance(d->ctx.brotli);
                d->ctx.brotli = NULL;
                rb_raise(eMemError, "brotli: failed to prepare dictionary");
            }
            if (!BrotliEncoderAttachPreparedDictionary(d->ctx.brotli, pd)) {
                BrotliEncoderDestroyPreparedDictionary(pd);
                BrotliEncoderDestroyInstance(d->ctx.brotli);
                d->ctx.brotli = NULL;
                rb_raise(eError, "brotli: failed to attach dictionary");
            }
            BrotliEncoderDestroyPreparedDictionary(pd);
        }
        break;
    }
    case ALGO_LZ4: {
        d->ctx.lz4 = LZ4_createStream();
        if (!d->ctx.lz4)
            rb_raise(eMemError, "lz4: failed to create stream");
        LZ4_resetStream(d->ctx.lz4);
        d->lz4_ring.buf = ALLOC_N(char, LZ4_RING_BUFFER_TOTAL);
        d->lz4_ring.ring_offset = 0;
        d->lz4_ring.pending = 0;
        break;
    }
    }

    return self;
}

Instance Method Details

#closeObject



2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
# File 'ext/multi_compress/multi_compress.c', line 2552

static VALUE deflater_close(VALUE self) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
    if (d->closed)
        return Qnil;

    switch (d->algo) {
    case ALGO_ZSTD:
        if (d->ctx.zstd) {
            ZSTD_freeCStream(d->ctx.zstd);
            d->ctx.zstd = NULL;
        }
        break;
    case ALGO_BROTLI:
        if (d->ctx.brotli) {
            BrotliEncoderDestroyInstance(d->ctx.brotli);
            d->ctx.brotli = NULL;
        }
        break;
    case ALGO_LZ4:
        if (d->ctx.lz4) {
            LZ4_freeStream(d->ctx.lz4);
            d->ctx.lz4 = NULL;
        }
        break;
    }
    d->closed = 1;
    return Qnil;
}

#closed?Boolean

Returns:

  • (Boolean)


2582
2583
2584
2585
2586
# File 'ext/multi_compress/multi_compress.c', line 2582

static VALUE deflater_closed_p(VALUE self) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
    return d->closed ? Qtrue : Qfalse;
}

#finishObject



2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
# File 'ext/multi_compress/multi_compress.c', line 2395

static VALUE deflater_finish(VALUE self) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
    if (d->closed)
        rb_raise(eStreamError, "stream is closed");
    if (d->finished)
        return rb_binary_str_new("", 0);
    d->finished = 1;

    switch (d->algo) {
    case ALGO_ZSTD: {
        size_t out_cap = ZSTD_CStreamOutSize();
        size_t result_cap = out_cap;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;
        size_t ret;

        do {
            if (result_len + out_cap > result_cap) {
                result_cap *= 2;
                grow_binary_str(result, result_len, result_cap);
            }

            ZSTD_outBuffer output = {RSTRING_PTR(result) + result_len, out_cap, 0};
            ret = ZSTD_endStream(d->ctx.zstd, &output);
            if (ZSTD_isError(ret))
                rb_raise(eError, "zstd end stream: %s", ZSTD_getErrorName(ret));
            result_len += output.pos;
        } while (ret > 0);

        rb_str_set_len(result, result_len);
        return result;
    }
    case ALGO_BROTLI: {
        size_t available_in = 0;
        const uint8_t *next_in = NULL;
        size_t result_cap = 1024;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;

        do {
            size_t available_out = 0;
            uint8_t *next_out = NULL;
            BROTLI_BOOL ok =
                BrotliEncoderCompressStream(d->ctx.brotli, BROTLI_OPERATION_FINISH, &available_in,
                                            &next_in, &available_out, &next_out, NULL);
            if (!ok)
                rb_raise(eError, "brotli finish failed");
            const uint8_t *out_data;
            size_t out_size = 0;
            out_data = BrotliEncoderTakeOutput(d->ctx.brotli, &out_size);
            if (out_size > 0) {
                if (result_len + out_size > result_cap) {
                    result_cap = (result_len + out_size) * 2;
                    grow_binary_str(result, result_len, result_cap);
                }

                memcpy(RSTRING_PTR(result) + result_len, out_data, out_size);
                result_len += out_size;
            }
        } while (BrotliEncoderHasMoreOutput(d->ctx.brotli) ||
                 !BrotliEncoderIsFinished(d->ctx.brotli));

        rb_str_set_len(result, result_len);
        return result;
    }
    case ALGO_LZ4: {
        size_t result_cap = 256;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;

        if (d->lz4_ring.pending > 0) {
            VALUE block = lz4_compress_ring_block(d);
            size_t blen = RSTRING_LEN(block);
            if (blen > 0) {
                if (blen + 4 > result_cap) {
                    result_cap = blen + 4;
                    grow_binary_str(result, result_len, result_cap);
                }

                memcpy(RSTRING_PTR(result), RSTRING_PTR(block), blen);
                result_len = blen;
            }
        }

        if (result_len + 4 > result_cap) {
            result_cap = result_len + 4;
            grow_binary_str(result, result_len, result_cap);
        }

        char *out = RSTRING_PTR(result) + result_len;
        write_le_u32((uint8_t *)out, 0);
        result_len += 4;

        rb_str_set_len(result, result_len);
        return result;
    }
    }
    return rb_binary_str_new("", 0);
}

#flushObject



2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
# File 'ext/multi_compress/multi_compress.c', line 2325

static VALUE deflater_flush(VALUE self) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
    if (d->closed)
        rb_raise(eStreamError, "stream is closed");
    if (d->finished)
        rb_raise(eStreamError, "stream is already finished");

    switch (d->algo) {
    case ALGO_ZSTD: {
        size_t out_cap = ZSTD_CStreamOutSize();
        size_t result_cap = out_cap;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;
        size_t ret;

        do {
            if (result_len + out_cap > result_cap) {
                result_cap *= 2;
                grow_binary_str(result, result_len, result_cap);
            }

            ZSTD_outBuffer output = {RSTRING_PTR(result) + result_len, out_cap, 0};
            ret = ZSTD_flushStream(d->ctx.zstd, &output);
            if (ZSTD_isError(ret))
                rb_raise(eError, "zstd flush: %s", ZSTD_getErrorName(ret));
            result_len += output.pos;
        } while (ret > 0);

        rb_str_set_len(result, result_len);
        return result;
    }
    case ALGO_BROTLI: {
        size_t available_in = 0;
        const uint8_t *next_in = NULL;
        size_t result_cap = 1024;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;

        do {
            size_t available_out = 0;
            uint8_t *next_out = NULL;
            BROTLI_BOOL ok =
                BrotliEncoderCompressStream(d->ctx.brotli, BROTLI_OPERATION_FLUSH, &available_in,
                                            &next_in, &available_out, &next_out, NULL);
            if (!ok)
                rb_raise(eError, "brotli flush failed");
            const uint8_t *out_data;
            size_t out_size = 0;
            out_data = BrotliEncoderTakeOutput(d->ctx.brotli, &out_size);
            if (out_size > 0) {
                if (result_len + out_size > result_cap) {
                    result_cap = (result_len + out_size) * 2;
                    grow_binary_str(result, result_len, result_cap);
                }

                memcpy(RSTRING_PTR(result) + result_len, out_data, out_size);
                result_len += out_size;
            }
        } while (BrotliEncoderHasMoreOutput(d->ctx.brotli));

        rb_str_set_len(result, result_len);
        return result;
    }
    case ALGO_LZ4:
        return lz4_compress_ring_block(d);
    }
    return rb_binary_str_new("", 0);
}

#resetObject



2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
# File 'ext/multi_compress/multi_compress.c', line 2496

static VALUE deflater_reset(VALUE self) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);

    VALUE dict_val = dictionary_ivar_get(self);
    dictionary_t *dict = NULL;
    if (!NIL_P(dict_val)) {
        TypedData_Get_Struct(dict_val, dictionary_t, &dictionary_type, dict);
    }

    switch (d->algo) {
    case ALGO_ZSTD:
        if (d->ctx.zstd) {
            ZSTD_CCtx_reset(d->ctx.zstd, ZSTD_reset_session_only);
            ZSTD_CCtx_setParameter(d->ctx.zstd, ZSTD_c_compressionLevel, d->level);
            if (dict) {
                size_t r = ZSTD_CCtx_loadDictionary(d->ctx.zstd, dict->data, dict->size);
                if (ZSTD_isError(r))
                    rb_raise(eError, "zstd dict reload on reset: %s", ZSTD_getErrorName(r));
            }
        }
        break;
    case ALGO_BROTLI:
        if (d->ctx.brotli) {
            BrotliEncoderDestroyInstance(d->ctx.brotli);
            d->ctx.brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL);
            if (!d->ctx.brotli)
                rb_raise(eMemError, "brotli: failed to recreate encoder");
            if (!BrotliEncoderSetParameter(d->ctx.brotli, BROTLI_PARAM_QUALITY, d->level))
                rb_raise(eError, "brotli: failed to set quality on reset");
            if (dict) {
                BrotliEncoderPreparedDictionary *pd = BrotliEncoderPrepareDictionary(
                    BROTLI_SHARED_DICTIONARY_RAW, dict->size, dict->data, BROTLI_MAX_QUALITY, NULL,
                    NULL, NULL);
                if (!pd)
                    rb_raise(eMemError, "brotli: failed to prepare dictionary on reset");
                if (!BrotliEncoderAttachPreparedDictionary(d->ctx.brotli, pd)) {
                    BrotliEncoderDestroyPreparedDictionary(pd);
                    rb_raise(eError, "brotli: failed to reattach dictionary on reset");
                }
                BrotliEncoderDestroyPreparedDictionary(pd);
            }
        }
        break;
    case ALGO_LZ4:
        if (d->ctx.lz4)
            LZ4_resetStream(d->ctx.lz4);
        d->lz4_ring.ring_offset = 0;
        d->lz4_ring.pending = 0;
        break;
    }
    d->closed = 0;
    d->finished = 0;
    return self;
}

#write(chunk) ⇒ Object



2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
# File 'ext/multi_compress/multi_compress.c', line 2135

static VALUE deflater_write(VALUE self, VALUE chunk) {
    deflater_t *d;
    TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
    if (d->closed)
        rb_raise(eStreamError, "stream is closed");
    if (d->finished)
        rb_raise(eStreamError, "stream is already finished");
    StringValue(chunk);

    const char *src = RSTRING_PTR(chunk);
    size_t slen = RSTRING_LEN(chunk);
    const algo_policy_t *policy = algo_policy(d->algo);
    if (slen == 0)
        return rb_binary_str_new("", 0);

    switch (d->algo) {
    case ALGO_ZSTD: {
        ZSTD_inBuffer input = {src, slen, 0};
        size_t out_cap = ZSTD_CStreamOutSize();
        size_t result_cap = out_cap > slen ? out_cap : slen;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;
        VALUE scheduler = current_fiber_scheduler();

        while (input.pos < input.size) {
            if (result_len + out_cap > result_cap) {
                result_cap = result_cap * 2;
                grow_binary_str(result, result_len, result_cap);
            }

            ZSTD_outBuffer output = {RSTRING_PTR(result) + result_len, out_cap, 0};

            {
                work_exec_mode_t mode = select_fiber_nogvl_or_direct_mode(
                    scheduler, input.size - input.pos, policy->fiber_stream_threshold,
                    policy->gvl_unlock_threshold);

                if (mode == WORK_EXEC_FIBER) {
                    zstd_stream_chunk_fiber_t fargs = {
                        .cstream = d->ctx.zstd,
                        .input = input,
                        .output = output,
                        .result = 0,
                    };
                    RUN_WITH_EXEC_MODE(mode, zstd_stream_chunk_fiber_nogvl, fargs);
                    input.pos = fargs.input.pos;
                    output.pos = fargs.output.pos;

                    if (ZSTD_isError(fargs.result))
                        rb_raise(eError, "zstd compress stream: %s",
                                 ZSTD_getErrorName(fargs.result));
                } else if (mode == WORK_EXEC_NOGVL) {
                    zstd_stream_chunk_args_t args = {
                        .cstream = d->ctx.zstd,
                        .output = &output,
                        .input = &input,
                        .result = 0,
                    };
                    RUN_WITH_EXEC_MODE(mode, zstd_compress_stream_chunk_nogvl, args);
                    if (ZSTD_isError(args.result))
                        rb_raise(eError, "zstd compress stream: %s",
                                 ZSTD_getErrorName(args.result));
                } else {
                    size_t ret = ZSTD_compressStream(d->ctx.zstd, &output, &input);
                    if (ZSTD_isError(ret))
                        rb_raise(eError, "zstd compress stream: %s", ZSTD_getErrorName(ret));
                }
            }
            result_len += output.pos;
        }
        rb_str_set_len(result, result_len);
        RB_GC_GUARD(chunk);
        return result;
    }
    case ALGO_BROTLI: {
        size_t available_in = slen;
        const uint8_t *next_in = (const uint8_t *)src;
        size_t result_cap = slen;
        if (result_cap < 1024)
            result_cap = 1024;
        VALUE result = rb_binary_str_buf_reserve(result_cap);
        size_t result_len = 0;
        VALUE scheduler = current_fiber_scheduler();
        int use_fiber = (scheduler != Qnil);
        size_t fiber_counter = 0;

        while (available_in > 0 || BrotliEncoderHasMoreOutput(d->ctx.brotli)) {
            size_t available_out = 0;
            uint8_t *next_out = NULL;
            BROTLI_BOOL ok;

            if (use_fiber &&
                select_fiber_or_direct_mode(scheduler, available_in,
                                            policy->fiber_stream_threshold) == WORK_EXEC_FIBER) {
                brotli_stream_chunk_fiber_t fargs = {
                    .enc = d->ctx.brotli,
                    .op = BROTLI_OPERATION_PROCESS,
                    .available_in = available_in,
                    .next_in = next_in,
                    .available_out = available_out,
                    .next_out = next_out,
                    .result = BROTLI_FALSE,
                };
                RUN_VIA_FIBER_WORKER(brotli_stream_chunk_fiber_nogvl, fargs);
                available_in = fargs.available_in;
                next_in = fargs.next_in;
                available_out = fargs.available_out;
                next_out = fargs.next_out;
                ok = fargs.result;
            } else {
                ok = BrotliEncoderCompressStream(d->ctx.brotli, BROTLI_OPERATION_PROCESS,
                                                 &available_in, &next_in, &available_out, &next_out,
                                                 NULL);
            }
            if (!ok)
                rb_raise(eError, "brotli compress stream failed");

            const uint8_t *out_data;
            size_t out_size = 0;
            out_data = BrotliEncoderTakeOutput(d->ctx.brotli, &out_size);
            if (out_size > 0) {
                if (result_len + out_size > result_cap) {
                    result_cap = (result_len + out_size) * 2;
                    grow_binary_str(result, result_len, result_cap);
                }

                memcpy(RSTRING_PTR(result) + result_len, out_data, out_size);
                result_len += out_size;
                if (use_fiber) {
                    int did_yield = 0;
                    fiber_counter = fiber_maybe_yield(fiber_counter, out_size,
                                                      policy->fiber_yield_chunk, &did_yield);
                    (void)did_yield;
                }
            }
        }
        rb_str_set_len(result, result_len);
        RB_GC_GUARD(chunk);
        return result;
    }
    case ALGO_LZ4: {
        VALUE result = rb_binary_str_buf_reserve(0);
        size_t result_len = 0;
        size_t result_cap = 0;
        int use_fiber = has_fiber_scheduler();
        size_t fiber_counter = 0;

        while (slen > 0) {
            size_t space = LZ4_RING_BUFFER_SIZE - d->lz4_ring.pending;
            size_t copy = slen < space ? slen : space;

            if (d->lz4_ring.ring_offset + copy > LZ4_RING_BUFFER_TOTAL) {
                rb_raise(eError, "lz4: ring buffer overflow");
            }

            memcpy(d->lz4_ring.buf + d->lz4_ring.ring_offset, src, copy);
            d->lz4_ring.ring_offset += copy;
            d->lz4_ring.pending += copy;
            src += copy;
            slen -= copy;

            if (d->lz4_ring.pending >= (size_t)LZ4_RING_BUFFER_SIZE) {
                VALUE block = lz4_compress_ring_block(d);
                size_t blen = RSTRING_LEN(block);
                if (blen > 0) {
                    if (result_len + blen > result_cap) {
                        result_cap = (result_len + blen) * 2;
                        if (result_cap < 256)
                            result_cap = 256;
                        grow_binary_str(result, result_len, result_cap);
                    }
                    memcpy(RSTRING_PTR(result) + result_len, RSTRING_PTR(block), blen);
                    result_len += blen;
                }
                if (use_fiber) {
                    int did_yield = 0;
                    fiber_counter = fiber_maybe_yield(fiber_counter, LZ4_RING_BUFFER_SIZE,
                                                      policy->fiber_yield_chunk, &did_yield);
                    (void)did_yield;
                }
            }
        }
        rb_str_set_len(result, result_len);
        RB_GC_GUARD(chunk);
        return result;
    }
    }
    return rb_binary_str_new("", 0);
}