---
title: bastie/bzip2
framework: Swift Package Catalog
role: article
path: packages/bastie/bzip2
---

# bastie/bzip2

This is a working **proof of value** port of original BZip2 code.

## Compare User Time

*User Time*, the time user wait for ending of process.

| BZip2          | Version             | enwik9.tar     |      % | | -------------- | ------------------- | -------------- | ------ | | 1.0.8          | original C          | 0:48,949       | 100.00 | | 1.0.8          | parallel Swift      | 0:42,478       |  86.78 | | 1.0.8          | parallel, GPU Swift | 0:33,695       |  68.84 |

## Goals

0. Can an other AI write a good prompt for this task. 1. Let an AI convert a tool from one language to other, especially to Swift. 2. How expense is this translation. 3. What are main errors from AI.

### no goal

0. the code quality, because programms has to work at the user computer and no code had to be blinking in the eye of coder. The test cases are only on business requirements.

## Solutions

0. Yes, with minimal additions and give to the AI the original source code and the original testcases. 1. It works in less than one hour with a "normal AI model" like Claude Sonnet 5. 2. The AI works and I as good developer drink coffee and eat pizza. 3. First solution has many errors. So it is important to required tests, more tests, much more tests. Give these errors to AI, without hints, and the AI correct themselfe. Only one big error and this was a newbie mistake to ignore a pipe can be busy.

Plus: The port is for the user faster than original, because AI insert multithreading.

```bash Maxmilian:enwik$ ls -lisa enwik9.tar                                             432127629 1953128 -rw-r--r--  1 bastie  staff  1000001536  3 Juli 16:37 enwik9.tar Maxmilian:enwik$ bzip2 --version                                                 bzip2, a block-sorting file compressor.  Version 1.0.8, 13-Jul-2019.        Copyright (C) 1996-2019 by Julian Seward.        This program is free software; you can redistribute it and/or modify    it under the terms set out in the LICENSE file, which is included    in the bzip2 source distribution.        This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    LICENSE file for more details.     bzip2: I won´t write compressed data to a terminal. bzip2: For help, type: ´bzip2 --help´. Maxmilian:enwik$ time bzip2 -9kf enwik9.tar                                      bzip2 -9kf enwik9.tar  48,21s user 0,71s system 99% cpu 48,946 total Maxmilian:enwik$ ls -lisa enwik9.tar.bz2    432481345 525312 -rw-r--r--  1 bastie  staff  253985866  3 Juli 16:37 enwik9.tar.bz2 Maxmilian:enwik$ md5sum enwik9.tar.bz2                                           0665db0a37cf230f276287b56e7a1275  enwik9.tar.bz2 Maxmilian:enwik$ time ./.build/release/bzip2 -9kf enwik9.tar ./.build/release/bzip2 -9kf enwik9.tar  436,37s user 2,25s system 1032% cpu 42,478 total Maxmilian:enwik$ ls -lisa enwik9.tar.bz2                                           432481345 496072 -rw-r--r--  1 bastie  staff  253985866  7 Juli 14:21 enwik9.tar.bz2 Maxmilian:enwik$ md5sum enwik9.tar.bz2                                             0665db0a37cf230f276287b56e7a1275  enwik9.tar.bz2 Maxmilian:enwik$ ```

## Library (`BZip2Core`)

Besides the `bzip2` CLI, this package also builds a `BZip2Core` library product for other Swift projects:

```swift .package(path: "../bsip2"), // or a git URL/tag once published ```

```swift .product(name: "BZip2Core", package: "bzip2") ```

```swift import BZip2Core

let compressed = try await BZip2.compress(data, level: .default, strategy: .manyCoreGPU) let original = try BZip2.decompress(compressed) ```

`strategy` (`BZip2ComputeStrategy`, see `ADR-0012`) picks between `.singleCore` (no concurrency), `.multiCore` (the default -- fans blocks out across available CPU cores), and `.manyCoreGPU` (also tries a GPU- accelerated suffix sort per block, throwing `BZip2Error.gpuUnavailable` if no usable GPU exists on the system at all -- check `BZip2ComputeStrategy.isManyCoreGPUAvailable` first to avoid that).

`BZip2Core`'s public API is deliberately **not** a port of the original C `bzlib.h` interface (no `bz_stream`/`BZ2_bzCompress*` state machine) -- it's a Swift-idiomatic, whole-buffer `async`/`throws` API instead. See `LASTENHEFT-BZip2Core.md` for the requirements behind that decision and `ADR-0011`/`ADR-0012` (in `Sources/BZip2Core/bzip2.docc/`) for the implementation.

## Version

### [BZip2 1.0.8 (GPU)](https://github.com/bastie/bzip2/releases/tag/1.0.8_swift-1.1.0)

- BZip2 1.0.8 with GPU support.

### [BZip2 1.0.8 (Parallel)](https://github.com/bastie/bzip2/releases/tag/1.0.8_swift-1.0.0)

- BZip2 1.0.8 ported from C to Swift with AI. It contains also multithreading.

## Package Metadata

Repository: bastie/bzip2

Default branch: main

README: README.md
