diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 81044ae3..30f2755c 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -64,6 +64,127 @@ jobs:
- name: Run tests
run: cargo test --all-features --workspace
+ cross-compile-aarch64:
+ runs-on: ubuntu-latest
+ name: Cross-compile for aarch64
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Install Rust
+ uses: dtolnay/rust-toolchain@nightly
+ with:
+ targets: aarch64-unknown-linux-gnu
+
+ - name: Setup Rust cache
+ uses: Swatinem/rust-cache@v2
+ with:
+ shared-key: "cross-compile-aarch64"
+ cache-on-failure: true
+
+ - name: Install cross
+ run: cargo install cross --locked
+
+ - name: Build library for aarch64
+ run: cross build --lib --target aarch64-unknown-linux-gnu
+
+ - name: Build tests for aarch64
+ run: cross build --tests --target aarch64-unknown-linux-gnu
+
+ - name: Verify aarch64 build
+ run: |
+ echo "✓ aarch64-unknown-linux-gnu build succeeded"
+ echo " - Architecture separation validated"
+ echo " - x86_64 backends: hasher_input, md5x2_scalar, md5x2_sse2,"
+ echo " md5x2_bmi1, md5x2_avx512"
+ echo " - aarch64 backends: md5x2_scalar, md5x2_neon, crc_armcrc"
+ echo " - Shared: md5x2 trait"
+
+ build-windows-binaries:
+ runs-on: windows-latest
+ name: Build Windows binaries
+ if: github.event_name == 'push' && github.ref == 'refs/heads/main'
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Install Rust
+ uses: dtolnay/rust-toolchain@nightly
+ with:
+ targets: x86_64-pc-windows-msvc
+
+ - name: Setup Rust cache
+ uses: Swatinem/rust-cache@v2
+ with:
+ shared-key: "build-windows-binaries"
+ cache-on-failure: true
+
+ - name: Build release binaries
+ run: cargo build --release --bins --target x86_64-pc-windows-msvc
+
+ - name: Package Windows binaries
+ shell: pwsh
+ run: |
+ New-Item -ItemType Directory -Force -Path dist | Out-Null
+ Compress-Archive `
+ -Path `
+ target/x86_64-pc-windows-msvc/release/par2.exe, `
+ target/x86_64-pc-windows-msvc/release/par2create.exe, `
+ target/x86_64-pc-windows-msvc/release/par2repair.exe, `
+ target/x86_64-pc-windows-msvc/release/par2verify.exe `
+ -DestinationPath dist/par2rs-windows-x86_64.zip
+
+ - name: Upload Windows binaries
+ uses: actions/upload-artifact@v4
+ with:
+ name: par2rs-windows-x86_64
+ path: |
+ dist/par2rs-windows-x86_64.zip
+ target/x86_64-pc-windows-msvc/release/par2.exe
+ target/x86_64-pc-windows-msvc/release/par2create.exe
+ target/x86_64-pc-windows-msvc/release/par2repair.exe
+ target/x86_64-pc-windows-msvc/release/par2verify.exe
+
+ build-linux-x86_64-binaries:
+ runs-on: ubuntu-latest
+ name: Build Linux x86_64 binaries
+ if: github.event_name == 'push' && github.ref == 'refs/heads/main'
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Install Rust
+ uses: dtolnay/rust-toolchain@nightly
+ with:
+ targets: x86_64-unknown-linux-gnu
+
+ - name: Setup Rust cache
+ uses: Swatinem/rust-cache@v2
+ with:
+ shared-key: "build-linux-x86_64-binaries"
+ cache-on-failure: true
+
+ - name: Build release binaries
+ run: cargo build --release --bins --target x86_64-unknown-linux-gnu
+
+ - name: Package Linux x86_64 binaries
+ run: |
+ mkdir -p dist
+ tar -C target/x86_64-unknown-linux-gnu/release -czf \
+ dist/par2rs-linux-x86_64-nightly.tar.gz \
+ par2 par2create par2repair par2verify
+
+ - name: Upload Linux x86_64 binaries
+ uses: actions/upload-artifact@v4
+ with:
+ name: par2rs-linux-x86_64-nightly
+ path: |
+ dist/par2rs-linux-x86_64-nightly.tar.gz
+ target/x86_64-unknown-linux-gnu/release/par2
+ target/x86_64-unknown-linux-gnu/release/par2create
+ target/x86_64-unknown-linux-gnu/release/par2repair
+ target/x86_64-unknown-linux-gnu/release/par2verify
+
coverage:
runs-on: ubuntu-latest
diff --git a/Cargo.lock b/Cargo.lock
index ebedbc23..ead4b0f8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -166,6 +166,16 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
+[[package]]
+name = "cc"
+version = "1.2.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d16d90359e986641506914ba71350897565610e87ce0ad9e6f28569db3dd5c6d"
+dependencies = [
+ "find-msvc-tools",
+ "shlex",
+]
+
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -238,6 +248,16 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dabb6555f92fb9ee4140454eb5dcd14c7960e1225c6d1a6cc361f032947713e"
+[[package]]
+name = "crc-fast"
+version = "1.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e75b2483e97a5a7da73ac68a05b629f9c53cff58d8ed1c77866079e18b00dba5"
+dependencies = [
+ "digest 0.10.7",
+ "spin",
+]
+
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -311,6 +331,16 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
+[[package]]
+name = "crypto-common"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
+dependencies = [
+ "generic-array",
+ "typenum",
+]
+
[[package]]
name = "crypto-common"
version = "0.2.0-rc.5"
@@ -340,6 +370,15 @@ dependencies = [
"syn",
]
+[[package]]
+name = "digest"
+version = "0.10.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
+dependencies = [
+ "crypto-common 0.1.7",
+]
+
[[package]]
name = "digest"
version = "0.11.0-rc.4"
@@ -348,7 +387,7 @@ checksum = "ea390c940e465846d64775e55e3115d5dc934acb953de6f6e6360bc232fe2bf7"
dependencies = [
"block-buffer",
"const-oid",
- "crypto-common",
+ "crypto-common 0.2.0-rc.5",
]
[[package]]
@@ -396,12 +435,28 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
+[[package]]
+name = "find-msvc-tools"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
+
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+[[package]]
+name = "generic-array"
+version = "0.14.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
[[package]]
name = "getrandom"
version = "0.3.4"
@@ -555,7 +610,7 @@ version = "0.11.0-rc.2"
source = "git+https://github.com/mjc/hashes.git?branch=mjc-asm#6a68d94060f1675411fd6ecebfd0fff36d1fef8e"
dependencies = [
"cfg-if",
- "digest",
+ "digest 0.11.0-rc.4",
]
[[package]]
@@ -604,12 +659,15 @@ dependencies = [
"anyhow",
"binrw",
"bytemuck",
+ "cc",
"clap",
+ "crc-fast",
"crc32fast",
"criterion",
"env_logger",
"hex",
"iai-callgrind",
+ "libc",
"log",
"md-5",
"proptest",
@@ -619,6 +677,7 @@ dependencies = [
"smallvec",
"tempfile",
"thiserror",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -926,12 +985,24 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "shlex"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
+[[package]]
+name = "spin"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591"
+
[[package]]
name = "strsim"
version = "0.11.1"
@@ -1016,6 +1087,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+[[package]]
+name = "version_check"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+
[[package]]
name = "wait-timeout"
version = "0.2.1"
diff --git a/Cargo.toml b/Cargo.toml
index ff3e17b4..9924795c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,11 +2,13 @@
name = "par2rs"
version = "0.1.0"
edition = "2021"
-license = "GPL-3.0-only"
+license = "GPL-2.0-or-later"
[features]
# Feature flag to skip tests that are unsuitable in Nix builds
nix-build = []
+# Feature for ParPar C++ FFI comparison (benchmarks only, adds build time)
+parpar-compare = []
[dependencies]
binrw = "*"
@@ -24,6 +26,10 @@ rustc-hash = "2.1"
thiserror = "2.0.17"
anyhow = "1.0"
smallvec = { version = "1.15.1", features = ["const_generics"] }
+libc = "0.2"
+
+[target.'cfg(windows)'.dependencies]
+windows-sys = { version = "0.61", features = ["Win32_System_Memory", "Win32_System_SystemInformation"] }
# Binaries
[[bin]]
@@ -42,6 +48,10 @@ path = "src/bin/par2repair.rs"
name = "par2create"
path = "src/bin/par2create.rs"
+[build-dependencies]
+# C++ compiler for conditional ParPar FFI compilation
+cc = "1.0"
+
[dev-dependencies]
# Temporary directory management for tests
tempfile = "3"
@@ -53,6 +63,8 @@ iai-callgrind = "0.16"
proptest = "1.5"
# Random number generation for tests
rand = "0.9"
+# CRC32 alternative under evaluation vs crc32fast (bench-only for now)
+crc-fast = "1.4"
[[bench]]
name = "repair_benchmark"
@@ -78,6 +90,42 @@ harness = false
name = "iai_simd"
harness = false
+[[bench]]
+name = "fused_hashing"
+harness = false
+
+[[bench]]
+name = "crc_compare"
+harness = false
+
+[[bench]]
+name = "md5x2_crc_fused"
+harness = false
+
+[[bench]]
+name = "parpar_hasher_input"
+harness = false
+
+[[bench]]
+name = "iai_hasher_input"
+harness = false
+
+[[bench]]
+name = "compare_with_parpar"
+harness = false
+
+[[bench]]
+name = "iai_parpar_comparison"
+harness = false
+
+[[bench]]
+name = "par2verify_compare"
+harness = false
+
+[[bench]]
+name = "iai_par2verify_compare"
+harness = false
+
[profile.release]
debug = true
strip = false
diff --git a/LICENSE b/LICENSE
index f288702d..9efa6fbc 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,622 +1,281 @@
GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
+ Version 2, June 1991
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
- The GNU General Public License is a free, copyleft license for
-software and other kinds of works.
-
- The licenses for most software and other practical works are designed
-to take away your freedom to share and change the works. By contrast,
-the GNU General Public License is intended to guarantee your freedom to
-share and change all versions of a program--to make sure it remains free
-software for all its users. We, the Free Software Foundation, use the
-GNU General Public License for most of our software; it applies also to
-any other work released this way by its authors. You can apply it to
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
-them if you wish), that you receive source code or can get it if you
-want it, that you can change the software or use pieces of it in new
-free programs, and that you know you can do these things.
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
- To protect your rights, we need to prevent others from denying you
-these rights or asking you to surrender the rights. Therefore, you have
-certain responsibilities if you distribute copies of the software, or if
-you modify it: responsibilities to respect the freedom of others.
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must pass on to the recipients the same
-freedoms that you received. You must make sure that they, too, receive
-or can get the source code. And you must show them these terms so they
-know their rights.
-
- Developers that use the GNU GPL protect your rights with two steps:
-(1) assert copyright on the software, and (2) offer you this License
-giving you legal permission to copy, distribute and/or modify it.
-
- For the developers' and authors' protection, the GPL clearly explains
-that there is no warranty for this free software. For both users' and
-authors' sake, the GPL requires that modified versions be marked as
-changed, so that their problems will not be attributed erroneously to
-authors of previous versions.
-
- Some devices are designed to deny users access to install or run
-modified versions of the software inside them, although the manufacturer
-can do so. This is fundamentally incompatible with the aim of
-protecting users' freedom to change the software. The systematic
-pattern of such abuse occurs in the area of products for individuals to
-use, which is precisely where it is most unacceptable. Therefore, we
-have designed this version of the GPL to prohibit the practice for those
-products. If such problems arise substantially in other domains, we
-stand ready to extend this provision to those domains in future versions
-of the GPL, as needed to protect the freedom of users.
-
- Finally, every program is threatened constantly by software patents.
-States should not allow patents to restrict development and use of
-software on general-purpose computers, but in those that do, we wish to
-avoid the special danger that patents applied to a free program could
-make it effectively proprietary. To prevent this, the GPL assures that
-patents cannot be used to render the program non-free.
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
- TERMS AND CONDITIONS
-
- 0. Definitions.
-
- "This License" refers to version 3 of the GNU General Public License.
-
- "Copyright" also means copyright-like laws that apply to other kinds of
-works, such as semiconductor masks.
-
- "The Program" refers to any copyrightable work licensed under this
-License. Each licensee is addressed as "you". "Licensees" and
-"recipients" may be individuals or organizations.
-
- To "modify" a work means to copy from or adapt all or part of the work
-in a fashion requiring copyright permission, other than the making of an
-exact copy. The resulting work is called a "modified version" of the
-earlier work or a work "based on" the earlier work.
-
- A "covered work" means either the unmodified Program or a work based
-on the Program.
-
- To "propagate" a work means to do anything with it that, without
-permission, would make you directly or secondarily liable for
-infringement under applicable copyright law, except executing it on a
-computer or modifying a private copy. Propagation includes copying,
-distribution (with or without modification), making available to the
-public, and in some countries other activities as well.
-
- To "convey" a work means any kind of propagation that enables other
-parties to make or receive copies. Mere interaction with a user through
-a computer network, with no transfer of a copy, is not conveying.
-
- An interactive user interface displays "Appropriate Legal Notices"
-to the extent that it includes a convenient and prominently visible
-feature that (1) displays an appropriate copyright notice, and (2)
-tells the user that there is no warranty for the work (except to the
-extent that warranties are provided), that licensees may convey the
-work under this License, and how to view a copy of this License. If
-the interface presents a list of user commands or options, such as a
-menu, a prominent item in the list meets this criterion.
-
- 1. Source Code.
-
- The "source code" for a work means the preferred form of the work
-for making modifications to it. "Object code" means any non-source
-form of a work.
-
- A "Standard Interface" means an interface that either is an official
-standard defined by a recognized standards body, or, in the case of
-interfaces specified for a particular programming language, one that
-is widely used among developers working in that language.
-
- The "System Libraries" of an executable work include anything, other
-than the work as a whole, that (a) is included in the normal form of
-packaging a Major Component, but which is not part of that Major
-Component, and (b) serves only to enable use of the work with that
-Major Component, or to implement a Standard Interface for which an
-implementation is available to the public in source code form. A
-"Major Component", in this context, means a major essential component
-(kernel, window system, and so on) of the specific operating system
-(if any) on which the executable work runs, or a compiler used to
-produce the work, or an object code interpreter used to run it.
-
- The "Corresponding Source" for a work in object code form means all
-the source code needed to generate, install, and (for an executable
-work) run the object code and to modify the work, including scripts to
-control those activities. However, it does not include the work's
-System Libraries, or general-purpose tools or generally available free
-programs which are used unmodified in performing those activities but
-which are not part of the work. For example, Corresponding Source
-includes interface definition files associated with source files for
-the work, and the source code for shared libraries and dynamically
-linked subprograms that the work is specifically designed to require,
-such as by intimate data communication or control flow between those
-subprograms and other parts of the work.
-
- The Corresponding Source need not include anything that users
-can regenerate automatically from other parts of the Corresponding
-Source.
-
- The Corresponding Source for a work in source code form is that
-same work.
-
- 2. Basic Permissions.
-
- All rights granted under this License are granted for the term of
-copyright on the Program, and are irrevocable provided the stated
-conditions are met. This License explicitly affirms your unlimited
-permission to run the unmodified Program. The output from running a
-covered work is covered by this License only if the output, given its
-content, constitutes a covered work. This License acknowledges your
-rights of fair use or other equivalent, as provided by copyright law.
-
- You may make, run and propagate covered works that you do not
-convey, without conditions so long as your license otherwise remains
-in force. You may convey covered works to others for the sole purpose
-of having them make modifications exclusively for you, or provide you
-with facilities for running those works, provided that you comply with
-the terms of this License in conveying all material for which you do
-not control copyright. Those thus making or running the covered works
-for you must do so exclusively on your behalf, under your direction
-and control, on terms that prohibit them from making any copies of
-your copyrighted material outside their relationship with you.
-
- Conveying under any other circumstances is permitted solely under
-the conditions stated below. Sublicensing is not allowed; section 10
-makes it unnecessary.
-
- 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
-
- No covered work shall be deemed part of an effective technological
-measure under any applicable law fulfilling obligations under article
-11 of the WIPO copyright treaty adopted on 20 December 1996, or
-similar laws prohibiting or restricting circumvention of such
-measures.
-
- When you convey a covered work, you waive any legal power to forbid
-circumvention of technological measures to the extent such circumvention
-is effected by exercising rights under this License with respect to
-the covered work, and you disclaim any intention to limit operation or
-modification of the work as a means of enforcing, against the work's
-users, your or third parties' legal rights to forbid circumvention of
-technological measures.
-
- 4. Conveying Verbatim Copies.
-
- You may convey verbatim copies of the Program's source code as you
-receive it, in any medium, provided that you conspicuously and
-appropriately publish on each copy an appropriate copyright notice;
-keep intact all notices stating that this License and any
-non-permissive terms added in accord with section 7 apply to the code;
-keep intact all notices of the absence of any warranty; and give all
-recipients a copy of this License along with the Program.
-
- You may charge any price or no price for each copy that you convey,
-and you may offer support or warranty protection for a fee.
-
- 5. Conveying Modified Source Versions.
-
- You may convey a work based on the Program, or the modifications to
-produce it from the Program, in the form of source code under the
-terms of section 4, provided that you also meet all of these conditions:
-
- a) The work must carry prominent notices stating that you modified
- it, and giving a relevant date.
-
- b) The work must carry prominent notices stating that it is
- released under this License and any conditions added under section
- 7. This requirement modifies the requirement in section 4 to
- "keep intact all notices".
-
- c) You must license the entire work, as a whole, under this
- License to anyone who comes into possession of a copy. This
- License will therefore apply, along with any applicable section 7
- additional terms, to the whole of the work, and all its parts,
- regardless of how they are packaged. This License gives no
- permission to license the work in any other way, but it does not
- invalidate such permission if you have separately received it.
-
- d) If the work has interactive user interfaces, each must display
- Appropriate Legal Notices; however, if the Program has interactive
- interfaces that do not display Appropriate Legal Notices, your
- work need not make them do so.
-
- A compilation of a covered work with other separate and independent
-works, which are not by their nature extensions of the covered work,
-and which are not combined with it such as to form a larger program,
-in or on a volume of a storage or distribution medium, is called an
-"aggregate" if the compilation and its resulting copyright are not
-used to limit the access or legal rights of the compilation's users
-beyond what the individual works permit. Inclusion of a covered work
-in an aggregate does not cause this License to apply to the other
-parts of the aggregate.
-
- 6. Conveying Non-Source Forms.
-
- You may convey a covered work in object code form under the terms
-of sections 4 and 5, provided that you also convey the
-machine-readable Corresponding Source under the terms of this License,
-in one of these ways:
-
- a) Convey the object code in, or embodied in, a physical product
- (including a physical distribution medium), accompanied by the
- Corresponding Source fixed on a durable physical medium
- customarily used for software interchange.
-
- b) Convey the object code in, or embodied in, a physical product
- (including a physical distribution medium), accompanied by a
- written offer, valid for at least three years and valid for as
- long as you offer spare parts or customer support for that product
- model, to give anyone who possesses the object code either (1) a
- copy of the Corresponding Source for all the software in the
- product that is covered by this License, on a durable physical
- medium customarily used for software interchange, for a price no
- more than your reasonable cost of physically performing this
- conveying of source, or (2) access to copy the
- Corresponding Source from a network server at no charge.
-
- c) Convey individual copies of the object code with a copy of the
- written offer to provide the Corresponding Source. This
- alternative is allowed only occasionally and noncommercially, and
- only if you received the object code with such an offer, in accord
- with subsection 6b.
-
- d) Convey the object code by offering access from a designated
- place (gratis or for a charge), and offer equivalent access to the
- Corresponding Source in the same way through the same place at no
- further charge. You need not require recipients to copy the
- Corresponding Source along with the object code. If the place to
- copy the object code is a network server, the Corresponding Source
- may be on a different server (operated by you or a third party)
- that supports equivalent copying facilities, provided you maintain
- clear directions next to the object code saying where to find the
- Corresponding Source. Regardless of what server hosts the
- Corresponding Source, you remain obligated to ensure that it is
- available for as long as needed to satisfy these requirements.
-
- e) Convey the object code using peer-to-peer transmission, provided
- you inform other peers where the object code and Corresponding
- Source of the work are being offered to the general public at no
- charge under subsection 6d.
-
- A separable portion of the object code, whose source code is excluded
-from the Corresponding Source as a System Library, need not be
-included in conveying the object code work.
-
- A "User Product" is either (1) a "consumer product", which means any
-tangible personal property which is normally used for personal, family,
-or household purposes, or (2) anything designed or sold for incorporation
-into a dwelling. In determining whether a product is a consumer product,
-doubtful cases shall be resolved in favor of coverage. For a particular
-product received by a particular user, "normally used" refers to a
-typical or common use of that class of product, regardless of the status
-of the particular user or of the way in which the particular user
-actually uses, or expects or is expected to use, the product. A product
-is a consumer product regardless of whether the product has substantial
-commercial, industrial or non-consumer uses, unless such uses represent
-the only significant mode of use of the product.
-
- "Installation Information" for a User Product means any methods,
-procedures, authorization keys, or other information required to install
-and execute modified versions of a covered work in that User Product from
-a modified version of its Corresponding Source. The information must
-suffice to ensure that the continued functioning of the modified object
-code is in no case prevented or interfered with solely because
-modification has been made.
-
- If you convey an object code work under this section in, or with, or
-specifically for use in, a User Product, and the conveying occurs as
-part of a transaction in which the right of possession and use of the
-User Product is transferred to the recipient in perpetuity or for a
-fixed term (regardless of how the transaction is characterized), the
-Corresponding Source conveyed under this section must be accompanied
-by the Installation Information. But this requirement does not apply
-if neither you nor any third party retains the ability to install
-modified object code on the User Product (for example, the work has
-been installed in ROM).
-
- The requirement to provide Installation Information does not include a
-requirement to continue to provide support service, warranty, or updates
-for a work that has been modified or installed by the recipient, or for
-the User Product in which it has been modified or installed. Access to a
-network may be denied when the modification itself materially and
-adversely affects the operation of the network or violates the rules and
-protocols for communication across the network.
-
- Corresponding Source conveyed, and Installation Information provided,
-in accord with this section must be in a format that is publicly
-documented (and with an implementation available to the public in
-source code form), and must require no special password or key for
-unpacking, reading or copying.
-
- 7. Additional Terms.
-
- "Additional permissions" are terms that supplement the terms of this
-License by making exceptions from one or more of its conditions.
-Additional permissions that are applicable to the entire Program shall
-be treated as though they were included in this License, to the extent
-that they are valid under applicable law. If additional permissions
-apply only to part of the Program, that part may be used separately
-under those permissions, but the entire Program remains governed by
-this License without regard to the additional permissions.
-
- When you convey a copy of a covered work, you may at your option
-remove any additional permissions from that copy, or from any part of
-it. (Additional permissions may be written to require their own
-removal in certain cases when you modify the work.) You may place
-additional permissions on material, added by you to a covered work,
-for which you have or can give appropriate copyright permission.
-
- Notwithstanding any other provision of this License, for material you
-add to a covered work, you may (if authorized by the copyright holders of
-that material) supplement the terms of this License with terms:
-
- a) Disclaiming warranty or limiting liability differently from the
- terms of sections 15 and 16 of this License; or
-
- b) Requiring preservation of specified reasonable legal notices or
- author attributions in that material or in the Appropriate Legal
- Notices displayed by works containing it; or
-
- c) Prohibiting misrepresentation of the origin of that material, or
- requiring that modified versions of such material be marked in
- reasonable ways as different from the original version; or
-
- d) Limiting the use for publicity purposes of names of licensors or
- authors of the material; or
-
- e) Declining to grant rights under trademark law for use of some
- trade names, trademarks, or service marks; or
-
- f) Requiring indemnification of licensors and authors of that
- material by anyone who conveys the material (or modified versions of
- it) with contractual assumptions of liability to the recipient, for
- any liability that these contractual assumptions directly impose on
- those licensors and authors.
-
- All other non-permissive additional terms are considered "further
-restrictions" within the meaning of section 10. If the Program as you
-received it, or any part of it, contains a notice stating that it is
-governed by this License along with a term that is a further
-restriction, you may remove that term. If a license document contains
-a further restriction but permits relicensing or conveying under this
-License, you may add to a covered work material governed by the terms
-of that license document, provided that the further restriction does
-not survive such relicensing or conveying.
-
- If you add terms to a covered work in accord with this section, you
-must place, in the relevant source files, a statement of the
-additional terms that apply to those files, or a notice indicating
-where to find the applicable terms.
-
- Additional terms, permissive or non-permissive, may be stated in the
-form of a separately written license, or stated as exceptions;
-the above requirements apply either way.
-
- 8. Termination.
-
- You may not propagate or modify a covered work except as expressly
-provided under this License. Any attempt otherwise to propagate or
-modify it is void, and will automatically terminate your rights under
-this License (including any patent licenses granted under the third
-paragraph of section 11).
-
- However, if you cease all violation of this License, then your
-license from a particular copyright holder is reinstated (a)
-provisionally, unless and until the copyright holder explicitly and
-finally terminates your license, and (b) permanently, if the copyright
-holder fails to notify you of the violation by some reasonable means
-prior to 60 days after the cessation.
-
- Moreover, your license from a particular copyright holder is
-reinstated permanently if the copyright holder notifies you of the
-violation by some reasonable means, this is the first time you have
-received notice of violation of this License (for any work) from that
-copyright holder, and you cure the violation prior to 30 days after
-your receipt of the notice.
-
- Termination of your rights under this section does not terminate the
-licenses of parties who have received copies or rights from you under
-this License. If your rights have been terminated and not permanently
-reinstated, you do not qualify to receive new licenses for the same
-material under section 10.
-
- 9. Acceptance Not Required for Having Copies.
-
- You are not required to accept this License in order to receive or
-run a copy of the Program. Ancillary propagation of a covered work
-occurring solely as a consequence of using peer-to-peer transmission
-to receive a copy likewise does not require acceptance. However,
-nothing other than this License grants you permission to propagate or
-modify any covered work. These actions infringe copyright if you do
-not accept this License. Therefore, by modifying or propagating a
-covered work, you indicate your acceptance of this License to do so.
-
- 10. Automatic Licensing of Downstream Recipients.
-
- Each time you convey a covered work, the recipient automatically
-receives a license from the original licensors, to run, modify and
-propagate that work, subject to this License. You are not responsible
-for enforcing compliance by third parties with this License.
-
- An "entity transaction" is a transaction transferring control of an
-organization, or substantially all assets of one, or subdividing an
-organization, or merging organizations. If propagation of a covered
-work results from an entity transaction, each party to that
-transaction who receives a copy of the work also receives whatever
-licenses to the work the party's predecessor in interest had or could
-give under the previous paragraph, plus a right to possession of the
-Corresponding Source of the work from the predecessor in interest, if
-the predecessor has it or can get it with reasonable efforts.
-
- You may not impose any further restrictions on the exercise of the
-rights granted or affirmed under this License. For example, you may
-not impose a license fee, royalty, or other charge for exercise of
-rights granted under this License, and you may not initiate litigation
-(including a cross-claim or counterclaim in a lawsuit) alleging that
-any patent claim is infringed by making, using, selling, offering for
-sale, or importing the Program or any portion of it.
-
- 11. Patents.
-
- A "contributor" is a copyright holder who authorizes use under this
-License of the Program or a work on which the Program is based. The
-work thus licensed is called the contributor's "contributor version".
-
- A contributor's "essential patent claims" are all patent claims
-owned or controlled by the contributor, whether already acquired or
-hereafter acquired, that would be infringed by some manner, permitted
-by this License, of making, using, or selling its contributor version,
-but do not include claims that would be infringed only as a
-consequence of further modification of the contributor version. For
-purposes of this definition, "control" includes the right to grant
-patent sublicenses in a manner consistent with the requirements of
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
this License.
- Each contributor grants you a non-exclusive, worldwide, royalty-free
-patent license under the contributor's essential patent claims, to
-make, use, sell, offer for sale, import and otherwise run, modify and
-propagate the contents of its contributor version.
-
- In the following three paragraphs, a "patent license" is any express
-agreement or commitment, however denominated, not to enforce a patent
-(such as an express permission to practice a patent or covenant not to
-sue for patent infringement). To "grant" such a patent license to a
-party means to make such an agreement or commitment not to enforce a
-patent against the party.
-
- If you convey a covered work, knowingly relying on a patent license,
-and the Corresponding Source of the work is not available for anyone
-to copy, free of charge and under the terms of this License, through a
-publicly available network server or other readily accessible means,
-then you must either (1) cause the Corresponding Source to be so
-available, or (2) arrange to deprive yourself of the benefit of the
-patent license for this particular work, or (3) arrange, in a manner
-consistent with the requirements of this License, to extend the patent
-license to downstream recipients. "Knowingly relying" means you have
-actual knowledge that, but for the patent license, your conveying the
-covered work in a country, or your recipient's use of the covered work
-in a country, would infringe one or more identifiable patents in that
-country that you have reason to believe are valid.
-
- If, pursuant to or in connection with a single transaction or
-arrangement, you convey, or propagate by procuring conveyance of, a
-covered work, and grant a patent license to some of the parties
-receiving the covered work authorizing them to use, propagate, modify
-or convey a specific copy of the covered work, then the patent license
-you grant is automatically extended to all recipients of the covered
-work and works based on it.
-
- A patent license is "discriminatory" if it does not include within
-the scope of its coverage, prohibits the exercise of, or is
-conditioned on the non-exercise of one or more of the rights that are
-specifically granted under this License. You may not convey a covered
-work if you are a party to an arrangement with a third party that is
-in the business of distributing software, under which you make payment
-to the third party based on the extent of your activity of conveying
-the work, and under which the third party grants, to any of the
-parties who would receive the covered work from you, a discriminatory
-patent license (a) in connection with copies of the covered work
-conveyed by you (or copies made from those copies), or (b) primarily
-for and in connection with specific products or compilations that
-contain the covered work, unless you entered into that arrangement,
-or that patent license was granted, prior to 28 March 2007.
-
- Nothing in this License shall be construed as excluding or limiting
-any implied license or other defenses to infringement that may
-otherwise be available to you under applicable patent law.
-
- 12. No Surrender of Others' Freedom.
-
- If conditions are imposed on you (whether by court order, agreement or
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License. If you cannot convey a
-covered work so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you may
-not convey it at all. For example, if you agree to terms that obligate you
-to collect a royalty for further conveying from those to whom you convey
-the Program, the only way you could satisfy both those terms and this
-License would be to refrain entirely from conveying the Program.
-
- 13. Use with the GNU Affero General Public License.
-
- Notwithstanding any other provision of this License, you have
-permission to link or combine any covered work with a work licensed
-under version 3 of the GNU Affero General Public License into a single
-combined work, and to convey the resulting work. The terms of this
-License will continue to apply to the part which is the covered work,
-but the special requirements of the GNU Affero General Public License,
-section 13, concerning interaction through a network will apply to the
-combination as such.
-
- 14. Revised Versions of this License.
-
- The Free Software Foundation may publish revised and/or new versions of
-the GNU General Public License from time to time. Such new versions will
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
- Each version is given a distinguishing version number. If the
-Program specifies that a certain numbered version of the GNU General
-Public License "or any later version" applies to it, you have the
-option of following the terms and conditions either of that numbered
-version or of any later version published by the Free Software
-Foundation. If the Program does not specify a version number of the
-GNU General Public License, you may choose any version ever published
-by the Free Software Foundation.
-
- If the Program specifies that a proxy can decide which future
-versions of the GNU General Public License can be used, that proxy's
-public statement of acceptance of a version permanently authorizes you
-to choose that version for the Program.
-
- Later license versions may give you additional or different
-permissions. However, no additional obligations are imposed on any
-author or copyright holder as a result of your choosing to follow a
-later version.
-
- 15. Disclaimer of Warranty.
-
- THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
-APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
-HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
-OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
-IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
-ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
-
- 16. Limitation of Liability.
-
- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
-THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
-GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
-USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
-DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
-PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
-EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGES.
-
- 17. Interpretation of Sections 15 and 16.
-
- If the disclaimer of warranty and limitation of liability provided
-above cannot be given local legal effect according to their terms,
-reviewing courts shall apply local law that most closely approximates
-an absolute waiver of all civil liability in connection with the
-Program, unless a warranty or assumption of liability accompanies a
-copy of the Program in return for a fee.
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
@@ -628,15 +287,15 @@ free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
-state the exclusion of warranty; and each file should have at least
+convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
- This program is free software: you can redistribute it and/or modify
+ This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
+ the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
@@ -644,31 +303,36 @@ the "copyright" line and a pointer to where the full notice is found.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see .
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, see .
Also add information on how to contact you by electronic and paper mail.
- If the program does terminal interaction, make it output a short
-notice like this when it starts in an interactive mode:
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
- Copyright (C)
- This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, your program's commands
-might be different; for a GUI interface, you would use an "about box".
-
- You should also get your employer (if you work as a programmer) or school,
-if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU GPL, see
-.
-
- The GNU General Public License does not permit incorporating your program
-into proprietary programs. If your program is a subroutine library, you
-may consider it more useful to permit linking proprietary applications with
-the library. If this is what you want to do, use the GNU Lesser General
-Public License instead of this License. But first, please read
-.
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ , 1 April 1989
+ Moe Ghoul, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/Makefile b/Makefile
index 68dc087f..aa462816 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ help:
@echo ""
@echo "Available targets:"
@echo " test - Run all tests"
- @echo " benchmark-create-perf - Compare par2rs create vs par2cmdline-turbo with perf counters"
+ @echo " benchmark-create-perf - Compare par2rs create vs par2cmdline-turbo with profiling support"
@echo " coverage - Generate HTML coverage report"
@echo " coverage-html - Generate HTML coverage report"
@echo " coverage-llvm - Generate text, HTML, LCOV, Cobertura, and Codecov JSON reports"
diff --git a/README.md b/README.md
index ad87aced..b9ab428b 100644
--- a/README.md
+++ b/README.md
@@ -193,28 +193,32 @@ cargo build --release --bins
### Create Performance Benchmarking
-For create-side comparisons on loaded machines, use the perf-counter benchmark
-rather than wall-clock timing:
+For create-side comparisons, use the benchmark harness:
```bash
nix develop --command make benchmark-create-perf
```
-The benchmark compares `par2rs` create against `par2cmdline-turbo` (`par2` by
-default), runs 30 measured iterations per case, reports mean/stddev/CV for
-Linux `perf` counters, and writes a `par2rs` create flamegraph. Results are
-saved under `target/perf-results/create/`.
+The benchmark compares `par2rs` create variants against `par2cmdline-turbo`
+(`par2` by default), runs measured iterations per case, verifies outputs across
+tools, reports wall time, records Linux `perf` counters when available, and
+writes a selected `par2rs` create flamegraph. On macOS, the flamegraph is
+generated with `xctrace`, `inferno-collapse-xctrace`, and `inferno-flamegraph`.
+Results are saved under
+`target/perf-results/create/`.
Useful overrides:
```bash
-nix develop --command env ITERATIONS=40 THREADS=16 \
- CASES='single_256m:1:256:1048576,multi_1g:64:16:1048576' \
+nix develop --command env ITERATIONS=10 THREADS=16 PROFILE_CASE=single_5g \
+ CASES='single_256m:1:256:1048576,multi_1g:64:16:1048576,single_5g:1:5120:1048576' \
scripts/benchmark_create_perf.sh
```
-The primary comparison is `par2rs/turbo` instructions. Wall-clock seconds are
-included only as context because they are noisy on a busy system.
+The pass/fail signal is mean wall time: `par2rs-xor-jit` must match or
+beat `turbo-auto` for every configured case. Linux `perf` counters are recorded
+to explain the wall-time result when the benchmark is run on Linux. Set
+`INCLUDE_PSHUFB=1` to add the `par2rs-pshufb` diagnostic baseline.
### Testing
@@ -418,9 +422,9 @@ This keeps the common case efficient while matching `par2cmdline` behavior for t
## License
-This project is licensed under the GNU General Public License v3.0 only - see the [LICENSE](LICENSE) file for details.
+This project is licensed under the GNU General Public License, version 2 or (at your option) any later version - see the [LICENSE](LICENSE) file for details.
-The project was relicensed to GPL-3.0-only so future PAR2 compatibility and performance work can incorporate GPL-compatible implementation details from established PAR2 tools when appropriate, with attribution and source availability preserved.
+The project uses GPL-2.0-or-later so future PAR2 compatibility and performance work can incorporate GPL-compatible implementation details from established PAR2 tools (notably the par2cmdline / par2cmdline-turbo / ParPar lineage, which is GPL-2.0-or-later) when appropriate, with attribution and source availability preserved.
## Acknowledgments
diff --git a/benches/common/par2verify_fixture.rs b/benches/common/par2verify_fixture.rs
new file mode 100644
index 00000000..d60a95ae
--- /dev/null
+++ b/benches/common/par2verify_fixture.rs
@@ -0,0 +1,286 @@
+#![allow(dead_code)]
+
+use par2rs::create::{CreateContextBuilder, SilentCreateReporter};
+use std::env;
+use std::ffi::OsString;
+use std::fs::{self, File};
+use std::io::{self, BufWriter, Write};
+use std::path::{Path, PathBuf};
+use std::sync::OnceLock;
+
+const DEFAULT_TURBO_ROOT: &str = "/home/mjc/projects/par2cmdline-turbo";
+const DATA_CHUNK_BYTES: usize = 1024 * 1024;
+
+#[derive(Debug, Clone)]
+pub struct VerifyFixture {
+ pub case_name: &'static str,
+ pub dir: PathBuf,
+ pub par2_file: PathBuf,
+ pub protected_bytes: u64,
+}
+
+#[derive(Debug, Clone)]
+pub struct Invocation {
+ pub program: PathBuf,
+ pub args: Vec,
+ pub current_dir: PathBuf,
+}
+
+#[derive(Debug, Clone)]
+struct TurboCommand {
+ program: PathBuf,
+ use_verify_subcommand: bool,
+}
+
+static CRITERION_FIXTURE: OnceLock = OnceLock::new();
+static IAI_FIXTURE: OnceLock = OnceLock::new();
+static PAR2RS_VERIFY: OnceLock = OnceLock::new();
+static TURBO_VERIFY: OnceLock = OnceLock::new();
+
+pub fn criterion_fixture() -> &'static VerifyFixture {
+ CRITERION_FIXTURE
+ .get_or_init(|| create_fixture("criterion-intact-64m", 64 * 1024 * 1024, 1024 * 1024))
+}
+
+pub fn iai_fixture() -> &'static VerifyFixture {
+ IAI_FIXTURE.get_or_init(|| create_fixture("iai-intact-8m", 8 * 1024 * 1024, 256 * 1024))
+}
+
+pub fn iai_fixture_arg() -> String {
+ iai_fixture().par2_file.to_string_lossy().into_owned()
+}
+
+pub fn par2rs_verify_invocation(fixture: &VerifyFixture) -> Invocation {
+ verify_invocation(resolve_par2rs_verify().clone(), false, &fixture.par2_file)
+}
+
+pub fn turbo_verify_invocation(fixture: &VerifyFixture) -> Invocation {
+ let turbo = resolve_turbo_verify().clone();
+ verify_invocation(
+ turbo.program,
+ turbo.use_verify_subcommand,
+ &fixture.par2_file,
+ )
+}
+
+pub fn par2rs_verify_invocation_for_path(par2_file: impl AsRef) -> Invocation {
+ verify_invocation(resolve_par2rs_verify().clone(), false, par2_file.as_ref())
+}
+
+pub fn turbo_verify_invocation_for_path(par2_file: impl AsRef) -> Invocation {
+ let turbo = resolve_turbo_verify().clone();
+ verify_invocation(
+ turbo.program,
+ turbo.use_verify_subcommand,
+ par2_file.as_ref(),
+ )
+}
+
+fn verify_invocation(
+ program: PathBuf,
+ use_verify_subcommand: bool,
+ par2_file: &Path,
+) -> Invocation {
+ let mut args = Vec::with_capacity(3);
+ if use_verify_subcommand {
+ args.push(OsString::from("verify"));
+ }
+ args.push(OsString::from("-q"));
+ args.push(par2_file.as_os_str().to_os_string());
+
+ let current_dir = par2_file
+ .parent()
+ .map(Path::to_path_buf)
+ .unwrap_or_else(|| manifest_dir());
+
+ Invocation {
+ program,
+ args,
+ current_dir,
+ }
+}
+
+fn create_fixture(
+ case_name: &'static str,
+ protected_bytes: usize,
+ block_size: u64,
+) -> VerifyFixture {
+ let dir = target_dir()
+ .join("bench-fixtures")
+ .join("par2verify")
+ .join(case_name);
+ let source_file = dir.join("payload.bin");
+ let par2_file = dir.join("payload.par2");
+
+ if !source_file.is_file() || !par2_file.is_file() {
+ if dir.exists() {
+ fs::remove_dir_all(&dir).expect("failed to clear stale benchmark fixture directory");
+ }
+ fs::create_dir_all(&dir).expect("failed to create benchmark fixture directory");
+ write_payload_file(&source_file, protected_bytes)
+ .expect("failed to create benchmark payload");
+
+ let reporter = Box::new(SilentCreateReporter);
+ let mut context = CreateContextBuilder::new()
+ .output_name(par2_file.to_string_lossy().into_owned())
+ .source_files(vec![source_file.clone()])
+ .block_size(block_size)
+ .redundancy_percentage(10)
+ .overwrite_existing(true)
+ .reporter(reporter)
+ .build()
+ .expect("failed to build benchmark create context");
+
+ context
+ .create()
+ .expect("failed to create benchmark PAR2 fixture");
+ }
+
+ VerifyFixture {
+ case_name,
+ dir,
+ par2_file,
+ protected_bytes: protected_bytes as u64,
+ }
+}
+
+fn write_payload_file(path: &Path, total_bytes: usize) -> io::Result<()> {
+ let file = File::create(path)?;
+ let mut writer = BufWriter::new(file);
+ let mut state = 0x1234_5678u32;
+ let mut chunk = vec![0u8; DATA_CHUNK_BYTES];
+ let mut remaining = total_bytes;
+
+ while remaining > 0 {
+ let take = remaining.min(chunk.len());
+ for byte in &mut chunk[..take] {
+ state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
+ *byte = (state >> 16) as u8;
+ }
+ writer.write_all(&chunk[..take])?;
+ remaining -= take;
+ }
+
+ writer.flush()
+}
+
+fn resolve_par2rs_verify() -> &'static PathBuf {
+ PAR2RS_VERIFY.get_or_init(|| {
+ if let Some(path) = option_env!("CARGO_BIN_EXE_par2verify") {
+ let path = PathBuf::from(path);
+ if path.is_file() {
+ return path;
+ }
+ }
+
+ for candidate in [
+ target_dir().join("release").join("par2verify"),
+ target_dir().join("debug").join("par2verify"),
+ ] {
+ if candidate.is_file() {
+ return candidate;
+ }
+ }
+
+ panic!(
+ "could not resolve par2rs par2verify binary; run cargo build --release --bin par2verify or cargo bench from a Cargo-built environment"
+ );
+ })
+}
+
+fn resolve_turbo_verify() -> &'static TurboCommand {
+ TURBO_VERIFY.get_or_init(|| {
+ let turbo_root = env::var_os("TURBO_ROOT")
+ .map(PathBuf::from)
+ .unwrap_or_else(|| PathBuf::from(DEFAULT_TURBO_ROOT));
+
+ if let Some(path) = env::var_os("TURBO_PAR2VERIFY")
+ .map(PathBuf::from)
+ .filter(|path| path.is_file())
+ {
+ return TurboCommand {
+ program: path,
+ use_verify_subcommand: false,
+ };
+ }
+
+ if let Some(path) = find_in_path("par2verify") {
+ return TurboCommand {
+ program: path,
+ use_verify_subcommand: false,
+ };
+ }
+
+ if let Some(path) = env::var_os("TURBO_PAR2")
+ .map(PathBuf::from)
+ .filter(|path| path.is_file())
+ {
+ let wrapper = path
+ .parent()
+ .map(|dir| dir.join("par2verify"))
+ .filter(|wrapper| wrapper.is_file());
+ if let Some(wrapper) = wrapper {
+ return TurboCommand {
+ program: wrapper,
+ use_verify_subcommand: false,
+ };
+ }
+ return TurboCommand {
+ program: path,
+ use_verify_subcommand: true,
+ };
+ }
+
+ let root_wrapper = turbo_root.join("par2verify");
+ if root_wrapper.is_file() {
+ return TurboCommand {
+ program: root_wrapper,
+ use_verify_subcommand: false,
+ };
+ }
+
+ let root_par2 = turbo_root.join("par2");
+ if root_par2.is_file() {
+ return TurboCommand {
+ program: root_par2,
+ use_verify_subcommand: true,
+ };
+ }
+
+ if let Some(path) = find_in_path("par2") {
+ return TurboCommand {
+ program: path,
+ use_verify_subcommand: true,
+ };
+ }
+
+ panic!(
+ "could not resolve par2cmdline-turbo verify command; run inside nix develop or set TURBO_PAR2VERIFY/TURBO_PAR2"
+ );
+ })
+}
+
+fn find_in_path(binary: &str) -> Option {
+ let paths = env::var_os("PATH")?;
+ env::split_paths(&paths)
+ .map(|dir| dir.join(binary))
+ .find(|candidate| candidate.is_file())
+}
+
+fn manifest_dir() -> PathBuf {
+ PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+}
+
+fn target_dir() -> PathBuf {
+ match env::var_os("CARGO_TARGET_DIR") {
+ Some(path) => {
+ let path = PathBuf::from(path);
+ if path.is_absolute() {
+ path
+ } else {
+ manifest_dir().join(path)
+ }
+ }
+ None => manifest_dir().join("target"),
+ }
+}
diff --git a/benches/compare_with_parpar.rs b/benches/compare_with_parpar.rs
new file mode 100644
index 00000000..c118b1ac
--- /dev/null
+++ b/benches/compare_with_parpar.rs
@@ -0,0 +1,182 @@
+//! Wall-clock comparison between par2rs and embedded ParPar hasher sources.
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use std::hint::black_box;
+
+fn make_data(len: usize) -> Vec {
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_scalar(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_scalar::Scalar;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_sse2(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_sse2::Sse2;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_bmi1(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_bmi1::Bmi1;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_avx512(data: &[u8]) -> Option<[u8; 16]> {
+ if !is_x86_feature_detected!("avx512f")
+ || !is_x86_feature_detected!("avx512vl")
+ || !is_x86_feature_detected!("avx512bw")
+ {
+ return None;
+ }
+
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_avx512::Avx512;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ Some(hasher.end())
+}
+
+fn par2rs_crc32_fast(data: &[u8]) -> u32 {
+ let mut hasher = crc32fast::Hasher::new();
+ hasher.update(data);
+ hasher.finalize()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn avx512_runtime_available() -> bool {
+ is_x86_feature_detected!("avx512f")
+ && is_x86_feature_detected!("avx512vl")
+ && is_x86_feature_detected!("avx512bw")
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+fn parpar_hasher_input(data: &[u8], method: par2rs::ffi::HasherInputMethod) -> [u8; 16] {
+ use par2rs::ffi::hasher_input::ParParHasherInput;
+
+ if !method.is_available() {
+ panic!("ParPar method {:?} is unavailable", method);
+ }
+ let mut hasher = ParParHasherInput::new(method).expect("ParPar hasher unavailable");
+ hasher.update(data);
+ *hasher.finalize().as_bytes()
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+fn parpar_crc32(data: &[u8]) -> u32 {
+ par2rs::ffi::crc32::crc32_compute(data)
+}
+
+#[cfg(target_arch = "x86_64")]
+fn bench_md5_variants(c: &mut Criterion) {
+ let sizes = [(16 * 1024, "16k"), (4 * 1024 * 1024, "4m")];
+
+ for (size, label) in &sizes {
+ let data = black_box(make_data(*size));
+ let mut group = c.benchmark_group(format!("md5_variants_{label}"));
+ group.throughput(Throughput::Bytes(*size as u64));
+
+ let rust_variants: &[(&str, fn(&[u8]) -> [u8; 16])] = &[
+ ("par2rs_scalar", par2rs_md5_scalar),
+ ("par2rs_sse2", par2rs_md5_sse2),
+ ("par2rs_bmi1", par2rs_md5_bmi1),
+ ];
+ for (name, func) in rust_variants {
+ group.bench_with_input(BenchmarkId::new(*name, label), &data, |b, data| {
+ b.iter(|| func(black_box(data)))
+ });
+ }
+ if avx512_runtime_available() && par2rs_md5_avx512(&data).is_some() {
+ group.bench_with_input(
+ BenchmarkId::new("par2rs_avx512", label),
+ &data,
+ |b, data| b.iter(|| par2rs_md5_avx512(black_box(data)).unwrap()),
+ );
+ }
+
+ #[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+ ))]
+ {
+ let parpar_variants: &[(&str, par2rs::ffi::HasherInputMethod)] = &[
+ ("parpar_scalar", par2rs::ffi::HasherInputMethod::Scalar),
+ ("parpar_simd", par2rs::ffi::HasherInputMethod::Simd),
+ ("parpar_crc", par2rs::ffi::HasherInputMethod::Crc),
+ ("parpar_simd_crc", par2rs::ffi::HasherInputMethod::SimdCrc),
+ ("parpar_bmi1", par2rs::ffi::HasherInputMethod::Bmi1),
+ ("parpar_avx512", par2rs::ffi::HasherInputMethod::Avx512),
+ ];
+
+ for (name, method) in parpar_variants {
+ if method.is_available()
+ && (method != &par2rs::ffi::HasherInputMethod::Avx512
+ || avx512_runtime_available())
+ {
+ group.bench_with_input(BenchmarkId::new(*name, label), &data, |b, data| {
+ b.iter(|| parpar_hasher_input(black_box(data), *method))
+ });
+ }
+ }
+ }
+
+ group.finish();
+ }
+}
+
+fn bench_crc32_variants(c: &mut Criterion) {
+ let data = black_box(make_data(4 * 1024 * 1024));
+ let mut group = c.benchmark_group("crc32_4m");
+ group.throughput(Throughput::Bytes(data.len() as u64));
+
+ group.bench_function("par2rs_crc32fast", |b| {
+ b.iter(|| par2rs_crc32_fast(black_box(&data)))
+ });
+
+ #[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+ ))]
+ group.bench_function("parpar_crc32", |b| {
+ b.iter(|| parpar_crc32(black_box(&data)))
+ });
+
+ group.finish();
+}
+
+#[cfg(target_arch = "x86_64")]
+criterion_group!(benches, bench_md5_variants, bench_crc32_variants);
+
+#[cfg(not(target_arch = "x86_64"))]
+criterion_group!(benches, bench_crc32_variants);
+
+criterion_main!(benches);
diff --git a/benches/crc_compare.rs b/benches/crc_compare.rs
new file mode 100644
index 00000000..c3538bcd
--- /dev/null
+++ b/benches/crc_compare.rs
@@ -0,0 +1,96 @@
+//! Bench: `crc32fast` vs `crc-fast` over realistic par2 block sizes.
+//!
+//! Decision driver for whether to swap the CRC backend. We care about:
+//! - 64 B: the fused HasherInput inner-loop granularity.
+//! - 16 KiB: the Tier-1 sub-slice size used by `update_md5_crc32_fused`.
+//! - 4 MiB & 64 MiB: full-buffer one-shots representative of large par2 blocks.
+//!
+//! Both crates produce IsoHdlc/zlib CRC-32. Each bench also asserts equality
+//! on the first iteration so we don't ship a result that doesn't match.
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use std::hint::black_box;
+
+fn make_data(len: usize) -> Vec {
+ // Deterministic non-zero pattern so optimizers can't constant-fold.
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+fn crc32fast_one_shot(data: &[u8]) -> u32 {
+ let mut h = crc32fast::Hasher::new();
+ h.update(data);
+ h.finalize()
+}
+
+fn crc_fast_one_shot(data: &[u8]) -> u32 {
+ use crc_fast::{CrcAlgorithm, Digest};
+ let mut d = Digest::new(CrcAlgorithm::Crc32IsoHdlc);
+ d.update(data);
+ d.finalize() as u32
+}
+
+fn bench_oneshot(c: &mut Criterion) {
+ let sizes = [64usize, 16 * 1024, 4 * 1024 * 1024, 64 * 1024 * 1024];
+ let mut group = c.benchmark_group("crc_oneshot");
+
+ for &len in &sizes {
+ let data = make_data(len);
+
+ // Sanity: both backends must agree.
+ assert_eq!(
+ crc32fast_one_shot(&data),
+ crc_fast_one_shot(&data),
+ "crc32fast and crc-fast disagree at len={len}"
+ );
+
+ group.throughput(Throughput::Bytes(len as u64));
+
+ group.bench_with_input(BenchmarkId::new("crc32fast", len), &data, |b, d| {
+ b.iter(|| black_box(crc32fast_one_shot(black_box(d))));
+ });
+ group.bench_with_input(BenchmarkId::new("crc-fast", len), &data, |b, d| {
+ b.iter(|| black_box(crc_fast_one_shot(black_box(d))));
+ });
+ }
+
+ group.finish();
+}
+
+/// Streaming bench: feed 64 B at a time. This is the actual fused-HasherInput
+/// access pattern — what matters for the T2.c port decision.
+fn bench_streaming_64b(c: &mut Criterion) {
+ let total_sizes = [16 * 1024usize, 4 * 1024 * 1024];
+ let mut group = c.benchmark_group("crc_stream_64B");
+
+ for &total in &total_sizes {
+ let data = make_data(total);
+ group.throughput(Throughput::Bytes(total as u64));
+
+ group.bench_with_input(BenchmarkId::new("crc32fast", total), &data, |b, d| {
+ b.iter(|| {
+ let mut h = crc32fast::Hasher::new();
+ for chunk in d.chunks_exact(64) {
+ h.update(black_box(chunk));
+ }
+ black_box(h.finalize())
+ });
+ });
+ group.bench_with_input(BenchmarkId::new("crc-fast", total), &data, |b, d| {
+ b.iter(|| {
+ use crc_fast::{CrcAlgorithm, Digest};
+ let mut h = Digest::new(CrcAlgorithm::Crc32IsoHdlc);
+ for chunk in d.chunks_exact(64) {
+ h.update(black_box(chunk));
+ }
+ black_box(h.finalize() as u32)
+ });
+ });
+ }
+
+ group.finish();
+}
+
+criterion_group!(benches, bench_oneshot, bench_streaming_64b);
+criterion_main!(benches);
diff --git a/benches/create_benchmark.rs b/benches/create_benchmark.rs
index 0b4aaa9b..26a97919 100644
--- a/benches/create_benchmark.rs
+++ b/benches/create_benchmark.rs
@@ -71,7 +71,9 @@ fn bench_par2_creation_redundancy(c: &mut Criterion) {
for redundancy in redundancy_levels {
group.bench_with_input(
- BenchmarkId::new("redundancy", format!("{}%", redundancy)),
+ // Avoid `%` in the display label; Criterion forwards benchmark IDs into
+ // plot generation, and gnuplot treats `%` as a format character.
+ BenchmarkId::new("redundancy", format!("{}pct", redundancy)),
&redundancy,
|b, &redundancy| {
b.iter_batched(
diff --git a/benches/fused_hashing.rs b/benches/fused_hashing.rs
new file mode 100644
index 00000000..38f7d693
--- /dev/null
+++ b/benches/fused_hashing.rs
@@ -0,0 +1,123 @@
+//! Microbenchmark for the Tier 1 fused-hashing helpers in `src/checksum.rs`.
+//!
+//! Compares four strategies over the same input buffer:
+//!
+//! * `naive_2pass` — `Md5::update(whole)` then `Crc32::update(whole)`.
+//! This is what `par2cmdline-turbo` would call the "double scan" baseline:
+//! for any buffer larger than L1d, the second pass eats cold cache lines.
+//!
+//! * `fused_2hash` — `compute_md5_crc32_simultaneous`, which walks the
+//! buffer in 16 KiB sub-slices feeding both hashers back-to-back so the
+//! CRC reads what MD5 just brought into L1.
+//!
+//! * `naive_3pass` — file-MD5 + block-MD5 + CRC32 each as full-buffer
+//! passes. Mirrors the pre-Tier-1 create() loop where the chunk was
+//! scanned three times.
+//!
+//! * `fused_3hash` — `update_file_md5_block_md5_crc32_fused`, the
+//! three-hasher variant that backs `encode_and_hash_files` post-Tier-1.
+//!
+//! The interesting buffer sizes are the ones that span L1 / L2 / L3 / DRAM:
+//! 16 KiB sits exactly in L1, 256 KiB falls out of L1, 4 MiB falls out of L2,
+//! 64 MiB falls out of L3 on most desktops. The fused helpers should shine
+//! once the buffer is bigger than L1.
+//!
+//! For the cache-miss number itself, run under `perf stat`:
+//!
+//! ```text
+//! perf stat -e cache-misses,cache-references,L1-dcache-load-misses,\
+//! LLC-load-misses,instructions,cycles,task-clock \
+//! cargo bench --bench fused_hashing -- --profile-time 5 fused_2hash/4MiB
+//! ```
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use std::hint::black_box;
+
+use crc32fast::Hasher as Crc32Hasher;
+use md5::{Digest, Md5};
+
+use par2rs::checksum::{compute_md5_crc32_simultaneous, update_file_md5_block_md5_crc32_fused};
+
+/// Buffer sizes, chosen to span L1 / L2 / L3 / DRAM on a typical desktop.
+const SIZES: &[(&str, usize)] = &[
+ ("16KiB", 16 * 1024), // fits in L1d (~32-48 KiB)
+ ("256KiB", 256 * 1024), // fits in L2 (~256 KiB-1 MiB)
+ ("4MiB", 4 * 1024 * 1024), // fits in L3
+ ("64MiB", 64 * 1024 * 1024), // exceeds typical L3
+];
+
+fn make_buffer(size: usize) -> Vec {
+ // Repeating low-entropy pattern is fine: we're measuring memory traffic +
+ // hash compress throughput, not anything data-dependent.
+ (0..size).map(|i| (i & 0xff) as u8).collect()
+}
+
+fn bench_two_hashes(c: &mut Criterion) {
+ let mut group = c.benchmark_group("two_hashes_md5_crc32");
+
+ for &(label, size) in SIZES {
+ let data = make_buffer(size);
+ group.throughput(Throughput::Bytes(size as u64));
+
+ // Pre-Tier-1 baseline: two full-buffer scans.
+ group.bench_with_input(BenchmarkId::new("naive_2pass", label), &data, |b, data| {
+ b.iter(|| {
+ let mut md5 = Md5::new();
+ let mut crc = Crc32Hasher::new();
+ md5.update(black_box(data));
+ crc.update(black_box(data));
+ black_box((md5.finalize(), crc.finalize()))
+ });
+ });
+
+ // Tier 1: cache-resident sub-slice walker.
+ group.bench_with_input(BenchmarkId::new("fused_2hash", label), &data, |b, data| {
+ b.iter(|| black_box(compute_md5_crc32_simultaneous(black_box(data))));
+ });
+ }
+
+ group.finish();
+}
+
+fn bench_three_hashes(c: &mut Criterion) {
+ let mut group = c.benchmark_group("three_hashes_file_block_crc");
+
+ for &(label, size) in SIZES {
+ let data = make_buffer(size);
+ group.throughput(Throughput::Bytes(size as u64));
+
+ // Pre-Tier-1: three full-buffer scans (the original create() loop).
+ group.bench_with_input(BenchmarkId::new("naive_3pass", label), &data, |b, data| {
+ b.iter(|| {
+ let mut file_md5 = Md5::new();
+ let mut block_md5 = Md5::new();
+ let mut crc = Crc32Hasher::new();
+ file_md5.update(black_box(data));
+ block_md5.update(black_box(data));
+ crc.update(black_box(data));
+ black_box((file_md5.finalize(), block_md5.finalize(), crc.finalize()))
+ });
+ });
+
+ // Tier 1: cache-resident three-hasher walker.
+ group.bench_with_input(BenchmarkId::new("fused_3hash", label), &data, |b, data| {
+ b.iter(|| {
+ let mut file_md5 = Md5::new();
+ let mut block_md5 = Md5::new();
+ let mut crc = Crc32Hasher::new();
+ update_file_md5_block_md5_crc32_fused(
+ &mut file_md5,
+ &mut block_md5,
+ &mut crc,
+ black_box(data),
+ );
+ black_box((file_md5.finalize(), block_md5.finalize(), crc.finalize()))
+ });
+ });
+ }
+
+ group.finish();
+}
+
+criterion_group!(benches, bench_two_hashes, bench_three_hashes);
+criterion_main!(benches);
diff --git a/benches/iai_hasher_input.rs b/benches/iai_hasher_input.rs
new file mode 100644
index 00000000..36946e37
--- /dev/null
+++ b/benches/iai_hasher_input.rs
@@ -0,0 +1,148 @@
+//! iai-callgrind instruction-count benchmark for the ParPar HasherInput
+//! port (T2.c). Complement to `parpar_hasher_input.rs` (criterion
+//! wall-clock): this one measures deterministic instruction / branch /
+//! cache counts so regressions show up without timing noise.
+//!
+//! Compares the same three implementations:
+//! 1. naive 3-pass (md-5 + crc32fast)
+//! 2. tier-1 helper (`update_file_md5_block_md5_crc32_fused`)
+//! 3. HasherInput (the new ParPar fused 64 B driver)
+//!
+//! at two single-block sizes (16 KiB sub-slice, 4 MiB block).
+
+use iai_callgrind::{library_benchmark, library_benchmark_group, main};
+use md5::Digest as _;
+use md5::Md5;
+use par2rs::checksum::update_file_md5_block_md5_crc32_fused;
+#[cfg(target_arch = "x86_64")]
+use par2rs::parpar_hasher::hasher_input::HasherInput;
+#[cfg(target_arch = "x86_64")]
+use par2rs::parpar_hasher::md5x2::Md5x2;
+#[cfg(target_arch = "x86_64")]
+use par2rs::parpar_hasher::md5x2_scalar::Scalar;
+#[cfg(target_arch = "x86_64")]
+use par2rs::parpar_hasher::md5x2_sse2::Sse2;
+use std::hint::black_box;
+
+type Out = ([u8; 16], [u8; 16], u32);
+
+fn make_data(len: usize) -> Vec {
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+// Naive 3-pass: file-MD5 + block-MD5 + block-CRC, three independent walks.
+fn naive_seq(data: &[u8]) -> Out {
+ let mut f = Md5::new();
+ f.update(data);
+ let mut b = Md5::new();
+ b.update(data);
+ let mut c = crc32fast::Hasher::new();
+ c.update(data);
+ (f.finalize().into(), b.finalize().into(), c.finalize())
+}
+
+// Tier-1 helper: one walk, three single-lane hashers.
+fn tier1_helper(data: &[u8]) -> Out {
+ let mut f = Md5::new();
+ let mut b = Md5::new();
+ let mut c = crc32fast::Hasher::new();
+ update_file_md5_block_md5_crc32_fused(&mut f, &mut b, &mut c, data);
+ (f.finalize().into(), b.finalize().into(), c.finalize())
+}
+
+// HasherInput: MD5x2 + CLMul CRC32 fused at 64 B granularity.
+// Single-block scenario: one update + one get_block(0) + one end().
+#[cfg(target_arch = "x86_64")]
+fn hasher_input(data: &[u8]) -> Out {
+ let mut h: HasherInput = HasherInput::new();
+ h.update(data);
+ let bh = h.get_block(0);
+ let file = h.end();
+ (file, bh.md5, bh.crc32)
+}
+
+// ----------------------- 16 KiB single block -----------------------
+
+#[library_benchmark]
+#[bench::sixteen_kib(make_data(16 * 1024))]
+fn bench_naive_seq_16k(data: Vec) -> Out {
+ black_box(naive_seq(black_box(&data)))
+}
+
+#[library_benchmark]
+#[bench::sixteen_kib(make_data(16 * 1024))]
+fn bench_tier1_helper_16k(data: Vec) -> Out {
+ black_box(tier1_helper(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::sixteen_kib(make_data(16 * 1024))]
+fn bench_hasher_input_scalar_16k(data: Vec) -> Out {
+ black_box(hasher_input::(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::sixteen_kib(make_data(16 * 1024))]
+fn bench_hasher_input_sse2_16k(data: Vec) -> Out {
+ black_box(hasher_input::(black_box(&data)))
+}
+
+// ----------------------- 4 MiB single block ------------------------
+
+#[library_benchmark]
+#[bench::four_mib(make_data(4 * 1024 * 1024))]
+fn bench_naive_seq_4m(data: Vec) -> Out {
+ black_box(naive_seq(black_box(&data)))
+}
+
+#[library_benchmark]
+#[bench::four_mib(make_data(4 * 1024 * 1024))]
+fn bench_tier1_helper_4m(data: Vec) -> Out {
+ black_box(tier1_helper(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::four_mib(make_data(4 * 1024 * 1024))]
+fn bench_hasher_input_scalar_4m(data: Vec) -> Out {
+ black_box(hasher_input::(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::four_mib(make_data(4 * 1024 * 1024))]
+fn bench_hasher_input_sse2_4m(data: Vec) -> Out {
+ black_box(hasher_input::(black_box(&data)))
+}
+
+// ----------------------- groups + main -----------------------------
+
+#[cfg(target_arch = "x86_64")]
+library_benchmark_group!(
+ name = hasher_input_group;
+ benchmarks =
+ bench_naive_seq_16k,
+ bench_tier1_helper_16k,
+ bench_hasher_input_scalar_16k,
+ bench_hasher_input_sse2_16k,
+ bench_naive_seq_4m,
+ bench_tier1_helper_4m,
+ bench_hasher_input_scalar_4m,
+ bench_hasher_input_sse2_4m
+);
+
+#[cfg(not(target_arch = "x86_64"))]
+library_benchmark_group!(
+ name = hasher_input_group;
+ benchmarks =
+ bench_naive_seq_16k,
+ bench_tier1_helper_16k,
+ bench_naive_seq_4m,
+ bench_tier1_helper_4m
+);
+
+main!(library_benchmark_groups = hasher_input_group);
diff --git a/benches/iai_par2verify_compare.rs b/benches/iai_par2verify_compare.rs
new file mode 100644
index 00000000..3789e3fe
--- /dev/null
+++ b/benches/iai_par2verify_compare.rs
@@ -0,0 +1,36 @@
+use iai_callgrind::{binary_benchmark, binary_benchmark_group, main, Command};
+
+#[path = "common/par2verify_fixture.rs"]
+mod par2verify_fixture;
+
+fn into_iai_command(invocation: par2verify_fixture::Invocation) -> Command {
+ let mut command = Command::new(invocation.program);
+ command.current_dir(invocation.current_dir);
+ for arg in invocation.args {
+ command.arg(arg);
+ }
+ command.build()
+}
+
+#[binary_benchmark]
+#[bench::intact_8m(par2verify_fixture::iai_fixture_arg())]
+fn bench_par2rs_verify(par2_file: String) -> Command {
+ into_iai_command(par2verify_fixture::par2rs_verify_invocation_for_path(
+ par2_file,
+ ))
+}
+
+#[binary_benchmark]
+#[bench::intact_8m(par2verify_fixture::iai_fixture_arg())]
+fn bench_turbo_verify(par2_file: String) -> Command {
+ into_iai_command(par2verify_fixture::turbo_verify_invocation_for_path(
+ par2_file,
+ ))
+}
+
+binary_benchmark_group!(
+ name = par2verify_group;
+ benchmarks = bench_par2rs_verify, bench_turbo_verify
+);
+
+main!(binary_benchmark_groups = par2verify_group);
diff --git a/benches/iai_parpar_comparison.rs b/benches/iai_parpar_comparison.rs
new file mode 100644
index 00000000..02aa3f03
--- /dev/null
+++ b/benches/iai_parpar_comparison.rs
@@ -0,0 +1,212 @@
+//! Instruction-count comparison for par2rs vs embedded ParPar hasher sources.
+
+use iai_callgrind::{library_benchmark, library_benchmark_group, main};
+use std::hint::black_box;
+
+fn make_data(len: usize) -> Vec {
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_scalar(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_scalar::Scalar;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_sse2(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_sse2::Sse2;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+#[cfg(target_arch = "x86_64")]
+fn par2rs_md5_bmi1(data: &[u8]) -> [u8; 16] {
+ use par2rs::parpar_hasher::hasher_input::HasherInput;
+ use par2rs::parpar_hasher::md5x2_bmi1::Bmi1;
+
+ let mut hasher = HasherInput::::new();
+ hasher.update(data);
+ hasher.end()
+}
+
+fn par2rs_crc32(data: &[u8]) -> u32 {
+ let mut hasher = crc32fast::Hasher::new();
+ hasher.update(data);
+ hasher.finalize()
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+fn parpar_hasher_input(data: &[u8], method: par2rs::ffi::HasherInputMethod) -> [u8; 16] {
+ use par2rs::ffi::hasher_input::ParParHasherInput;
+
+ let mut hasher = ParParHasherInput::new(method).expect("ParPar hasher unavailable");
+ hasher.update(data);
+ *hasher.finalize().as_bytes()
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+fn parpar_crc32(data: &[u8]) -> u32 {
+ par2rs::ffi::crc32::crc32_compute(data)
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::par2rs_scalar_16k(make_data(16 * 1024))]
+fn bench_par2rs_scalar_16k(data: Vec) -> [u8; 16] {
+ black_box(par2rs_md5_scalar(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::par2rs_sse2_16k(make_data(16 * 1024))]
+fn bench_par2rs_sse2_16k(data: Vec) -> [u8; 16] {
+ black_box(par2rs_md5_sse2(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::par2rs_bmi1_16k(make_data(16 * 1024))]
+fn bench_par2rs_bmi1_16k(data: Vec) -> [u8; 16] {
+ black_box(par2rs_md5_bmi1(black_box(&data)))
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+#[library_benchmark]
+#[bench::parpar_scalar_16k(make_data(16 * 1024))]
+fn bench_parpar_scalar_16k(data: Vec) -> [u8; 16] {
+ black_box(parpar_hasher_input(
+ black_box(&data),
+ par2rs::ffi::HasherInputMethod::Scalar,
+ ))
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+#[library_benchmark]
+#[bench::parpar_sse_16k(make_data(16 * 1024))]
+fn bench_parpar_sse_16k(data: Vec) -> [u8; 16] {
+ black_box(parpar_hasher_input(
+ black_box(&data),
+ par2rs::ffi::HasherInputMethod::Simd,
+ ))
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+#[library_benchmark]
+#[bench::parpar_bmi1_16k(make_data(16 * 1024))]
+fn bench_parpar_bmi1_16k(data: Vec) -> [u8; 16] {
+ black_box(parpar_hasher_input(
+ black_box(&data),
+ par2rs::ffi::HasherInputMethod::Bmi1,
+ ))
+}
+
+#[library_benchmark]
+#[bench::crc32_1m(make_data(1024 * 1024))]
+fn bench_crc32_1m(data: Vec) -> u32 {
+ black_box(par2rs_crc32(black_box(&data)))
+}
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+#[library_benchmark]
+#[bench::parpar_crc32_1m(make_data(1024 * 1024))]
+fn bench_parpar_crc32_1m(data: Vec) -> u32 {
+ black_box(parpar_crc32(black_box(&data)))
+}
+
+#[cfg(target_arch = "x86_64")]
+library_benchmark_group!(
+ name = md5_group;
+ benchmarks =
+ bench_par2rs_scalar_16k,
+ bench_par2rs_sse2_16k,
+ bench_par2rs_bmi1_16k
+);
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+library_benchmark_group!(
+ name = md5_parpar_group;
+ benchmarks =
+ bench_parpar_scalar_16k,
+ bench_parpar_sse_16k,
+ bench_parpar_bmi1_16k
+);
+
+library_benchmark_group!(
+ name = crc32_group;
+ benchmarks =
+ bench_crc32_1m
+);
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+library_benchmark_group!(
+ name = crc32_parpar_group;
+ benchmarks =
+ bench_parpar_crc32_1m
+);
+
+#[cfg(all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ parpar_compare_embedded
+))]
+main!(
+ library_benchmark_groups = md5_group,
+ md5_parpar_group,
+ crc32_group,
+ crc32_parpar_group
+);
+
+#[cfg(any(
+ all(not(feature = "parpar-compare"), target_arch = "x86_64"),
+ all(
+ feature = "parpar-compare",
+ target_arch = "x86_64",
+ not(parpar_compare_embedded)
+ )
+))]
+main!(library_benchmark_groups = md5_group, crc32_group);
+
+#[cfg(not(target_arch = "x86_64"))]
+main!(library_benchmark_groups = crc32_group);
diff --git a/benches/iai_simd.rs b/benches/iai_simd.rs
index a148bdf0..eec732a7 100644
--- a/benches/iai_simd.rs
+++ b/benches/iai_simd.rs
@@ -1,11 +1,50 @@
use iai_callgrind::{library_benchmark, library_benchmark_group, main};
+use par2rs::reed_solomon::alloc_aligned_vec;
use par2rs::reed_solomon::codec::build_split_mul_table;
use par2rs::reed_solomon::galois::Galois16;
use par2rs::reed_solomon::simd::process_slice_multiply_add_portable_simd;
#[cfg(target_arch = "x86_64")]
+use par2rs::reed_solomon::simd::{
+ prepare_xor_jit_bitplane_chunks, XorJitBitplaneKernel, XorJitPreparedCoeff,
+};
+#[cfg(target_arch = "x86_64")]
use par2rs::reed_solomon::simd::{process_slice_multiply_add_simd, SimdLevel};
use std::hint::black_box;
+#[cfg(target_arch = "x86_64")]
+const BITPLANE_BLOCK_BYTES: usize = 512;
+
+#[cfg(target_arch = "x86_64")]
+struct BitplaneFixture {
+ inputs: Vec>,
+ output: Vec,
+ kernels: Vec,
+}
+
+#[cfg(target_arch = "x86_64")]
+fn bitplane_fixture(batch_len: usize, segment_len: usize) -> BitplaneFixture {
+ let mut inputs = Vec::with_capacity(batch_len);
+ let mut kernels = Vec::with_capacity(batch_len);
+
+ for input_idx in 0..batch_len {
+ let source = (0..segment_len)
+ .map(|byte_idx| (input_idx * 31 + byte_idx * 17 + byte_idx / 7) as u8)
+ .collect::>();
+ let mut prepared = alloc_aligned_vec(segment_len.next_multiple_of(BITPLANE_BLOCK_BYTES));
+ prepare_xor_jit_bitplane_chunks(&mut prepared, &source);
+ inputs.push(prepared);
+
+ let coeff = XorJitPreparedCoeff::new((0x100b + input_idx * 37) as u16);
+ kernels.push(XorJitBitplaneKernel::new(&coeff).expect("bitplane kernel"));
+ }
+
+ BitplaneFixture {
+ inputs,
+ output: alloc_aligned_vec(segment_len.next_multiple_of(BITPLANE_BLOCK_BYTES)),
+ kernels,
+ }
+}
+
#[library_benchmark]
#[bench::small(vec![0xAAu8; 528])]
#[bench::medium(vec![0xAAu8; 4096])]
@@ -69,9 +108,48 @@ fn bench_scalar_baseline(input: Vec) -> Vec {
output
}
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::batch12_128k(bitplane_fixture(12, 128 * 1024))]
+fn bench_xor_jit_bitplane_batch_first(mut fixture: BitplaneFixture) -> Vec {
+ for (input, kernel) in fixture.inputs.iter().zip(fixture.kernels.iter()) {
+ kernel.multiply_add_chunks(black_box(input), black_box(&mut fixture.output));
+ }
+
+ fixture.output
+}
+
+#[cfg(target_arch = "x86_64")]
+#[library_benchmark]
+#[bench::batch12_128k(bitplane_fixture(12, 128 * 1024))]
+fn bench_xor_jit_bitplane_block_first(mut fixture: BitplaneFixture) -> Vec {
+ for block_offset in (0..fixture.output.len()).step_by(BITPLANE_BLOCK_BYTES) {
+ let output_block = &mut fixture.output[block_offset..block_offset + BITPLANE_BLOCK_BYTES];
+ for (input, kernel) in fixture.inputs.iter().zip(fixture.kernels.iter()) {
+ let input_block = &input[block_offset..block_offset + BITPLANE_BLOCK_BYTES];
+ kernel.multiply_add_block(black_box(input_block), black_box(output_block));
+ }
+ }
+
+ fixture.output
+}
+
library_benchmark_group!(
name = simd_group;
benchmarks = bench_pshufb_simd, bench_portable_simd, bench_scalar_baseline
);
+#[cfg(target_arch = "x86_64")]
+library_benchmark_group!(
+ name = xor_jit_bitplane_group;
+ benchmarks = bench_xor_jit_bitplane_batch_first, bench_xor_jit_bitplane_block_first
+);
+
+#[cfg(target_arch = "x86_64")]
+main!(
+ library_benchmark_groups = simd_group,
+ xor_jit_bitplane_group
+);
+
+#[cfg(not(target_arch = "x86_64"))]
main!(library_benchmark_groups = simd_group);
diff --git a/benches/md5x2_crc_fused.rs b/benches/md5x2_crc_fused.rs
new file mode 100644
index 00000000..0804cbe0
--- /dev/null
+++ b/benches/md5x2_crc_fused.rs
@@ -0,0 +1,269 @@
+//! End-to-end fused-hashing comparison for the par2 create path.
+//!
+//! Each variant computes the three outputs the create path needs:
+//! - file-MD5 (rolls across all blocks of a file)
+//! - block-MD5 (per source block)
+//! - block-CRC32 (per source block)
+//!
+//! 5 variants × 2 sizes (16 KiB sub-slice and 4 MiB block).
+//!
+//! All variants are sanity-checked at startup against the naive
+//! reference: any divergence aborts the run.
+//!
+//! Variants:
+//! 1. naive_seq — three independent passes (md-5 + md-5 + crc32fast).
+//! 2. tier1_subslice — current shipped helper:
+//! `update_file_md5_block_md5_crc32_fused`
+//! interleaves all three at 16 KiB sub-slice
+//! granularity (single-lane MD5s + crc32fast).
+//! 3. md5x2_crc32fast_64b — MD5x2 absorbs block+file MD5; crc32fast
+//! streamed in 64 B chunks.
+//! 4. md5x2_crcfast_64b — same, but `crc_fast::Digest` for CRC.
+//! 5. md5x2_only — lower bound: MD5x2 with no CRC.
+//! 6. md5x2_64b_then_crc_bulk — MD5x2 at 64 B granularity (block+file MD5
+//! in one walk) followed by ONE bulk
+//! `crc32fast::Hasher::update(data)` call.
+//! Tests whether crc32fast's per-call overhead
+//! (SIMD setup, alignment prologue) is what's
+//! costing variants 3/4 the gap to variant 5.
+//! 7. md5x2_clmul_64b_fused — MD5x2 + ParPar PCLMULQDQ CRC32 fused at
+//! 64 B granularity. The candidate: one
+//! cache-line read, three states updated,
+//! no per-call SIMD setup overhead.
+//!
+//! The MD5x2 lanes are fed the same 64 B for harness simplicity; this is
+//! what the real `HasherInput` will do when the file is 64 B-aligned and
+//! the staggered position offset is 0. (Stagger overhead is a separate
+//! concern for T2.c, not the backend choice here.)
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use md5::Digest as _;
+use md5::Md5;
+use par2rs::checksum::update_file_md5_block_md5_crc32_fused;
+use par2rs::parpar_hasher::{crc_clmul, md5x2_scalar};
+use std::hint::black_box;
+
+type Out = ([u8; 16], [u8; 16], u32); // (file_md5, block_md5, crc32)
+
+fn make_data(len: usize) -> Vec {
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+// ---------- variants ----------
+
+fn v1_naive_seq(data: &[u8]) -> Out {
+ let mut f = Md5::new();
+ f.update(data);
+ let mut b = Md5::new();
+ b.update(data);
+ let mut c = crc32fast::Hasher::new();
+ c.update(data);
+ (f.finalize().into(), b.finalize().into(), c.finalize())
+}
+
+fn v2_tier1_subslice(data: &[u8]) -> Out {
+ let mut f = Md5::new();
+ let mut b = Md5::new();
+ let mut c = crc32fast::Hasher::new();
+ update_file_md5_block_md5_crc32_fused(&mut f, &mut b, &mut c, data);
+ (f.finalize().into(), b.finalize().into(), c.finalize())
+}
+
+/// MD5x2 produces *two* MD5 results from one walk. We use lane 1 for
+/// file-MD5 and lane 2 for block-MD5; this bench feeds the same 64 B
+/// to both lanes so the two lanes' final digests are identical, but
+/// the cost profile (two MD5s + CRC in a single pass) matches what
+/// HasherInput will pay.
+fn md5x2_finalise(mut state: [u32; 8], total_len: usize) -> ([u8; 16], [u8; 16]) {
+ // Bench harness only feeds chunks_exact(64), so the partial buffer is
+ // empty at finalise time and one tail block suffices: 0x80, then zero
+ // pad to 56 mod 64, then 8-byte LE bit-length.
+ let bit_len = (total_len as u64).wrapping_mul(8);
+ let mut tail = [0u8; 64];
+ tail[0] = 0x80;
+ tail[56..64].copy_from_slice(&bit_len.to_le_bytes());
+ // Both lanes finalise in one MD5x2 step: same tail in both lanes,
+ // independent state per lane.
+ unsafe {
+ md5x2_scalar::process_block_x2_scalar(&mut state, tail.as_ptr(), tail.as_ptr());
+ }
+ let mut out1 = [0u8; 16];
+ let mut out2 = [0u8; 16];
+ for i in 0..4 {
+ out1[i * 4..i * 4 + 4].copy_from_slice(&state[i].to_le_bytes());
+ out2[i * 4..i * 4 + 4].copy_from_slice(&state[i + 4].to_le_bytes());
+ }
+ (out1, out2)
+}
+
+fn v3_md5x2_crc32fast(data: &[u8]) -> Out {
+ let mut state = md5x2_scalar::init_state();
+ let mut crc = crc32fast::Hasher::new();
+ for chunk in data.chunks_exact(64) {
+ unsafe {
+ md5x2_scalar::process_block_x2_scalar(&mut state, chunk.as_ptr(), chunk.as_ptr());
+ }
+ crc.update(chunk);
+ }
+ let (lane1, lane2) = md5x2_finalise(state, data.len());
+ (lane1, lane2, crc.finalize())
+}
+
+fn v4_md5x2_crcfast(data: &[u8]) -> Out {
+ use crc_fast::{CrcAlgorithm, Digest};
+ let mut state = md5x2_scalar::init_state();
+ let mut crc = Digest::new(CrcAlgorithm::Crc32IsoHdlc);
+ for chunk in data.chunks_exact(64) {
+ unsafe {
+ md5x2_scalar::process_block_x2_scalar(&mut state, chunk.as_ptr(), chunk.as_ptr());
+ }
+ crc.update(chunk);
+ }
+ let (lane1, lane2) = md5x2_finalise(state, data.len());
+ (lane1, lane2, crc.finalize() as u32)
+}
+
+fn v5_md5x2_only(data: &[u8]) -> ([u8; 16], [u8; 16]) {
+ let mut state = md5x2_scalar::init_state();
+ for chunk in data.chunks_exact(64) {
+ unsafe {
+ md5x2_scalar::process_block_x2_scalar(&mut state, chunk.as_ptr(), chunk.as_ptr());
+ }
+ }
+ md5x2_finalise(state, data.len())
+}
+
+/// MD5x2 fused at 64 B (block+file MD5 in one walk), but CRC32 is called
+/// ONCE on the whole buffer afterwards so crc32fast hits its big-buffer
+/// fast path instead of paying per-call overhead 65 536× per 4 MiB block.
+///
+/// The buffer is L2-resident from the MD5x2 walk, so the second pass is
+/// a hot read. Tests whether that's cheaper than 64 B-granularity CRC.
+fn v6_md5x2_then_crc_bulk(data: &[u8]) -> Out {
+ let mut state = md5x2_scalar::init_state();
+ for chunk in data.chunks_exact(64) {
+ unsafe {
+ md5x2_scalar::process_block_x2_scalar(&mut state, chunk.as_ptr(), chunk.as_ptr());
+ }
+ }
+ let (lane1, lane2) = md5x2_finalise(state, data.len());
+ let mut crc = crc32fast::Hasher::new();
+ crc.update(data);
+ (lane1, lane2, crc.finalize())
+}
+
+/// MD5x2 + ParPar PCLMULQDQ CRC32 fused at 64 B granularity. Both states
+/// advance from the same cache-line read; no per-call SIMD setup cost.
+///
+/// Bench harness only feeds 16 KiB / 4 MiB inputs (both 64-aligned), so
+/// the CRC `finish` path's tail handling never triggers here — the loop
+/// is the steady-state cost the create path will see for full blocks.
+fn v7_md5x2_clmul_64b_fused(data: &[u8]) -> Out {
+ let mut md5_state = md5x2_scalar::init_state();
+ let mut crc_state = crc_clmul::State::new();
+ unsafe {
+ crc_clmul::init(&mut crc_state);
+ let mut p = data.as_ptr();
+ let mut remaining = data.len();
+ while remaining >= 64 {
+ md5x2_scalar::process_block_x2_scalar(&mut md5_state, p, p);
+ crc_clmul::process_block(&mut crc_state, p);
+ p = p.add(64);
+ remaining -= 64;
+ }
+ let crc = crc_clmul::finish(&mut crc_state, p, remaining);
+ let (lane1, lane2) = md5x2_finalise(md5_state, data.len());
+ (lane1, lane2, crc)
+ }
+}
+
+// ---------- bench ----------
+
+fn bench(c: &mut Criterion) {
+ let sizes = [16 * 1024usize, 4 * 1024 * 1024];
+ let mut group = c.benchmark_group("create_path_hashing");
+
+ // Sanity-check correctness at every size before benching.
+ for &len in &sizes {
+ assert!(
+ len % 64 == 0,
+ "bench sizes must be 64-aligned for MD5x2 harness"
+ );
+ let d = make_data(len);
+ let r1 = v1_naive_seq(&d);
+ let r2 = v2_tier1_subslice(&d);
+ let r3 = v3_md5x2_crc32fast(&d);
+ let r4 = v4_md5x2_crcfast(&d);
+ let r5 = v5_md5x2_only(&d);
+ let r6 = v6_md5x2_then_crc_bulk(&d);
+ let r7 = v7_md5x2_clmul_64b_fused(&d);
+
+ // CRC: variants 1, 2, 3, 6, 7 must match (all CRC32 IEEE);
+ // variant 4 same value via crc-fast.
+ assert_eq!(r1.2, r2.2);
+ assert_eq!(r1.2, r3.2);
+ assert_eq!(r1.2, r4.2, "crc-fast disagrees with crc32fast at len={len}");
+ assert_eq!(r1.2, r6.2, "v6 bulk crc disagrees at len={len}");
+ assert_eq!(r1.2, r7.2, "v7 clmul crc disagrees at len={len}");
+
+ // Naive single-lane MD5 == MD5x2 lane (since both lanes get the
+ // same input, MD5x2 lane1 == MD5x2 lane2 == single-lane MD5).
+ assert_eq!(
+ r1.0, r3.0,
+ "MD5x2 lane1 != single-lane MD5 at len={len} (v3)"
+ );
+ assert_eq!(
+ r1.0, r3.1,
+ "MD5x2 lane2 != single-lane MD5 at len={len} (v3)"
+ );
+ assert_eq!(r3.0, r4.0);
+ assert_eq!(r3.0, r5.0);
+ assert_eq!(r3.1, r5.1);
+ assert_eq!(r3.0, r6.0);
+ assert_eq!(r3.1, r6.1);
+ assert_eq!(r3.0, r7.0);
+ assert_eq!(r3.1, r7.1);
+ }
+
+ for &len in &sizes {
+ let data = make_data(len);
+ group.throughput(Throughput::Bytes(len as u64));
+
+ group.bench_with_input(BenchmarkId::new("1_naive_seq", len), &data, |b, d| {
+ b.iter(|| black_box(v1_naive_seq(black_box(d))));
+ });
+ group.bench_with_input(BenchmarkId::new("2_tier1_subslice", len), &data, |b, d| {
+ b.iter(|| black_box(v2_tier1_subslice(black_box(d))));
+ });
+ group.bench_with_input(BenchmarkId::new("3_md5x2+crc32fast", len), &data, |b, d| {
+ b.iter(|| black_box(v3_md5x2_crc32fast(black_box(d))))
+ });
+ group.bench_with_input(BenchmarkId::new("4_md5x2+crc-fast", len), &data, |b, d| {
+ b.iter(|| black_box(v4_md5x2_crcfast(black_box(d))));
+ });
+ group.bench_with_input(BenchmarkId::new("5_md5x2_only", len), &data, |b, d| {
+ b.iter(|| black_box(v5_md5x2_only(black_box(d))));
+ });
+ group.bench_with_input(
+ BenchmarkId::new("6_md5x2_then_crc_bulk", len),
+ &data,
+ |b, d| {
+ b.iter(|| black_box(v6_md5x2_then_crc_bulk(black_box(d))));
+ },
+ );
+ group.bench_with_input(
+ BenchmarkId::new("7_md5x2_clmul_64b_fused", len),
+ &data,
+ |b, d| {
+ b.iter(|| black_box(v7_md5x2_clmul_64b_fused(black_box(d))));
+ },
+ );
+ }
+
+ group.finish();
+}
+
+criterion_group!(benches, bench);
+criterion_main!(benches);
diff --git a/benches/par2verify_compare.rs b/benches/par2verify_compare.rs
new file mode 100644
index 00000000..074afd34
--- /dev/null
+++ b/benches/par2verify_compare.rs
@@ -0,0 +1,51 @@
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use std::process::Command;
+use std::time::Duration;
+
+#[path = "common/par2verify_fixture.rs"]
+mod par2verify_fixture;
+
+fn run_invocation(invocation: &par2verify_fixture::Invocation) {
+ let output = Command::new(&invocation.program)
+ .current_dir(&invocation.current_dir)
+ .args(&invocation.args)
+ .output()
+ .unwrap_or_else(|err| {
+ panic!(
+ "failed to run {}: {err}",
+ invocation.program.to_string_lossy()
+ )
+ });
+
+ assert!(
+ output.status.success(),
+ "verify command failed: program={} status={:?} stderr={}",
+ invocation.program.display(),
+ output.status.code(),
+ String::from_utf8_lossy(&output.stderr)
+ );
+}
+
+fn bench_par2verify_compare(c: &mut Criterion) {
+ let fixture = par2verify_fixture::criterion_fixture();
+ let mut group = c.benchmark_group("par2verify_compare");
+ group.sample_size(10);
+ group.warm_up_time(Duration::from_secs(2));
+ group.measurement_time(Duration::from_secs(20));
+ group.throughput(Throughput::Bytes(fixture.protected_bytes));
+
+ let par2rs = par2verify_fixture::par2rs_verify_invocation(fixture);
+ group.bench_function(BenchmarkId::new("par2rs", fixture.case_name), |b| {
+ b.iter(|| run_invocation(&par2rs))
+ });
+
+ let turbo = par2verify_fixture::turbo_verify_invocation(fixture);
+ group.bench_function(BenchmarkId::new("turbo", fixture.case_name), |b| {
+ b.iter(|| run_invocation(&turbo))
+ });
+
+ group.finish();
+}
+
+criterion_group!(benches, bench_par2verify_compare);
+criterion_main!(benches);
diff --git a/benches/parpar_hasher_input.rs b/benches/parpar_hasher_input.rs
new file mode 100644
index 00000000..de372620
--- /dev/null
+++ b/benches/parpar_hasher_input.rs
@@ -0,0 +1,285 @@
+//! Benchmark for the ported ParPar `HasherInput` fused driver (T2.c).
+//!
+//! Compares three implementations of the create-path workload —
+//! producing per-file MD5 + per-block MD5 + per-block CRC32 over a
+//! sequence of source bytes:
+//!
+//! 1. `naive_seq` — three independent passes (file MD5, block MD5,
+//! block CRC32) using `md-5` + `crc32fast`. The
+//! upper bound on naive throughput.
+//! 2. `tier1_helper` — the currently-shipped Tier-1 sub-slice helper
+//! `update_file_md5_block_md5_crc32_fused` driving
+//! three single-lane hashers from one walk.
+//! 3. `hasher_input` — the new ParPar HasherInput port: MD5x2
+//! (block-MD5 + file-MD5 in one walk via ILP) +
+//! PCLMULQDQ CRC32, fused at 64 B granularity.
+//!
+//! Two scenarios:
+//!
+//! * `single_block_` — exactly one PAR2 block, no zero-pad.
+//! Driver is reset per iteration. Sizes: 16 KiB, 4 MiB.
+//! * `multi_block_x` — N blocks of `block_size` bytes
+//! plus a `block_size/2` short tail. Exercises the steady-state
+//! loop AND the staggered-offset get_block path. Default: 4×4 MiB.
+//!
+//! Correctness: every variant is sanity-checked once at startup
+//! against the naive reference; any mismatch panics before timing.
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use md5::Digest as _;
+use md5::Md5;
+use par2rs::checksum::update_file_md5_block_md5_crc32_fused;
+use par2rs::parpar_hasher::hasher_input::HasherInput;
+use par2rs::parpar_hasher::md5x2::Md5x2;
+use par2rs::parpar_hasher::md5x2_avx512::Avx512;
+use par2rs::parpar_hasher::md5x2_bmi1::Bmi1;
+use par2rs::parpar_hasher::md5x2_scalar::Scalar;
+use par2rs::parpar_hasher::md5x2_sse2::Sse2;
+use std::hint::black_box;
+
+/// (file_md5, Vec<(block_md5, block_crc32)>)
+type MultiBlockOut = ([u8; 16], Vec<([u8; 16], u32)>);
+
+fn make_data(len: usize) -> Vec {
+ (0..len)
+ .map(|i| (i as u8).wrapping_mul(31).wrapping_add(7))
+ .collect()
+}
+
+// ----------------------------- Variants -----------------------------
+
+fn v1_naive_seq_blocks(data: &[u8], block_size: usize) -> MultiBlockOut {
+ let mut file = Md5::new();
+ file.update(data);
+ let file_md5: [u8; 16] = file.finalize().into();
+
+ let mut blocks = Vec::new();
+ let mut off = 0;
+ while off < data.len() {
+ let end = (off + block_size).min(data.len());
+ let real = &data[off..end];
+ let pad = block_size - real.len();
+
+ let mut bm = Md5::new();
+ bm.update(real);
+ if pad > 0 {
+ let zeros = vec![0u8; pad];
+ bm.update(&zeros);
+ }
+ let bmd5: [u8; 16] = bm.finalize().into();
+
+ let mut bc = crc32fast::Hasher::new();
+ bc.update(real);
+ if pad > 0 {
+ let zeros = vec![0u8; pad];
+ bc.update(&zeros);
+ }
+ blocks.push((bmd5, bc.finalize()));
+ off = end;
+ }
+ (file_md5, blocks)
+}
+
+fn v2_tier1_helper_blocks(data: &[u8], block_size: usize) -> MultiBlockOut {
+ let mut file = Md5::new();
+ let mut blocks = Vec::new();
+ let mut off = 0;
+ while off < data.len() {
+ let end = (off + block_size).min(data.len());
+ let real = &data[off..end];
+ let pad = block_size - real.len();
+
+ let mut bm = Md5::new();
+ let mut bc = crc32fast::Hasher::new();
+ update_file_md5_block_md5_crc32_fused(&mut file, &mut bm, &mut bc, real);
+ if pad > 0 {
+ // Pad block-only (file MD5 already absorbed real bytes only,
+ // matching naive: file is over data, NOT over zero-padded blocks).
+ let zeros = vec![0u8; pad];
+ bm.update(&zeros);
+ bc.update(&zeros);
+ }
+ let bmd5: [u8; 16] = bm.finalize().into();
+ blocks.push((bmd5, bc.finalize()));
+ off = end;
+ }
+ let file_md5: [u8; 16] = file.finalize().into();
+ (file_md5, blocks)
+}
+
+fn hasher_input_blocks(data: &[u8], block_size: usize) -> MultiBlockOut {
+ let mut h: HasherInput = HasherInput::new();
+ let mut blocks = Vec::new();
+ let mut off = 0;
+ let mut written_in_block = 0usize;
+
+ while off < data.len() {
+ let block_remaining = block_size - written_in_block;
+ let take = block_remaining.min(data.len() - off);
+ h.update(&data[off..off + take]);
+ off += take;
+ written_in_block += take;
+ if written_in_block == block_size {
+ let bh = h.get_block(0);
+ blocks.push((bh.md5, bh.crc32));
+ written_in_block = 0;
+ }
+ }
+ if written_in_block > 0 {
+ let pad = (block_size - written_in_block) as u64;
+ let bh = h.get_block(pad);
+ blocks.push((bh.md5, bh.crc32));
+ }
+ let file_md5 = h.end();
+ (file_md5, blocks)
+}
+
+fn v3_hasher_input_scalar(data: &[u8], block_size: usize) -> MultiBlockOut {
+ hasher_input_blocks::(data, block_size)
+}
+
+fn v4_hasher_input_sse2(data: &[u8], block_size: usize) -> MultiBlockOut {
+ hasher_input_blocks::(data, block_size)
+}
+
+fn v5_hasher_input_bmi1(data: &[u8], block_size: usize) -> MultiBlockOut {
+ hasher_input_blocks::(data, block_size)
+}
+
+fn v6_hasher_input_avx512(data: &[u8], block_size: usize) -> MultiBlockOut {
+ hasher_input_blocks::(data, block_size)
+}
+
+fn avx512_supported() -> bool {
+ std::is_x86_feature_detected!("avx512f")
+ && std::is_x86_feature_detected!("avx512vl")
+ && std::is_x86_feature_detected!("pclmulqdq")
+ && std::is_x86_feature_detected!("sse4.1")
+}
+
+// ----------------------------- Bench -----------------------------
+
+fn bench(c: &mut Criterion) {
+ // Scenario A: a single full block. Highlights steady-state cost
+ // without the get_block / cross-block plumbing.
+ let single_sizes = [16 * 1024usize, 4 * 1024 * 1024];
+
+ // Scenario B: a multi-block "file": four full blocks + one short
+ // tail (half a block) that exercises zero-pad + carry between
+ // get_block calls.
+ let multi_block_size = 4 * 1024 * 1024;
+ let multi_blocks = 4usize;
+ let multi_total = multi_block_size * multi_blocks + multi_block_size / 2;
+
+ // ---- Correctness sanity check (panics on divergence) ----
+ let bmi1_ok = std::is_x86_feature_detected!("bmi1");
+ let avx512_ok = avx512_supported();
+ for &len in &single_sizes {
+ let data = make_data(len);
+ let r1 = v1_naive_seq_blocks(&data, len);
+ let r2 = v2_tier1_helper_blocks(&data, len);
+ let r3 = v3_hasher_input_scalar(&data, len);
+ let r4 = v4_hasher_input_sse2(&data, len);
+ assert_eq!(r1, r2, "tier1 helper diverges from naive at len={len}");
+ assert_eq!(
+ r1, r3,
+ "HasherInput diverges from naive at len={len}"
+ );
+ assert_eq!(r1, r4, "HasherInput diverges from naive at len={len}");
+ if bmi1_ok {
+ let r5 = v5_hasher_input_bmi1(&data, len);
+ assert_eq!(r1, r5, "HasherInput diverges from naive at len={len}");
+ }
+ if avx512_ok {
+ let r6 = v6_hasher_input_avx512(&data, len);
+ assert_eq!(
+ r1, r6,
+ "HasherInput diverges from naive at len={len}"
+ );
+ }
+ }
+ {
+ let data = make_data(multi_total);
+ let r1 = v1_naive_seq_blocks(&data, multi_block_size);
+ let r2 = v2_tier1_helper_blocks(&data, multi_block_size);
+ let r3 = v3_hasher_input_scalar(&data, multi_block_size);
+ let r4 = v4_hasher_input_sse2(&data, multi_block_size);
+ assert_eq!(r1, r2, "tier1 helper multi-block diverges");
+ assert_eq!(r1, r3, "HasherInput multi-block diverges");
+ assert_eq!(r1, r4, "HasherInput multi-block diverges");
+ if bmi1_ok {
+ let r5 = v5_hasher_input_bmi1(&data, multi_block_size);
+ assert_eq!(r1, r5, "HasherInput multi-block diverges");
+ }
+ if avx512_ok {
+ let r6 = v6_hasher_input_avx512(&data, multi_block_size);
+ assert_eq!(r1, r6, "HasherInput multi-block diverges");
+ }
+ }
+
+ // ---- Single-block group ----
+ let mut g = c.benchmark_group("hasher_input_single_block");
+ for &len in &single_sizes {
+ let data = make_data(len);
+ g.throughput(Throughput::Bytes(len as u64));
+ g.bench_with_input(BenchmarkId::new("1_naive_seq", len), &data, |b, d| {
+ b.iter(|| black_box(v1_naive_seq_blocks(black_box(d), len)))
+ });
+ g.bench_with_input(BenchmarkId::new("2_tier1_helper", len), &data, |b, d| {
+ b.iter(|| black_box(v2_tier1_helper_blocks(black_box(d), len)))
+ });
+ g.bench_with_input(BenchmarkId::new("3_hasher_scalar", len), &data, |b, d| {
+ b.iter(|| black_box(v3_hasher_input_scalar(black_box(d), len)))
+ });
+ g.bench_with_input(BenchmarkId::new("4_hasher_sse2", len), &data, |b, d| {
+ b.iter(|| black_box(v4_hasher_input_sse2(black_box(d), len)))
+ });
+ if bmi1_ok {
+ g.bench_with_input(BenchmarkId::new("5_hasher_bmi1", len), &data, |b, d| {
+ b.iter(|| black_box(v5_hasher_input_bmi1(black_box(d), len)))
+ });
+ }
+ if avx512_ok {
+ g.bench_with_input(BenchmarkId::new("6_hasher_avx512", len), &data, |b, d| {
+ b.iter(|| black_box(v6_hasher_input_avx512(black_box(d), len)))
+ });
+ }
+ }
+ g.finish();
+
+ // ---- Multi-block group ----
+ let mut g = c.benchmark_group("hasher_input_multi_block");
+ let data = make_data(multi_total);
+ let label = format!("{multi_blocks}x{multi_block_size}+half");
+ g.throughput(Throughput::Bytes(multi_total as u64));
+ g.bench_with_input(BenchmarkId::new("1_naive_seq", &label), &data, |b, d| {
+ b.iter(|| black_box(v1_naive_seq_blocks(black_box(d), multi_block_size)))
+ });
+ g.bench_with_input(BenchmarkId::new("2_tier1_helper", &label), &data, |b, d| {
+ b.iter(|| black_box(v2_tier1_helper_blocks(black_box(d), multi_block_size)))
+ });
+ g.bench_with_input(
+ BenchmarkId::new("3_hasher_scalar", &label),
+ &data,
+ |b, d| b.iter(|| black_box(v3_hasher_input_scalar(black_box(d), multi_block_size))),
+ );
+ g.bench_with_input(BenchmarkId::new("4_hasher_sse2", &label), &data, |b, d| {
+ b.iter(|| black_box(v4_hasher_input_sse2(black_box(d), multi_block_size)))
+ });
+ if bmi1_ok {
+ g.bench_with_input(BenchmarkId::new("5_hasher_bmi1", &label), &data, |b, d| {
+ b.iter(|| black_box(v5_hasher_input_bmi1(black_box(d), multi_block_size)))
+ });
+ }
+ if avx512_ok {
+ g.bench_with_input(
+ BenchmarkId::new("6_hasher_avx512", &label),
+ &data,
+ |b, d| b.iter(|| black_box(v6_hasher_input_avx512(black_box(d), multi_block_size))),
+ );
+ }
+ g.finish();
+}
+
+criterion_group!(benches, bench);
+criterion_main!(benches);
diff --git a/build.rs b/build.rs
new file mode 100644
index 00000000..2cb2da25
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,118 @@
+use std::env;
+use std::path::PathBuf;
+
+fn main() {
+ println!("cargo:rustc-check-cfg=cfg(parpar_compare_embedded)");
+
+ if env::var_os("CARGO_FEATURE_PARPAR_COMPARE").is_none() {
+ return;
+ }
+
+ let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
+ let parpar = manifest_dir.join("par2cmdline-turbo/parpar");
+ let hasher = parpar.join("hasher");
+ let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
+ let md5_final = hasher.join("md5-final.c");
+
+ if target_arch != "x86_64" {
+ println!(
+ "cargo:warning=parpar-compare requested for unsupported target arch `{target_arch}`; skipping embedded ParPar build"
+ );
+ return;
+ }
+
+ if !md5_final.exists() {
+ println!(
+ "cargo:warning=parpar-compare requested but vendored ParPar sources are unavailable at {}; skipping embedded ParPar build",
+ parpar.display()
+ );
+ return;
+ }
+
+ println!("cargo:rustc-cfg=parpar_compare_embedded");
+
+ for path in [
+ "src/ffi/wrapper.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_input.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_md5crc.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_scalar.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_sse.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_bmi1.cpp",
+ "par2cmdline-turbo/parpar/hasher/hasher_clmul.cpp",
+ "par2cmdline-turbo/parpar/hasher/tables.cpp",
+ "par2cmdline-turbo/parpar/hasher/crc_zeropad.c",
+ "par2cmdline-turbo/parpar/hasher/hasher_input_impl.h",
+ "par2cmdline-turbo/parpar/hasher/hasher_input.h",
+ "par2cmdline-turbo/parpar/hasher/hasher_md5crc.h",
+ "par2cmdline-turbo/parpar/hasher/hasher_md5crc_impl.h",
+ "par2cmdline-turbo/parpar/hasher/hasher_input_base.h",
+ "par2cmdline-turbo/parpar/hasher/md5x2-base.h",
+ "par2cmdline-turbo/parpar/hasher/md5x2-scalar.h",
+ "par2cmdline-turbo/parpar/hasher/md5x2-sse.h",
+ "par2cmdline-turbo/parpar/hasher/crc_slice4.h",
+ "par2cmdline-turbo/parpar/hasher/crc_clmul.h",
+ "par2cmdline-turbo/parpar/hasher/crc_zeropad.h",
+ "par2cmdline-turbo/parpar/src/platform.h",
+ "par2cmdline-turbo/parpar/src/hedley.h",
+ ] {
+ println!("cargo:rerun-if-changed={path}");
+ }
+
+ let mut c_build = cc::Build::new();
+ c_build.include(&manifest_dir);
+ c_build.include(&parpar);
+ c_build.include(&hasher);
+ c_build.file(&md5_final);
+ c_build.file(hasher.join("crc_zeropad.c"));
+ c_build.compile("parpar_helpers");
+
+ let mut cpp_build = cc::Build::new();
+ cpp_build.cpp(true);
+ cpp_build.std("c++17");
+ cpp_build.define("PARPAR_ENABLE_HASHER_MD5CRC", None);
+ cpp_build.include(&manifest_dir);
+ cpp_build.include(&parpar);
+ cpp_build.include(&hasher);
+
+ if target_arch == "x86_64" {
+ cpp_build.flag_if_supported("-msse2");
+ cpp_build.flag_if_supported("-mssse3");
+ cpp_build.flag_if_supported("-msse4.1");
+ cpp_build.flag_if_supported("-mpclmul");
+ cpp_build.flag_if_supported("-mbmi");
+ cpp_build.flag_if_supported("-mbmi2");
+ cpp_build.flag_if_supported("-mavx");
+ }
+
+ cpp_build
+ .file(manifest_dir.join("src/ffi/wrapper.cpp"))
+ .file(hasher.join("hasher_input.cpp"))
+ .file(hasher.join("hasher_md5crc.cpp"))
+ .file(hasher.join("hasher_scalar.cpp"))
+ .file(hasher.join("hasher_sse.cpp"))
+ .file(hasher.join("hasher_bmi1.cpp"))
+ .file(hasher.join("hasher_clmul.cpp"))
+ .file(hasher.join("tables.cpp"))
+ .compile("parpar_wrapper");
+
+ let mut avx512_build = cc::Build::new();
+ avx512_build.cpp(true);
+ avx512_build.std("c++17");
+ avx512_build.include(&manifest_dir);
+ avx512_build.include(&parpar);
+ avx512_build.include(&hasher);
+ avx512_build.flag_if_supported("-msse2");
+ avx512_build.flag_if_supported("-mssse3");
+ avx512_build.flag_if_supported("-msse4.1");
+ avx512_build.flag_if_supported("-mpclmul");
+ avx512_build.flag_if_supported("-mbmi");
+ avx512_build.flag_if_supported("-mavx512f");
+ avx512_build.flag_if_supported("-mavx512vl");
+ avx512_build.flag_if_supported("-mavx512bw");
+ avx512_build.flag_if_supported("-mavx512dq");
+ avx512_build.flag_if_supported("-mbmi2");
+ avx512_build
+ .file(hasher.join("hasher_avx512.cpp"))
+ .file(hasher.join("hasher_avx512vl.cpp"))
+ .compile("parpar_avx512");
+}
diff --git a/flake.nix b/flake.nix
index 52aee1cd..7bbb1635 100644
--- a/flake.nix
+++ b/flake.nix
@@ -102,11 +102,11 @@
inferno
bc # for benchmark averaging calculations
gnuplot # for criterion benchmark graphs
- valgrind # for iai-callgrind benchmarks
gh # GitHub CLI
git-filter-repo # for rewriting git history
]
++ pkgs.lib.optionals pkgs.stdenv.isLinux [
+ valgrind # for iai-callgrind benchmarks
heaptrack
perf
];
diff --git a/scripts/benchmark_create_perf.sh b/scripts/benchmark_create_perf.sh
index 594803d7..9368a611 100755
--- a/scripts/benchmark_create_perf.sh
+++ b/scripts/benchmark_create_perf.sh
@@ -1,30 +1,49 @@
#!/usr/bin/env bash
-# Compare par2rs create against par2cmdline-turbo using instruction counts.
+# Compare par2rs create against par2cmdline-turbo.
#
-# This harness is designed for loaded machines: wall clock is captured only as
-# context, while Linux perf hardware counters are the primary comparison signal.
+# Wall time is the pass/fail signal for the JIT-vs-turbo requirement. Linux perf
+# hardware counters are captured as diagnostics to explain timing differences.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
+PLATFORM="$(uname -s)"
+ARCH="$(uname -m)"
ITERATIONS="${ITERATIONS:-30}"
WARMUP_RUNS="${WARMUP_RUNS:-3}"
REDUNDANCY="${REDUNDANCY:-10}"
-RECOVERY_FILES="${RECOVERY_FILES:-1}"
+RECOVERY_FILES="${RECOVERY_FILES:-8}"
+FIRST_RECOVERY_BLOCK="${FIRST_RECOVERY_BLOCK:-}"
THREADS="${THREADS:-1}"
PAR2CMD_BIN="${PAR2CMD_BIN:-par2}"
PAR2RS_BIN="${PAR2RS_BIN:-$PROJECT_ROOT/target/release/par2}"
RESULTS_ROOT="${RESULTS_ROOT:-$PROJECT_ROOT/target/perf-results/create}"
KEEP_WORK="${KEEP_WORK:-0}"
RUN_FLAMEGRAPH="${RUN_FLAMEGRAPH:-1}"
+PROFILE_CASE="${PROFILE_CASE:-}"
+PROFILE_TOOL="${PROFILE_TOOL:-}"
PROFILE_FREQUENCY="${PROFILE_FREQUENCY:-997}"
PERF_EVENTS="${PERF_EVENTS:-instructions,cycles,branches,branch-misses,cache-references,cache-misses,task-clock,context-switches,cpu-migrations,page-faults}"
+CACHE_PROFILE="${CACHE_PROFILE:-0}"
+CACHE_PROFILE_TOOL="${CACHE_PROFILE_TOOL:-}"
+CACHE_PROFILE_JIT_DUMPS="${CACHE_PROFILE_JIT_DUMPS:-1}"
+CACHE_LOAD_LATENCY="${CACHE_LOAD_LATENCY:-30}"
+CACHE_STAT_EVENTS="${CACHE_STAT_EVENTS:-instructions,cycles,L1-dcache-loads,L1-dcache-load-misses,LLC-loads,LLC-load-misses,dTLB-loads,dTLB-load-misses,cache-references,cache-misses}"
VERIFY_OUTPUTS="${VERIFY_OUTPUTS:-1}"
+VERIFY_REPAIR="${VERIFY_REPAIR:-smoke}"
+INCLUDE_PSHUFB="${INCLUDE_PSHUFB:-0}"
+DEFAULT_BENCHMARK_TOOLS=(
+ par2rs-xor-jit
+ turbo-auto
+)
+if [[ "$INCLUDE_PSHUFB" == "1" ]]; then
+ DEFAULT_BENCHMARK_TOOLS=(par2rs-pshufb "${DEFAULT_BENCHMARK_TOOLS[@]}")
+fi
# label:file_count:file_size_mib:block_size_bytes
-DEFAULT_CASES="single_16m:1:16:262144,single_128m:1:128:1048576,multi_128m:32:4:262144"
+DEFAULT_CASES="single_256m:1:256:1048576,multi_1g:64:16:1048576,single_5g:1:5120:1048576"
CASES="${CASES:-$DEFAULT_CASES}"
usage() {
@@ -39,15 +58,37 @@ Environment:
THREADS=N pass -t N to both tools; 0 omits -t (default: $THREADS)
REDUNDANCY=N create redundancy percentage (default: $REDUNDANCY)
RECOVERY_FILES=N recovery file count via -n (default: $RECOVERY_FILES)
+ FIRST_RECOVERY_BLOCK=N
+ pass -f N to both tools; default unset
PAR2CMD_BIN=PATH par2cmdline-turbo binary (default: $PAR2CMD_BIN)
+ INCLUDE_PSHUFB=1 include the par2rs PSHUFB baseline (default: $INCLUDE_PSHUFB)
+ WORK_ROOT=DIR generated corpus and PAR2 work directory
+ default: result run directory/work
+ CORPUS_ROOT=DIR reusable corpus root, separate from per-run work/output dirs
+ default: under WORK_ROOT//corpus
VERIFY_OUTPUTS=0 skip cross-tool verification after each create
- RUN_FLAMEGRAPH=0 skip par2rs perf/inferno flamegraph generation
- PROFILE_FREQUENCY=N perf sampling frequency for flamegraph (default: $PROFILE_FREQUENCY)
+ VERIFY_REPAIR=MODE run destructive cross-tool repair validation: smoke, 1, or 0
+ default: $VERIFY_REPAIR
+ Required tools depend on platform/arch.
+ Forced JIT runs set PAR2RS_CREATE_XOR_JIT_FALLBACK=error.
+ RUN_FLAMEGRAPH=0 skip par2rs flamegraph generation
+ PROFILE_CASE=LABEL profile this CASES label; default profiles the last case
+ PROFILE_TOOL=TOOL par2rs tool variant to profile (default: platform-specific)
+ PROFILE_FREQUENCY=N sampling frequency for flamegraph (default: $PROFILE_FREQUENCY)
+ CACHE_PROFILE=1 run Linux cache-miss attribution for the profiled case
+ CACHE_PROFILE_TOOL=TOOL par2rs variant for cache profile (default: PROFILE_TOOL)
+ CACHE_PROFILE_JIT_DUMPS=0 skip xor-jit byte dumps during cache profiles (default: $CACHE_PROFILE_JIT_DUMPS)
+ also disables per-overwrite coefficient perf-map labels
+ CACHE_LOAD_LATENCY=N minimum load latency for precise mem-load sampling (default: $CACHE_LOAD_LATENCY)
+ CACHE_STAT_EVENTS=EVENTS comma-separated cache counter list
KEEP_WORK=1 keep generated benchmark work directory
Example:
- nix develop --command env ITERATIONS=40 THREADS=16 \\
- CASES='single_256m:1:256:1048576,multi_1g:64:16:1048576' \\
+ nix develop --command env ITERATIONS=10 THREADS=16 PROFILE_CASE=single_5g \\
+ WORK_ROOT="\$HOME/uncompressed/par2rs-create-perf/manual-run" \\
+ CORPUS_ROOT="\$HOME/uncompressed/par2rs-create-perf/corpus-cache" \\
+ RECOVERY_FILES=8 FIRST_RECOVERY_BLOCK=1 \\
+ CASES='single_256m:1:256:1048576,multi_1g:64:16:1048576,single_5g:1:5120:1048576' \\
scripts/benchmark_create_perf.sh
EOF
}
@@ -65,29 +106,88 @@ need_tool() {
fi
}
-need_tool perf
+tool_supported() {
+ local tool="$1"
+
+ case "$tool" in
+ turbo-auto|par2rs-auto|par2rs-scalar)
+ return 0
+ ;;
+ par2rs-pshufb|par2rs-xor-jit|par2rs-xor-jit-port)
+ [[ "$ARCH" == "x86_64" ]]
+ return
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
+BENCHMARK_TOOLS=()
+for tool in "${DEFAULT_BENCHMARK_TOOLS[@]}"; do
+ if tool_supported "$tool"; then
+ BENCHMARK_TOOLS+=("$tool")
+ fi
+done
+
+if [[ "${#BENCHMARK_TOOLS[@]}" == "1" && "${BENCHMARK_TOOLS[0]}" == "turbo-auto" ]]; then
+ BENCHMARK_TOOLS=(par2rs-auto turbo-auto)
+fi
+
+if [[ -z "$PROFILE_TOOL" ]]; then
+ if tool_supported "par2rs-xor-jit"; then
+ PROFILE_TOOL="par2rs-xor-jit"
+ else
+ for tool in "${BENCHMARK_TOOLS[@]}"; do
+ if [[ "$tool" == par2rs-* ]]; then
+ PROFILE_TOOL="$tool"
+ break
+ fi
+ done
+ fi
+fi
+
+if ! tool_supported "$PROFILE_TOOL"; then
+ echo "error: PROFILE_TOOL '$PROFILE_TOOL' is unsupported on $PLATFORM/$ARCH" >&2
+ exit 1
+fi
+
+if [[ -z "$CACHE_PROFILE_TOOL" ]]; then
+ CACHE_PROFILE_TOOL="$PROFILE_TOOL"
+fi
+if ! tool_supported "$CACHE_PROFILE_TOOL"; then
+ echo "error: CACHE_PROFILE_TOOL '$CACHE_PROFILE_TOOL' is unsupported on $PLATFORM/$ARCH" >&2
+ exit 1
+fi
+
need_tool python3
need_tool dd
+need_tool cmp
need_tool "$PAR2CMD_BIN"
-if ! perf stat -x, -e instructions -- true >/dev/null 2>&1; then
- echo "error: perf cannot read the instructions counter." >&2
- echo "Check kernel.perf_event_paranoid or run in an environment with perf permissions." >&2
- exit 1
+if [[ "$PLATFORM" == "Linux" ]]; then
+ need_tool perf
+ if ! perf stat -x, -e instructions -- true >/dev/null 2>&1; then
+ echo "error: perf cannot read the instructions counter." >&2
+ echo "Check kernel.perf_event_paranoid or run in an environment with perf permissions." >&2
+ exit 1
+ fi
fi
mkdir -p "$RESULTS_ROOT"
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
RUN_ROOT="$RESULTS_ROOT/run-$TIMESTAMP"
-WORK_ROOT="$RUN_ROOT/work"
+WORK_ROOT="${WORK_ROOT:-$RUN_ROOT/work}"
+CORPUS_ROOT="${CORPUS_ROOT:-}"
RAW_CSV="$RUN_ROOT/raw.csv"
SUMMARY_MD="$RUN_ROOT/summary.md"
SMOKE_CSV="$RUN_ROOT/smoke.csv"
SMOKE_SUMMARY_MD="$RUN_ROOT/smoke-summary.md"
-mkdir -p "$WORK_ROOT"
+PRESERVE_WORK=0
+mkdir -p "$RUN_ROOT" "$WORK_ROOT"
cleanup() {
- if [[ "$KEEP_WORK" != "1" ]]; then
+ if [[ "$KEEP_WORK" != "1" && "$PRESERVE_WORK" != "1" ]]; then
rm -rf "$WORK_ROOT"
fi
}
@@ -95,36 +195,109 @@ trap cleanup EXIT
echo "=== par2rs create perf benchmark ==="
echo "results: $RUN_ROOT"
+echo "work: $WORK_ROOT"
+if [[ -n "$CORPUS_ROOT" ]]; then
+ echo "corpus: $CORPUS_ROOT"
+fi
echo "iterations: $ITERATIONS measured, $WARMUP_RUNS warmup"
echo "cases: $CASES"
echo "threads: $THREADS"
+echo "recovery files: $RECOVERY_FILES"
+if [[ -n "$FIRST_RECOVERY_BLOCK" ]]; then
+ echo "first recovery block: $FIRST_RECOVERY_BLOCK"
+fi
+echo "platform: $PLATFORM/$ARCH"
+echo "tools: ${BENCHMARK_TOOLS[*]}"
+echo "profile tool: $PROFILE_TOOL"
+if [[ "$CACHE_PROFILE" == "1" ]]; then
+ echo "cache profile tool: $CACHE_PROFILE_TOOL"
+fi
echo
+probe_turbo_method() {
+ local probe_dir="$RUN_ROOT/turbo-method-probe"
+ local stdout_file="$RUN_ROOT/turbo-method-probe.stdout"
+ local stderr_file="$RUN_ROOT/turbo-method-probe.stderr"
+ rm -rf "$probe_dir"
+ mkdir -p "$probe_dir"
+
+ python3 - "$probe_dir/file.bin" <<'PY'
+import sys
+
+with open(sys.argv[1], "wb") as f:
+ for idx in range(1024):
+ f.write(bytes([(idx * 17 + 3) & 0xFF]) * 1024)
+PY
+
+ local probe_recovery_files="$RECOVERY_FILES"
+ if [[ -z "$probe_recovery_files" || "$probe_recovery_files" -lt 1 ]]; then
+ probe_recovery_files=1
+ fi
+ local -a probe_cmd=("$PAR2CMD_BIN" c -v -s1048576 -r10 "-n$probe_recovery_files")
+ if [[ -n "$FIRST_RECOVERY_BLOCK" ]]; then
+ probe_cmd+=("-f$FIRST_RECOVERY_BLOCK")
+ fi
+ if [[ "$THREADS" != "0" ]]; then
+ probe_cmd+=("-t$THREADS")
+ fi
+
+ if ! (cd "$probe_dir" && "${probe_cmd[@]}" out.par2 file.bin >"$stdout_file" 2>"$stderr_file"); then
+ rm -rf "$probe_dir"
+ printf 'unknown'
+ return 0
+ fi
+
+ local method
+ method="$(awk -F': ' '/Multiply method:/ {print $2; exit}' "$stdout_file" "$stderr_file")"
+ rm -rf "$probe_dir"
+ printf '%s' "${method:-unknown}"
+}
+
echo "Building par2rs release binary..."
(cd "$PROJECT_ROOT" && cargo build --release --bin par2 --quiet)
write_csv_header() {
local csv_file="$1"
- printf 'case,tool,run,status,validation_status,instructions,cycles,branches,branch_misses,cache_references,cache_misses,task_clock_msec,context_switches,cpu_migrations,page_faults,wall_seconds\n' > "$csv_file"
+ printf 'case,tool,selected_method,run,status,validation_status,instructions,cycles,branches,branch_misses,cache_references,cache_misses,task_clock_msec,context_switches,cpu_migrations,page_faults,wall_seconds\n' > "$csv_file"
}
write_csv_header "$RAW_CSV"
write_csv_header "$SMOKE_CSV"
+TURBO_OBSERVED_METHOD="$(probe_turbo_method)"
+echo "turbo observed method: $TURBO_OBSERVED_METHOD"
+echo
+
+now_ns() {
+ python3 - <<'PY'
+import time
+print(time.time_ns())
+PY
+}
+
split_cases() {
tr ',' '\n' <<< "$CASES"
}
+corpus_dir_for_case() {
+ local label="$1"
+ if [[ -n "$CORPUS_ROOT" ]]; then
+ printf '%s/%s/corpus\n' "$CORPUS_ROOT" "$label"
+ else
+ printf '%s/%s/corpus\n' "$WORK_ROOT" "$label"
+ fi
+}
+
make_corpus() {
- local case_dir="$1"
+ local corpus_dir="$1"
local file_count="$2"
local file_size_mib="$3"
- mkdir -p "$case_dir/corpus"
+ mkdir -p "$corpus_dir"
local i
for i in $(seq 1 "$file_count"); do
local file
- file="$case_dir/corpus/file_$(printf '%04d' "$i").bin"
+ file="$corpus_dir/file_$(printf '%04d' "$i").bin"
if [[ ! -f "$file" ]]; then
python3 - "$file" "$i" "$file_size_mib" <<'PY'
import sys
@@ -160,6 +333,9 @@ link_corpus() {
perf_value() {
local perf_file="$1"
local event="$2"
+ if [[ ! -f "$perf_file" ]]; then
+ return 0
+ fi
awk -F, -v event="$event" '
{
parsed_event = $3
@@ -192,26 +368,61 @@ run_create() {
link_corpus "$corpus_dir" "$run_dir"
local -a cmd
- if [[ "$tool" == "par2rs" ]]; then
+ local selected_method="$tool"
+ local -a env_prefix=()
+ if [[ "$tool" == par2rs-* ]]; then
+ case "$tool" in
+ par2rs-auto)
+ selected_method="auto"
+ env_prefix=()
+ ;;
+ par2rs-pshufb)
+ selected_method="pshufb"
+ env_prefix=(env PAR2RS_CREATE_GF16=pshufb)
+ ;;
+ par2rs-xor-jit|par2rs-xor-jit-port)
+ selected_method="xor-jit"
+ env_prefix=(env PAR2RS_CREATE_GF16=xor-jit PAR2RS_CREATE_XOR_JIT_FALLBACK=error)
+ ;;
+ par2rs-scalar)
+ selected_method="scalar"
+ env_prefix=(env PAR2RS_CREATE_GF16=scalar)
+ ;;
+ *)
+ echo "error: unknown par2rs variant: $tool" >&2
+ return 2
+ ;;
+ esac
cmd=("$PAR2RS_BIN" c -q -q "-s$block_size" "-r$REDUNDANCY" "-n$RECOVERY_FILES")
else
+ selected_method="auto"
cmd=("$PAR2CMD_BIN" c -q -q "-s$block_size" "-r$REDUNDANCY" "-n$RECOVERY_FILES")
fi
+ if [[ -n "$FIRST_RECOVERY_BLOCK" ]]; then
+ cmd+=("-f$FIRST_RECOVERY_BLOCK")
+ fi
if [[ "$THREADS" != "0" ]]; then
cmd+=("-t$THREADS")
fi
-
local start_ns end_ns status validation_status wall_seconds
- start_ns="$(date +%s%N)"
+ start_ns="$(now_ns)"
set +e
- (
- cd "$run_dir"
- sources=(./*.bin)
- perf stat -x, -o "$perf_file" -e "$PERF_EVENTS" -- "${cmd[@]}" out.par2 "${sources[@]}" >/dev/null
- )
+ if [[ "$PLATFORM" == "Linux" ]]; then
+ (
+ cd "$run_dir"
+ sources=(./*.bin)
+ perf stat -x, -o "$perf_file" -e "$PERF_EVENTS" -- "${env_prefix[@]}" "${cmd[@]}" out.par2 "${sources[@]}" >/dev/null
+ )
+ else
+ (
+ cd "$run_dir"
+ sources=(./*.bin)
+ "${env_prefix[@]}" "${cmd[@]}" out.par2 "${sources[@]}" >/dev/null
+ )
+ fi
status="$?"
set -e
- end_ns="$(date +%s%N)"
+ end_ns="$(now_ns)"
wall_seconds="$(awk -v s="$start_ns" -v e="$end_ns" 'BEGIN { printf "%.6f", (e - s) / 1000000000 }')"
validation_status=0
@@ -220,7 +431,7 @@ run_create() {
verifier_stdout="$RUN_ROOT/verify-$case_label-$tool-$run_number.stdout"
verifier_stderr="$RUN_ROOT/verify-$case_label-$tool-$run_number.stderr"
set +e
- if [[ "$tool" == "par2rs" ]]; then
+ if [[ "$tool" == par2rs-* ]]; then
(cd "$run_dir" && "$PAR2CMD_BIN" v -q -q out.par2 >"$verifier_stdout" 2>"$verifier_stderr")
else
(cd "$run_dir" && "$PAR2RS_BIN" verify -q -q out.par2 >"$verifier_stdout" 2>"$verifier_stderr")
@@ -233,7 +444,7 @@ run_create() {
echo "quiet validation failed with status $validation_status; rerunning verbosely"
} >> "$verifier_stderr"
set +e
- if [[ "$tool" == "par2rs" ]]; then
+ if [[ "$tool" == par2rs-* ]]; then
(cd "$run_dir" && "$PAR2CMD_BIN" v out.par2 >>"$verifier_stdout" 2>>"$verifier_stderr")
else
(cd "$run_dir" && "$PAR2RS_BIN" verify out.par2 >>"$verifier_stdout" 2>>"$verifier_stderr")
@@ -246,6 +457,24 @@ run_create() {
else
rm -f "$verifier_stdout" "$verifier_stderr"
fi
+
+ if [[ "$validation_status" == "0" ]] && should_repair_validate "$run_number"; then
+ local repair_stdout repair_stderr
+ repair_stdout="$RUN_ROOT/repair-$case_label-$tool-$run_number.stdout"
+ repair_stderr="$RUN_ROOT/repair-$case_label-$tool-$run_number.stderr"
+ set +e
+ validate_repair_output "$tool" "$run_dir" "$corpus_dir" "$block_size" "$repair_stdout" "$repair_stderr"
+ validation_status="$?"
+ set -e
+ if [[ "$validation_status" != "0" ]]; then
+ echo "error: cross-tool repair validation failed for $case_label/$tool run $run_number" >&2
+ echo "stdout: $repair_stdout" >&2
+ echo "stderr: $repair_stderr" >&2
+ echo "work dir: $run_dir" >&2
+ else
+ rm -f "$repair_stdout" "$repair_stderr"
+ fi
+ fi
fi
if [[ "$measured" == "1" ]]; then
@@ -261,8 +490,8 @@ run_create() {
context_switches="$(perf_value "$perf_file" context-switches)"
cpu_migrations="$(perf_value "$perf_file" cpu-migrations)"
page_faults="$(perf_value "$perf_file" page-faults)"
- printf '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' \
- "$case_label" "$tool" "$run_number" "$status" "$validation_status" \
+ printf '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' \
+ "$case_label" "$tool" "$selected_method" "$run_number" "$status" "$validation_status" \
"$instructions" "$cycles" "$branches" "$branch_misses" \
"$cache_references" "$cache_misses" "$task_clock" \
"$context_switches" "$cpu_migrations" "$page_faults" "$wall_seconds" >> "$output_csv"
@@ -271,6 +500,7 @@ run_create() {
if [[ "$status" == "0" && "$validation_status" == "0" ]]; then
rm -rf "$run_dir"
elif [[ "$KEEP_WORK" != "1" ]]; then
+ PRESERVE_WORK=1
echo "preserving failed run directory: $run_dir" >&2
fi
if [[ "$status" != "0" ]]; then
@@ -279,6 +509,66 @@ run_create() {
return "$validation_status"
}
+should_repair_validate() {
+ local run_number="$1"
+ case "$VERIFY_REPAIR" in
+ 1|true|yes)
+ return 0
+ ;;
+ smoke)
+ [[ "$run_number" == "smoke" ]]
+ return
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
+validate_repair_output() {
+ local tool="$1"
+ local run_dir="$2"
+ local corpus_dir="$3"
+ local block_size="$4"
+ local repair_stdout="$5"
+ local repair_stderr="$6"
+
+ local target base
+ target="$(find "$run_dir" -maxdepth 1 -type f -name '*.bin' | sort | head -n 1)"
+ if [[ -z "$target" ]]; then
+ echo "no source file found to damage" > "$repair_stderr"
+ return 97
+ fi
+ base="$(basename "$target")"
+
+ cp -p "$target" "$target.detached"
+ mv "$target.detached" "$target"
+ python3 - "$target" "$block_size" <<'PY'
+import sys
+
+path = sys.argv[1]
+block_size = int(sys.argv[2])
+
+with open(path, "r+b") as f:
+ data = f.read(block_size)
+ if not data:
+ sys.exit(2)
+ f.seek(0)
+ f.write(bytes(b ^ 0xFF for b in data))
+PY
+
+ if [[ "$tool" == par2rs-* ]]; then
+ (cd "$run_dir" && "$PAR2CMD_BIN" r -q -q out.par2 >"$repair_stdout" 2>"$repair_stderr")
+ else
+ (cd "$run_dir" && "$PAR2RS_BIN" repair -q -q out.par2 >"$repair_stdout" 2>"$repair_stderr")
+ fi || return "$?"
+
+ if ! cmp -s "$corpus_dir/$base" "$run_dir/$base"; then
+ echo "repaired file differs from original corpus file: $base" >> "$repair_stderr"
+ return 98
+ fi
+}
+
summarize_results() {
local raw_csv="${1:-$RAW_CSV}"
local summary_md="${2:-$SUMMARY_MD}"
@@ -286,14 +576,14 @@ summarize_results() {
local warmups="${4:-$WARMUP_RUNS}"
local title="${5:-PAR2 Create Perf Benchmark}"
- python3 - "$raw_csv" "$summary_md" "$iterations" "$warmups" "$THREADS" "$REDUNDANCY" "$RECOVERY_FILES" "$title" <<'PY'
+ python3 - "$raw_csv" "$summary_md" "$iterations" "$warmups" "$THREADS" "$REDUNDANCY" "$RECOVERY_FILES" "$FIRST_RECOVERY_BLOCK" "$title" "$TURBO_OBSERVED_METHOD" <<'PY'
import csv
import math
import statistics
import sys
from collections import defaultdict
-raw_csv, summary_md, iterations, warmups, threads, redundancy, recovery_files, title = sys.argv[1:]
+raw_csv, summary_md, iterations, warmups, threads, redundancy, recovery_files, first_recovery_block, title, turbo_observed_method = sys.argv[1:]
metrics = [
("instructions", "instructions"),
("cycles", "cycles"),
@@ -340,6 +630,16 @@ def fmt(value):
return f"{value:,.2f}"
return f"{value:.4f}"
+def metric_mean(case, tool, key):
+ group = groups.get((case, tool), [])
+ s = stats(number(r, key) for r in group)
+ return s[0] if s else None
+
+def ratio(num, den):
+ if num is None or den is None or den == 0:
+ return None
+ return num / den
+
cases = sorted({row["case"] for row in rows})
with open(summary_md, "w") as out:
@@ -349,29 +649,49 @@ with open(summary_md, "w") as out:
out.write(f"- threads: {threads}\n")
out.write(f"- redundancy: {redundancy}%\n")
out.write(f"- recovery files: {recovery_files}\n")
- out.write("- validation: par2rs output verified by turbo; turbo output verified by par2rs\n")
- out.write("- primary signal: Linux perf `instructions`\n\n")
+ out.write(f"- first recovery block: {first_recovery_block or 'default'}\n")
+ out.write(f"- turbo observed method: {turbo_observed_method}\n")
+ out.write("- validation: par2rs output verified/repaired by turbo; turbo output verified/repaired by par2rs\n")
+ out.write("- primary signal: wall seconds; Linux perf counters are recorded for diagnostics when available\n\n")
out.write("## Relative Summary\n\n")
- out.write("| case | par2rs instructions | turbo instructions | par2rs/turbo | par2rs cycles | turbo cycles | par2rs/turbo cycles |\n")
- out.write("|---|---:|---:|---:|---:|---:|---:|\n")
+ out.write("| case | tool | method | instructions | cycles | wall seconds | effective CPU |\n")
+ out.write("|---|---|---|---:|---:|---:|---:|\n")
+ for case in cases:
+ case_tools = sorted({row["tool"] for row in rows if row["case"] == case})
+ for tool in case_tools:
+ group = groups[(case, tool)]
+ method = group[0].get("selected_method", tool) if group else tool
+ instr = stats(number(r, "instructions") for r in group)
+ cycles = stats(number(r, "cycles") for r in group)
+ wall = stats(number(r, "wall_seconds") for r in group)
+ task_clock = stats(number(r, "task_clock_msec") for r in group)
+ effective_cpu = task_clock[0] / (wall[0] * 1000) if task_clock and wall and wall[0] else None
+ out.write(
+ f"| {case} | {tool} | {method} | {fmt(instr[0] if instr else None)} | "
+ f"{fmt(cycles[0] if cycles else None)} | {fmt(wall[0] if wall else None)} | {fmt(effective_cpu)} |\n"
+ )
+
+ out.write("\n## XOR-JIT Ratios\n\n")
+ out.write("| case | wall / turbo | instructions / turbo | cache misses / turbo |\n")
+ out.write("|---|---:|---:|---:|\n")
for case in cases:
- p_instr = stats(number(r, "instructions") for r in groups[(case, "par2rs")])
- t_instr = stats(number(r, "instructions") for r in groups[(case, "turbo")])
- p_cycles = stats(number(r, "cycles") for r in groups[(case, "par2rs")])
- t_cycles = stats(number(r, "cycles") for r in groups[(case, "turbo")])
- instr_ratio = p_instr[0] / t_instr[0] if p_instr and t_instr and t_instr[0] else None
- cycle_ratio = p_cycles[0] / t_cycles[0] if p_cycles and t_cycles and t_cycles[0] else None
+ xor_wall = metric_mean(case, "par2rs-xor-jit", "wall_seconds")
+ turbo_wall = metric_mean(case, "turbo-auto", "wall_seconds")
+ xor_instr = metric_mean(case, "par2rs-xor-jit", "instructions")
+ turbo_instr = metric_mean(case, "turbo-auto", "instructions")
+ xor_cache = metric_mean(case, "par2rs-xor-jit", "cache_misses")
+ turbo_cache = metric_mean(case, "turbo-auto", "cache_misses")
out.write(
- f"| {case} | {fmt(p_instr[0] if p_instr else None)} | {fmt(t_instr[0] if t_instr else None)} | "
- f"{fmt(instr_ratio)} | {fmt(p_cycles[0] if p_cycles else None)} | {fmt(t_cycles[0] if t_cycles else None)} | {fmt(cycle_ratio)} |\n"
+ f"| {case} | {fmt(ratio(xor_wall, turbo_wall))} | "
+ f"{fmt(ratio(xor_instr, turbo_instr))} | {fmt(ratio(xor_cache, turbo_cache))} |\n"
)
out.write("\n## Detailed Stats\n\n")
out.write("| case | tool | metric | n | mean | stddev | cv % | min | max |\n")
out.write("|---|---|---|---:|---:|---:|---:|---:|---:|\n")
for case in cases:
- for tool in ("par2rs", "turbo"):
+ for tool in sorted({row["tool"] for row in rows if row["case"] == case}):
group = groups[(case, tool)]
failures = [r for r in group if r["status"] != "0"]
validation_failures = [r for r in group if r.get("validation_status", "0") != "0"]
@@ -389,6 +709,82 @@ with open(summary_md, "w") as out:
PY
}
+enforce_pass_criteria() {
+ local raw_csv="${1:-$RAW_CSV}"
+
+ if ! tool_supported "par2rs-xor-jit"; then
+ echo "Skipping XOR-JIT pass criteria on $PLATFORM/$ARCH."
+ return 0
+ fi
+
+ python3 - "$raw_csv" <<'PY'
+import csv
+import math
+import statistics
+import sys
+from collections import defaultdict
+
+raw_csv = sys.argv[1]
+required = ["par2rs-xor-jit", "turbo-auto"]
+groups = defaultdict(list)
+failures = []
+
+with open(raw_csv, newline="") as f:
+ for row in csv.DictReader(f):
+ if row.get("status") != "0" or row.get("validation_status") != "0":
+ failures.append(row)
+ groups[(row["case"], row["tool"])].append(row)
+
+def mean_wall(rows):
+ values = []
+ for row in rows:
+ try:
+ value = float(row.get("wall_seconds", ""))
+ except ValueError:
+ continue
+ if math.isfinite(value):
+ values.append(value)
+ return statistics.fmean(values) if values else None
+
+errors = []
+if failures:
+ for row in failures:
+ errors.append(
+ f"{row['case']}/{row['tool']} run {row['run']} status={row.get('status')} "
+ f"validation={row.get('validation_status')}"
+ )
+
+cases = sorted({case for case, _tool in groups})
+for case in cases:
+ missing = [tool for tool in required if not groups.get((case, tool))]
+ if missing:
+ errors.append(f"{case}: missing required tools: {', '.join(missing)}")
+ continue
+
+ turbo = mean_wall(groups[(case, "turbo-auto")])
+ if turbo is None:
+ errors.append(f"{case}: missing turbo-auto wall_seconds")
+ continue
+
+ tool = "par2rs-xor-jit"
+ wall = mean_wall(groups[(case, tool)])
+ if wall is None:
+ errors.append(f"{case}: missing {tool} wall_seconds")
+ elif wall > turbo:
+ errors.append(
+ f"{case}: {tool} mean wall {wall:.6f}s is slower than turbo-auto {turbo:.6f}s"
+ )
+
+if errors:
+ print("error: XOR-JIT performance pass criteria failed", file=sys.stderr)
+ for error in errors:
+ print(f" - {error}", file=sys.stderr)
+ sys.exit(1)
+
+print("XOR-JIT performance pass criteria passed.")
+PY
+}
+
run_smoke_benchmarks() {
echo
echo "Smoke benchmark: one measured, cross-validated run for each configured case/tool"
@@ -401,15 +797,15 @@ run_smoke_benchmarks() {
fi
local case_dir="$WORK_ROOT/$label"
- local corpus_dir="$case_dir/corpus"
+ local corpus_dir
+ corpus_dir="$(corpus_dir_for_case "$label")"
echo "Smoke preparing '$label': ${file_count}x${file_size_mib}MiB, block size ${block_size}"
- make_corpus "$case_dir" "$file_count" "$file_size_mib"
-
- echo "Smoke measured: $label / par2rs"
- run_create "$label" "par2rs" "smoke" "$block_size" "$corpus_dir" 1 "$SMOKE_CSV"
+ make_corpus "$corpus_dir" "$file_count" "$file_size_mib"
- echo "Smoke measured: $label / turbo"
- run_create "$label" "turbo" "smoke" "$block_size" "$corpus_dir" 1 "$SMOKE_CSV"
+ for tool in "${BENCHMARK_TOOLS[@]}"; do
+ echo "Smoke measured: $label / $tool"
+ run_create "$label" "$tool" "smoke" "$block_size" "$corpus_dir" 1 "$SMOKE_CSV"
+ done
done < <(split_cases)
summarize_results "$SMOKE_CSV" "$SMOKE_SUMMARY_MD" 1 0 "PAR2 Create Perf Smoke Benchmark"
@@ -476,29 +872,76 @@ for name, count in inclusive.most_common(40):
'
}
+par2rs_tool_env() {
+ local tool="$1"
+ local -a env_args=(env)
+
+ case "$tool" in
+ par2rs-auto)
+ ;;
+ par2rs-pshufb)
+ env_args+=(PAR2RS_CREATE_GF16=pshufb)
+ ;;
+ par2rs-xor-jit|par2rs-xor-jit-port)
+ env_args+=(PAR2RS_CREATE_GF16=xor-jit PAR2RS_CREATE_XOR_JIT_FALLBACK=error)
+ ;;
+ par2rs-scalar)
+ env_args+=(PAR2RS_CREATE_GF16=scalar)
+ ;;
+ *)
+ echo "error: PROFILE_TOOL must be a par2rs variant, got: $tool" >&2
+ return 1
+ ;;
+ esac
+ env_args+=(PAR2RS_XOR_JIT_PERF_MAP=1)
+ printf '%s\0' "${env_args[@]}"
+}
+
+par2rs_tool_env_args() {
+ local tool="$1"
+ local -a env_args=()
+
+ case "$tool" in
+ par2rs-auto)
+ ;;
+ par2rs-pshufb)
+ env_args+=(PAR2RS_CREATE_GF16=pshufb)
+ ;;
+ par2rs-xor-jit|par2rs-xor-jit-port)
+ env_args+=(PAR2RS_CREATE_GF16=xor-jit PAR2RS_CREATE_XOR_JIT_FALLBACK=error)
+ ;;
+ par2rs-scalar)
+ env_args+=(PAR2RS_CREATE_GF16=scalar)
+ ;;
+ *)
+ echo "error: PROFILE_TOOL must be a par2rs variant, got: $tool" >&2
+ return 1
+ ;;
+ esac
+ env_args+=(PAR2RS_XOR_JIT_PERF_MAP=1)
+ if [[ "${#env_args[@]}" -gt 0 ]]; then
+ printf '%s\0' "${env_args[@]}"
+ fi
+}
+
generate_flamegraph() {
local case_spec="$1"
IFS=: read -r label file_count file_size_mib block_size <<< "$case_spec"
local case_dir="$WORK_ROOT/$label"
- local corpus_dir="$case_dir/corpus"
- local flamegraph_file="$RUN_ROOT/par2rs-create-$label-flamegraph.svg"
+ local corpus_dir
+ corpus_dir="$(corpus_dir_for_case "$label")"
+ local flamegraph_file="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL-flamegraph.svg"
local output_file="$case_dir/flamegraph-out.par2"
if [[ "$RUN_FLAMEGRAPH" != "1" ]]; then
return 0
fi
- if ! command -v inferno-collapse-perf >/dev/null 2>&1 || ! command -v inferno-flamegraph >/dev/null 2>&1; then
- if command -v cargo >/dev/null 2>&1; then
- echo "inferno not found; installing with cargo install inferno..."
- cargo install inferno
- else
- echo "warning: inferno is unavailable; skipping flamegraph" >&2
- return 0
- fi
- fi
rm -f "$case_dir"/flamegraph-out*.par2
local -a args=(c -q -q "-s$block_size" "-r$REDUNDANCY" "-n$RECOVERY_FILES")
+ if [[ -n "$FIRST_RECOVERY_BLOCK" ]]; then
+ args+=("-f$FIRST_RECOVERY_BLOCK")
+ fi
if [[ "$THREADS" != "0" ]]; then
args+=("-t$THREADS")
fi
@@ -508,8 +951,169 @@ generate_flamegraph() {
args+=("$file")
done
+ case "$PLATFORM" in
+ Linux)
+ if ! command -v inferno-collapse-perf >/dev/null 2>&1 || ! command -v inferno-flamegraph >/dev/null 2>&1; then
+ if command -v cargo >/dev/null 2>&1; then
+ echo "inferno not found; installing with cargo install inferno..."
+ cargo install inferno
+ else
+ echo "warning: inferno is unavailable; skipping flamegraph" >&2
+ return 0
+ fi
+ fi
+
+ echo
+ echo "Building par2rs profiling binary with frame pointers..."
+ (
+ cd "$PROJECT_ROOT"
+ RUSTFLAGS="-C target-cpu=native -C force-frame-pointers=yes" \
+ cargo build --profile profiling --bin par2 --quiet
+ )
+
+ local profiling_bin="$PROJECT_ROOT/target/profiling/par2"
+ local perf_data="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.perf.data"
+ local perf_summary="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.perf-report.txt"
+ local hotspots="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.hotspots.txt"
+ local -a env_prefix
+ mapfile -d '' -t env_prefix < <(par2rs_tool_env "$PROFILE_TOOL")
+
+ echo "Recording par2rs flamegraph for case '$label' / $PROFILE_TOOL..."
+ if ! perf record -g --call-graph fp -F "$PROFILE_FREQUENCY" -o "$perf_data" -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ echo "warning: flamegraph generation failed; benchmark results are still available" >&2
+ return 0
+ fi
+
+ echo "Rendering flamegraph SVG..."
+ perf script -i "$perf_data" 2>/dev/null | inferno-collapse-perf | inferno-flamegraph > "$flamegraph_file"
+
+ echo "Generating perf report text..."
+ perf report -i "$perf_data" --stdio --sort comm,dso,symbol > "$perf_summary" 2>/dev/null || true
+
+ echo "Generating hotspot summary..."
+ perf script -i "$perf_data" 2>/dev/null | summarize_perf_script > "$hotspots" || true
+
+ echo "flamegraph: $flamegraph_file"
+ echo "perf data: $perf_data"
+ echo "perf text: $perf_summary"
+ echo "hotspots: $hotspots"
+ ;;
+ Darwin)
+ if ! command -v xctrace >/dev/null 2>&1; then
+ echo "warning: xctrace is unavailable; skipping flamegraph" >&2
+ return 0
+ fi
+ if ! command -v inferno-collapse-xctrace >/dev/null 2>&1; then
+ echo "warning: inferno-collapse-xctrace is unavailable; skipping flamegraph" >&2
+ return 0
+ fi
+ if ! command -v inferno-flamegraph >/dev/null 2>&1; then
+ echo "warning: inferno-flamegraph is unavailable; skipping flamegraph" >&2
+ return 0
+ fi
+
+ local -a env_vars
+ local xctrace_bin xctrace_dir developer_dir
+ local trace_file xml_file folded_file
+ mapfile -d '' -t env_vars < <(par2rs_tool_env_args "$PROFILE_TOOL")
+ xctrace_bin="$(command -v xctrace || true)"
+ if [[ -z "$xctrace_bin" ]]; then
+ xctrace_bin="$(xcrun --find xctrace 2>/dev/null || true)"
+ fi
+ if [[ -z "$xctrace_bin" ]]; then
+ echo "warning: xctrace is unavailable; skipping flamegraph" >&2
+ return 0
+ fi
+ xctrace_dir="$(dirname "$xctrace_bin")"
+ developer_dir="/Applications/Xcode.app/Contents/Developer"
+ if [[ ! -d "$developer_dir" ]]; then
+ developer_dir="$(dirname "$(dirname "$xctrace_dir")")"
+ fi
+ trace_file="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.trace"
+ xml_file="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.xml"
+ folded_file="$RUN_ROOT/par2rs-create-$label-$PROFILE_TOOL.folded"
+
+ echo
+ echo "Building par2rs profiling binary with frame pointers..."
+ (
+ cd "$PROJECT_ROOT"
+ RUSTFLAGS="-C target-cpu=native -C force-frame-pointers=yes" \
+ cargo build --profile profiling --bin par2 --quiet
+ ) || {
+ echo "warning: flamegraph generation failed; benchmark results are still available" >&2
+ return 0
+ }
+
+ local profiling_bin="$PROJECT_ROOT/target/profiling/par2"
+ local -a xctrace_cmd=(
+ "$xctrace_bin" record
+ --template "Time Profiler"
+ --output "$trace_file"
+ --target-stdout /dev/null
+ --launch --
+ "$profiling_bin"
+ )
+ local env_var
+ for env_var in "${env_vars[@]}"; do
+ xctrace_cmd+=(--env "$env_var")
+ done
+ xctrace_cmd+=("${args[@]}")
+
+ echo "Recording par2rs flamegraph for case '$label' / $PROFILE_TOOL with xctrace..."
+ env DEVELOPER_DIR="$developer_dir" PATH="$xctrace_dir:$PATH" \
+ "${xctrace_cmd[@]}" >/dev/null || {
+ echo "warning: xctrace recording failed; benchmark results are still available" >&2
+ return 0
+ }
+
+ echo "Exporting xctrace profile..."
+ env DEVELOPER_DIR="$developer_dir" PATH="$xctrace_dir:$PATH" \
+ xctrace export --input "$trace_file" \
+ --xpath '/trace-toc/*/data/table[@schema="time-profile"]' > "$xml_file" || {
+ echo "warning: xctrace export failed; benchmark results are still available" >&2
+ return 0
+ }
+
+ echo "Collapsing xctrace stacks..."
+ if ! inferno-collapse-xctrace "$xml_file" > "$folded_file"; then
+ echo "warning: xctrace stack collapse failed; benchmark results are still available" >&2
+ return 0
+ fi
+
+ echo "Rendering flamegraph SVG..."
+ if ! inferno-flamegraph "$folded_file" > "$flamegraph_file"; then
+ echo "warning: inferno flamegraph rendering failed; benchmark results are still available" >&2
+ return 0
+ fi
+
+ echo "flamegraph: $flamegraph_file"
+ echo "trace: $trace_file"
+ echo "xml: $xml_file"
+ echo "folded: $folded_file"
+ ;;
+ *)
+ echo "warning: flamegraph generation is unsupported on $PLATFORM; skipping flamegraph" >&2
+ ;;
+ esac
+}
+
+generate_cache_profile() {
+ local case_spec="$1"
+ IFS=: read -r label file_count file_size_mib block_size <<< "$case_spec"
+ local case_dir="$WORK_ROOT/$label"
+ local corpus_dir
+ corpus_dir="$(corpus_dir_for_case "$label")"
+
+ if [[ "$CACHE_PROFILE" != "1" ]]; then
+ return 0
+ fi
+ if [[ "$PLATFORM" != "Linux" ]]; then
+ echo "warning: cache profiling is currently Linux-only; skipping cache profile" >&2
+ return 0
+ fi
+
echo
- echo "Building par2rs profiling binary with frame pointers..."
+ echo "Preparing cache-miss attribution for case '$label' / $CACHE_PROFILE_TOOL..."
(
cd "$PROJECT_ROOT"
RUSTFLAGS="-C target-cpu=native -C force-frame-pointers=yes" \
@@ -517,32 +1121,118 @@ generate_flamegraph() {
)
local profiling_bin="$PROJECT_ROOT/target/profiling/par2"
- local perf_data="$RUN_ROOT/par2rs-create-$label.perf.data"
- local perf_summary="$RUN_ROOT/par2rs-create-$label.perf-report.txt"
- local hotspots="$RUN_ROOT/par2rs-create-$label.hotspots.txt"
+ local output_file="$case_dir/cache-profile-out.par2"
+ local cache_stat="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.cache-stat.txt"
+ local cache_miss_data="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.cache-misses.data"
+ local cache_miss_report="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.cache-misses-report.txt"
+ local cache_miss_script="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.cache-misses-script.txt"
+ local perf_mem_data="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.perf-mem.data"
+ local perf_mem_report="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.perf-mem-report.txt"
+ local precise_data="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.mem-loads.data"
+ local precise_report="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.mem-loads-report.txt"
+ local ibs_data="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.ibs-op.data"
+ local ibs_report="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.ibs-op-report.txt"
+ local ibs_script="$RUN_ROOT/par2rs-create-$label-$CACHE_PROFILE_TOOL.ibs-op-script.txt"
+ local -a env_prefix
+ local -a args=(c -q -q "-s$block_size" "-r$REDUNDANCY" "-n$RECOVERY_FILES")
+ if [[ -n "$FIRST_RECOVERY_BLOCK" ]]; then
+ args+=("-f$FIRST_RECOVERY_BLOCK")
+ fi
- echo "Recording par2rs flamegraph for case '$label'..."
- if ! perf record -g --call-graph fp -F "$PROFILE_FREQUENCY" -o "$perf_data" -- "$profiling_bin" "${args[@]}" >/dev/null; then
- echo "warning: flamegraph generation failed; benchmark results are still available" >&2
- return 0
+ mapfile -d '' -t env_prefix < <(par2rs_tool_env "$CACHE_PROFILE_TOOL")
+ if [[ "$CACHE_PROFILE_JIT_DUMPS" == "1" ]]; then
+ env_prefix+=(PAR2RS_XOR_JIT_DUMP_DIR="$RUN_ROOT/xor-jit-dumps")
+ else
+ env_prefix+=(PAR2RS_XOR_JIT_PERF_COEFF_LABELS=0)
+ fi
+ if [[ "$THREADS" != "0" ]]; then
+ args+=("-t$THREADS")
+ fi
+ args+=("$output_file")
+ local file
+ for file in "$corpus_dir"/*.bin; do
+ args+=("$file")
+ done
+
+ echo "Collecting targeted cache counters..."
+ rm -f "$case_dir"/cache-profile-out*.par2
+ if ! perf stat -o "$cache_stat" -d -d -d -e "$CACHE_STAT_EVENTS" -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ echo "warning: cache perf stat failed; see $cache_stat" >&2
+ fi
+
+ echo "Recording cache-miss callgraph samples..."
+ rm -f "$case_dir"/cache-profile-out*.par2
+ if perf record -g --call-graph fp -e cache-misses:u -o "$cache_miss_data" -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ perf report -i "$cache_miss_data" --stdio --sort comm,dso,symbol > "$cache_miss_report" 2>/dev/null || true
+ perf script -i "$cache_miss_data" > "$cache_miss_script" 2>/dev/null || true
+ echo "cache-misses data: $cache_miss_data"
+ echo "cache-misses text: $cache_miss_report"
+ echo "cache-misses script:$cache_miss_script"
+ else
+ echo "warning: cache-miss callgraph sampling failed; see perf permissions/counter availability" >&2
+ rm -f "$cache_miss_data"
+ fi
+
+ echo "Recording data-source samples with perf mem..."
+ rm -f "$case_dir"/cache-profile-out*.par2
+ if perf mem record -o "$perf_mem_data" --call-graph fp -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ echo "Generating perf mem report..."
+ perf mem report -i "$perf_mem_data" --stdio --sort symbol,dso,mem,local_weight > "$perf_mem_report" 2>/dev/null || true
+ echo "perf mem data: $perf_mem_data"
+ echo "perf mem report: $perf_mem_report"
+ else
+ echo "warning: perf mem record failed; falling back to precise mem-load sampling" >&2
+ rm -f "$perf_mem_data"
fi
- echo "Rendering flamegraph SVG..."
- perf script -i "$perf_data" 2>/dev/null | inferno-collapse-perf | inferno-flamegraph > "$flamegraph_file"
+ echo "Recording high-latency load samples..."
+ rm -f "$case_dir"/cache-profile-out*.par2
+ if perf record -g --call-graph fp -e "cpu/mem-loads,ldlat=${CACHE_LOAD_LATENCY}/P" -o "$precise_data" -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ echo "Generating high-latency load report..."
+ perf report -i "$precise_data" --stdio --sort comm,dso,symbol > "$precise_report" 2>/dev/null || true
+ echo "mem-loads data: $precise_data"
+ echo "mem-loads text: $precise_report"
+ else
+ echo "warning: precise mem-load sampling failed; trying AMD IBS op sampling" >&2
+ rm -f "$precise_data"
+ if [[ "$(perf list 2>/dev/null)" == *ibs_op* ]]; then
+ local old_perf_paranoid=""
+ if [[ -r /proc/sys/kernel/perf_event_paranoid ]]; then
+ old_perf_paranoid="$(cat /proc/sys/kernel/perf_event_paranoid)"
+ fi
+ if [[ -n "$old_perf_paranoid" && "$old_perf_paranoid" != "-1" ]]; then
+ if sudo -n true >/dev/null 2>&1; then
+ sudo -n sysctl -w kernel.perf_event_paranoid=-1 >/dev/null || true
+ else
+ echo "warning: AMD IBS needs system-wide perf access; sudo -n is unavailable" >&2
+ fi
+ fi
- echo "Generating perf report text..."
- perf report -i "$perf_data" --stdio --sort comm,dso,symbol > "$perf_summary" 2>/dev/null || true
+ rm -f "$case_dir"/cache-profile-out*.par2
+ if perf record -g --call-graph fp -e ibs_op// -o "$ibs_data" -- "${env_prefix[@]}" "$profiling_bin" "${args[@]}" >/dev/null; then
+ perf report -i "$ibs_data" --stdio --sort comm,dso,symbol > "$ibs_report" 2>/dev/null || true
+ perf script -i "$ibs_data" > "$ibs_script" 2>/dev/null || true
+ echo "ibs-op data: $ibs_data"
+ echo "ibs-op text: $ibs_report"
+ echo "ibs-op script: $ibs_script"
+ else
+ echo "warning: AMD IBS op sampling failed; see perf permissions and IBS PMU support" >&2
+ rm -f "$ibs_data"
+ fi
- echo "Generating hotspot summary..."
- perf script -i "$perf_data" 2>/dev/null | summarize_perf_script > "$hotspots" || true
+ if [[ -n "$old_perf_paranoid" && "$(cat /proc/sys/kernel/perf_event_paranoid 2>/dev/null || true)" != "$old_perf_paranoid" ]]; then
+ sudo -n sysctl -w kernel.perf_event_paranoid="$old_perf_paranoid" >/dev/null || true
+ fi
+ else
+ echo "warning: precise mem-load sampling failed; event may be unsupported on this CPU/kernel" >&2
+ fi
+ fi
- echo "flamegraph: $flamegraph_file"
- echo "perf data: $perf_data"
- echo "perf text: $perf_summary"
- echo "hotspots: $hotspots"
+ echo "cache stat: $cache_stat"
}
last_case=""
+profile_case_spec=""
run_smoke_benchmarks
while IFS= read -r case_spec; do
@@ -554,12 +1244,15 @@ while IFS= read -r case_spec; do
fi
last_case="$case_spec"
+ if [[ -n "$PROFILE_CASE" && ( "$PROFILE_CASE" == "$label" || "$PROFILE_CASE" == "$case_spec" ) ]]; then
+ profile_case_spec="$case_spec"
+ fi
case_dir="$WORK_ROOT/$label"
- corpus_dir="$case_dir/corpus"
+ corpus_dir="$(corpus_dir_for_case "$label")"
echo "Preparing case '$label': ${file_count}x${file_size_mib}MiB, block size ${block_size}"
- make_corpus "$case_dir" "$file_count" "$file_size_mib"
+ make_corpus "$corpus_dir" "$file_count" "$file_size_mib"
- for tool in par2rs turbo; do
+ for tool in "${BENCHMARK_TOOLS[@]}"; do
echo "Warmup: $label / $tool"
for run in $(seq 1 "$WARMUP_RUNS"); do
run_create "$label" "$tool" "warmup-$run" "$block_size" "$corpus_dir" 0
@@ -575,9 +1268,18 @@ while IFS= read -r case_spec; do
done < <(split_cases)
summarize_results
-if [[ -n "$last_case" ]]; then
- generate_flamegraph "$last_case"
+if [[ -n "$PROFILE_CASE" && -z "$profile_case_spec" ]]; then
+ echo "error: PROFILE_CASE '$PROFILE_CASE' did not match any CASES label or spec" >&2
+ exit 1
+fi
+if [[ -z "$profile_case_spec" ]]; then
+ profile_case_spec="$last_case"
+fi
+if [[ -n "$profile_case_spec" ]]; then
+ generate_flamegraph "$profile_case_spec"
+ generate_cache_profile "$profile_case_spec"
fi
+enforce_pass_criteria "$RAW_CSV"
echo
echo "raw data: $RAW_CSV"
diff --git a/scripts/profile_create_slow_paths.sh b/scripts/profile_create_slow_paths.sh
new file mode 100755
index 00000000..26416380
--- /dev/null
+++ b/scripts/profile_create_slow_paths.sh
@@ -0,0 +1,358 @@
+#!/usr/bin/env bash
+# Profile representative par2rs create workloads against ParPar-backed comparison paths.
+#
+# The default comparison target is par2cmdline-turbo, which embeds ParPar's
+# create backend. Set PARPAR_BIN to also run a standalone ParPar-compatible CLI
+# if one is available in your environment.
+
+set -euo pipefail
+
+SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
+PROJECT_ROOT="${PROJECT_ROOT:-$(git -C "$SCRIPT_DIR/.." rev-parse --show-toplevel)}"
+PAR2RS_BIN="${PAR2RS_BIN:-$PROJECT_ROOT/target/release/par2}"
+PAR2CMD_BIN="${PAR2CMD_BIN:-par2}"
+PARPAR_BIN="${PARPAR_BIN:-}"
+RESULTS_ROOT="${RESULTS_ROOT:-$PROJECT_ROOT/target/perf-results/create-slow-paths}"
+WORK_ROOT="${WORK_ROOT:-}"
+ITERATIONS="${ITERATIONS:-3}"
+WARMUP_RUNS="${WARMUP_RUNS:-1}"
+THREADS="${THREADS:-1}"
+RECOVERY_FILES="${RECOVERY_FILES:-4}"
+KEEP_WORK="${KEEP_WORK:-0}"
+RUN_KERNEL_BENCH="${RUN_KERNEL_BENCH:-0}"
+TRACE_IO="${TRACE_IO:-0}"
+PERF_EVENTS="${PERF_EVENTS:-instructions,cache-misses,branches,branch-misses,task-clock,context-switches}"
+DATE_BIN="${DATE_BIN:-date}"
+
+# label:file_count:file_size_bytes:block_size:recovery_percent:memory_mb
+DEFAULT_WORKLOADS="small_full_block:1:8388608:1048576:10:0,large_capped:1:100663296:67108864:10:0,many_small_files:128:65536:65536:10:0,one_large_file:1:268435456:1048576:10:0,high_recovery_count:1:33554432:1048576:50:0,forced_slim_low_memory:1:33554432:8388608:10:16"
+WORKLOADS="${WORKLOADS:-$DEFAULT_WORKLOADS}"
+
+usage() {
+ cat </dev/null 2>&1; then
+ echo "error: required tool not found: $tool" >&2
+ exit 1
+ fi
+}
+
+need_tool perl
+need_tool cargo
+need_tool perf
+need_tool "$PAR2CMD_BIN"
+
+if ! perf stat -x, -e instructions -- true >/dev/null 2>&1; then
+ echo "error: perf cannot read hardware counters in this environment" >&2
+ exit 1
+fi
+
+mkdir -p "$RESULTS_ROOT"
+RUN_ID="$("$DATE_BIN" +%Y%m%d_%H%M%S)"
+RUN_ROOT="$RESULTS_ROOT/run-$RUN_ID"
+WORK_ROOT="${WORK_ROOT:-$RUN_ROOT/work}"
+RAW_CSV="$RUN_ROOT/raw.csv"
+COUNTERS_CSV="$RUN_ROOT/counters.csv"
+PHASE_CSV="$RUN_ROOT/phase.csv"
+SUMMARY_MD="$RUN_ROOT/summary.md"
+mkdir -p "$RUN_ROOT" "$WORK_ROOT"
+
+cleanup() {
+ if [[ "$KEEP_WORK" != "1" ]]; then
+ rm -rf "$WORK_ROOT"
+ fi
+}
+trap cleanup EXIT
+
+echo "Building par2rs release binary..."
+cargo build --manifest-path "$PROJECT_ROOT/Cargo.toml" --release --bin par2 --quiet
+
+printf 'case,tool,iteration,kind,wall_seconds,exit_status,perf_log,profile_log,io_log,work_dir\n' >"$RAW_CSV"
+printf 'case,tool,iteration,wall_seconds,instructions,cache_misses,branches,branch_misses,task_clock_ms,context_switches\n' >"$COUNTERS_CSV"
+printf 'case,tool,iteration,section,name,value\n' >"$PHASE_CSV"
+
+make_corpus() {
+ local corpus_dir="$1"
+ local file_count="$2"
+ local file_size="$3"
+ rm -rf "$corpus_dir"
+ mkdir -p "$corpus_dir"
+ perl - "$corpus_dir" "$file_count" "$file_size" <<'PL'
+use strict;
+use warnings;
+use bytes;
+
+my ($root, $file_count, $file_size) = @ARGV;
+my $chunk_size = 1024 * 1024;
+
+for my $file_idx (0 .. $file_count - 1) {
+ my $path = sprintf "%s/file_%05d.bin", $root, $file_idx;
+ open my $fh, ">:raw", $path or die "open $path: $!";
+ my $remaining = $file_size;
+ my $offset = 0;
+ while ($remaining > 0) {
+ my $n = $remaining < $chunk_size ? $remaining : $chunk_size;
+ my $data = pack "C*", map { ($file_idx * 131 + $offset + $_) & 0xFF } 0 .. $n - 1;
+ print {$fh} $data or die "write $path: $!";
+ $remaining -= $n;
+ $offset += $n;
+ }
+ close $fh or die "close $path: $!";
+}
+PL
+}
+
+source_files_for() {
+ local corpus_dir="$1"
+ find "$corpus_dir" -type f -name '*.bin' -print | sort
+}
+
+run_create() {
+ local case_label="$1"
+ local tool="$2"
+ local iteration="$3"
+ local kind="$4"
+ local output_dir="$5"
+ local corpus_dir="$6"
+ local block_size="$7"
+ local redundancy="$8"
+ local memory_mb="$9"
+
+ rm -rf "$output_dir"
+ mkdir -p "$output_dir"
+ local output_base="$output_dir/out.par2"
+ local perf_log="$output_dir/perf.log"
+ local profile_log="$output_dir/profile.log"
+ local io_log="$output_dir/io.log"
+ local -a sources
+ mapfile -t sources < <(source_files_for "$corpus_dir")
+
+ local -a cmd env_prefix
+ env_prefix=()
+ case "$tool" in
+ par2rs)
+ env_prefix=(env PAR2RS_CREATE_PROFILE=1)
+ cmd=("$PAR2RS_BIN" c -q "-s$block_size" "-r$redundancy" "-n$RECOVERY_FILES")
+ ;;
+ turbo-parpar)
+ cmd=("$PAR2CMD_BIN" c -q "-s$block_size" "-r$redundancy" "-n$RECOVERY_FILES")
+ ;;
+ parpar)
+ if [[ -z "$PARPAR_BIN" ]]; then
+ return 0
+ fi
+ cmd=("$PARPAR_BIN" c -q "-s$block_size" "-r$redundancy" "-n$RECOVERY_FILES")
+ ;;
+ *)
+ echo "error: unknown tool '$tool'" >&2
+ exit 1
+ ;;
+ esac
+ if [[ "$THREADS" != "0" ]]; then
+ cmd+=("-t$THREADS")
+ fi
+ if [[ "$memory_mb" != "0" ]]; then
+ cmd+=("-m$memory_mb")
+ fi
+ cmd+=("$output_base")
+ cmd+=("${sources[@]}")
+
+ local start_ns end_ns status wall
+ start_ns="$("$DATE_BIN" +%s%N)"
+ set +e
+ "${env_prefix[@]}" perf stat -x, -e "$PERF_EVENTS" -o "$perf_log" -- "${cmd[@]}" >"$output_dir/stdout.log" 2>"$profile_log"
+ status=$?
+ set -e
+ end_ns="$("$DATE_BIN" +%s%N)"
+ wall="$(perl -e 'printf "%.9f\n", ($ARGV[1] - $ARGV[0]) / 1_000_000_000' "$start_ns" "$end_ns")"
+
+ if [[ "$TRACE_IO" == "1" && "$kind" == "measured" ]] && command -v strace >/dev/null 2>&1; then
+ rm -rf "$output_dir/io-sample"
+ mkdir -p "$output_dir/io-sample"
+ local io_output="$output_dir/io-sample/out.par2"
+ local -a io_cmd=("${cmd[@]}")
+ local output_idx=$(( ${#io_cmd[@]} - ${#sources[@]} - 1 ))
+ io_cmd[$output_idx]="$io_output"
+ strace -qq -c -e trace=read,pread64,lseek,write,pwrite64 -o "$io_log" "${env_prefix[@]}" "${io_cmd[@]}" >/dev/null 2>&1 || true
+ fi
+
+ printf '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n' \
+ "$case_label" "$tool" "$iteration" "$kind" "$wall" "$status" \
+ "$perf_log" "$profile_log" "$io_log" "$output_dir" >>"$RAW_CSV"
+
+ if [[ "$kind" == "measured" ]]; then
+ perl - "$case_label" "$tool" "$iteration" "$wall" "$perf_log" "$profile_log" "$COUNTERS_CSV" "$PHASE_CSV" <<'PL'
+use strict;
+use warnings;
+
+my ($case_label, $tool, $iteration, $wall, $perf_log, $profile_log, $counters_csv, $phase_csv) = @ARGV;
+my %events = map { $_ => "" } qw(instructions cache-misses branches branch-misses task-clock context-switches);
+
+if (open my $perf, "<", $perf_log) {
+ while (my $line = <$perf>) {
+ chomp $line;
+ my @row = split /,/, $line;
+ next unless @row >= 3;
+ if (exists $events{$row[2]}) {
+ $row[0] =~ s/,//g;
+ $events{$row[2]} = $row[0];
+ }
+ }
+ close $perf;
+}
+
+open my $counters, ">>", $counters_csv or die "open $counters_csv: $!";
+print {$counters} join(",", $case_label, $tool, $iteration, $wall,
+ @events{qw(instructions cache-misses branches branch-misses task-clock context-switches)}
+) . "\n";
+close $counters;
+
+if (open my $profile, "<", $profile_log) {
+ open my $phase, ">>", $phase_csv or die "open $phase_csv: $!";
+ my $in_profile = 0;
+ my $section = "";
+ while (my $line = <$profile>) {
+ chomp $line;
+ if ($line eq "PAR2RS_CREATE_PROFILE_BEGIN") {
+ $in_profile = 1;
+ next;
+ }
+ last if $line eq "PAR2RS_CREATE_PROFILE_END";
+ next unless $in_profile;
+ if ($line eq "phase,seconds") {
+ $section = "phase";
+ next;
+ }
+ if ($line eq "counter,value") {
+ $section = "counter";
+ next;
+ }
+ next unless index($line, ",") >= 0;
+ my ($name, $value) = split /,/, $line, 2;
+ print {$phase} join(",", $case_label, $tool, $iteration, $section, $name, $value) . "\n";
+ }
+ close $phase;
+ close $profile;
+}
+PL
+ fi
+}
+
+IFS=',' read -r -a workload_specs <<<"$WORKLOADS"
+tools=(par2rs turbo-parpar)
+if [[ -n "$PARPAR_BIN" ]]; then
+ tools+=(parpar)
+fi
+
+for spec in "${workload_specs[@]}"; do
+ IFS=':' read -r label file_count file_size block_size redundancy memory_mb <<<"$spec"
+ corpus_dir="$WORK_ROOT/$label/corpus"
+ echo "Preparing workload $label..."
+ make_corpus "$corpus_dir" "$file_count" "$file_size"
+
+ for tool in "${tools[@]}"; do
+ for ((i = 1; i <= WARMUP_RUNS; i++)); do
+ run_create "$label" "$tool" "$i" "warmup" "$WORK_ROOT/$label/$tool/warmup-$i" "$corpus_dir" "$block_size" "$redundancy" "$memory_mb"
+ done
+ for ((i = 1; i <= ITERATIONS; i++)); do
+ echo "Running $label / $tool / iteration $i..."
+ run_create "$label" "$tool" "$i" "measured" "$WORK_ROOT/$label/$tool/run-$i" "$corpus_dir" "$block_size" "$redundancy" "$memory_mb"
+ done
+ done
+done
+
+perl - "$COUNTERS_CSV" "$PHASE_CSV" "$SUMMARY_MD" <<'PL'
+use strict;
+use warnings;
+
+my ($counters_csv, $phase_csv, $summary_md) = @ARGV;
+my %wall;
+my %phase;
+
+sub median {
+ my @values = sort { $a <=> $b } @_;
+ return 0 unless @values;
+ my $mid = int(@values / 2);
+ return @values % 2 ? $values[$mid] : ($values[$mid - 1] + $values[$mid]) / 2;
+}
+
+if (open my $counters, "<", $counters_csv) {
+ my $header = <$counters>;
+ while (my $line = <$counters>) {
+ chomp $line;
+ my @row = split /,/, $line;
+ next unless @row >= 4 && $row[3] ne "";
+ push @{ $wall{"$row[0]\0$row[1]"} }, $row[3] + 0;
+ }
+ close $counters;
+}
+
+if (open my $ph, "<", $phase_csv) {
+ my $header = <$ph>;
+ while (my $line = <$ph>) {
+ chomp $line;
+ my @row = split /,/, $line;
+ next unless @row >= 6;
+ next unless $row[3] eq "phase" && $row[1] eq "par2rs";
+ push @{ $phase{"$row[0]\0$row[4]"} }, $row[5] + 0;
+ }
+ close $ph;
+}
+
+open my $out, ">", $summary_md or die "open $summary_md: $!";
+print {$out} "# par2rs create slow-path profile\n\n";
+print {$out} "## Median wall time\n\n";
+print {$out} "| workload | tool | median seconds |\n";
+print {$out} "| --- | ---: | ---: |\n";
+for my $key (sort keys %wall) {
+ my ($case, $tool) = split /\0/, $key;
+ printf {$out} "| %s | %s | %.6f |\n", $case, $tool, median(@{ $wall{$key} });
+}
+print {$out} "\n## par2rs median phase time\n\n";
+print {$out} "| workload | phase | median seconds |\n";
+print {$out} "| --- | ---: | ---: |\n";
+for my $key (sort keys %phase) {
+ my ($case, $name) = split /\0/, $key;
+ printf {$out} "| %s | %s | %.6f |\n", $case, $name, median(@{ $phase{$key} });
+}
+close $out;
+PL
+
+if [[ "$RUN_KERNEL_BENCH" == "1" ]]; then
+ cargo bench --manifest-path "$PROJECT_ROOT/Cargo.toml" --features parpar-compare --bench compare_with_parpar -- --save-baseline parpar-create-slow-paths
+fi
+
+echo "Results written to $RUN_ROOT"
+echo "Summary: $SUMMARY_MD"
diff --git a/scripts/turbo_dump_xor_finish_packed_avx2.c b/scripts/turbo_dump_xor_finish_packed_avx2.c
new file mode 100644
index 00000000..7e4eb184
--- /dev/null
+++ b/scripts/turbo_dump_xor_finish_packed_avx2.c
@@ -0,0 +1,50 @@
+#include
+#include
+#include
+#include
+
+#define PARPAR_INCLUDE_BASIC_OPS
+#include "../../par2cmdline-turbo/parpar/gf16/gf16_xor_avx2.c"
+
+static void fill_pattern(uint8_t* dst, size_t len) {
+ for(size_t i = 0; i < len; i++) {
+ dst[i] = (uint8_t)((i * 29u + 7u) & 0xffu);
+ }
+}
+
+int main(int argc, char** argv) {
+ const char* output_path = argc > 1 ? argv[1] : NULL;
+ size_t slice_len = argc > 2 ? strtoull(argv[2], NULL, 0) : 1024 * 1024;
+ size_t chunk_len = argc > 3 ? strtoull(argv[3], NULL, 0) : 128 * 1024;
+ unsigned num_outputs = argc > 4 ? (unsigned)strtoul(argv[4], NULL, 0) : 7;
+ unsigned output_num = argc > 5 ? (unsigned)strtoul(argv[5], NULL, 0) : 3;
+
+ size_t segment_count = (slice_len + chunk_len - 1) / chunk_len;
+ size_t prepared_len = segment_count * num_outputs * chunk_len;
+ uint8_t* prepared = (uint8_t*)calloc(prepared_len, 1);
+ uint8_t* input = (uint8_t*)malloc(slice_len);
+ uint8_t* output = (uint8_t*)malloc(slice_len);
+ if(!prepared || !input || !output) {
+ fprintf(stderr, "allocation failed\n");
+ return 1;
+ }
+
+ fill_pattern(input, slice_len);
+ gf16_xor_prepare_packed_avx2(prepared, input, slice_len, slice_len, num_outputs, output_num, chunk_len);
+
+ gf16_xor_finish_packed_avx2(output, prepared, slice_len, num_outputs, output_num, chunk_len);
+
+ FILE* fp = output_path ? fopen(output_path, "wb") : stdout;
+ if(!fp) {
+ perror("fopen");
+ return 1;
+ }
+ fwrite(output, 1, slice_len, fp);
+ if(output_path)
+ fclose(fp);
+
+ free(output);
+ free(input);
+ free(prepared);
+ return 0;
+}
diff --git a/scripts/turbo_dump_xor_prepare_packed_avx2.c b/scripts/turbo_dump_xor_prepare_packed_avx2.c
new file mode 100644
index 00000000..95b82ff3
--- /dev/null
+++ b/scripts/turbo_dump_xor_prepare_packed_avx2.c
@@ -0,0 +1,48 @@
+#include
+#include
+#include
+#include
+
+#define PARPAR_INCLUDE_BASIC_OPS
+#include "../../par2cmdline-turbo/parpar/gf16/gf16_xor_avx2.c"
+
+static void fill_pattern(uint8_t* dst, size_t len) {
+ for(size_t i = 0; i < len; i++) {
+ dst[i] = (uint8_t)((i * 37u + 11u) & 0xffu);
+ }
+}
+
+int main(int argc, char** argv) {
+ const char* output_path = argc > 1 ? argv[1] : NULL;
+ size_t src_len = argc > 2 ? strtoull(argv[2], NULL, 0) : 1024 * 1024;
+ size_t slice_len = argc > 3 ? strtoull(argv[3], NULL, 0) : 1024 * 1024;
+ size_t chunk_len = argc > 4 ? strtoull(argv[4], NULL, 0) : 128 * 1024;
+ unsigned input_pack_size = argc > 5 ? (unsigned)strtoul(argv[5], NULL, 0) : 12;
+ unsigned input_num = argc > 6 ? (unsigned)strtoul(argv[6], NULL, 0) : 5;
+
+ size_t segment_count = (slice_len + chunk_len - 1) / chunk_len;
+ size_t dst_len = segment_count * input_pack_size * chunk_len;
+
+ uint8_t* src = (uint8_t*)malloc(src_len);
+ uint8_t* dst = (uint8_t*)calloc(dst_len, 1);
+ if(!src || !dst) {
+ fprintf(stderr, "allocation failed\n");
+ return 1;
+ }
+
+ fill_pattern(src, src_len);
+ gf16_xor_prepare_packed_avx2(dst, src, src_len, slice_len, input_pack_size, input_num, chunk_len);
+
+ FILE* fp = output_path ? fopen(output_path, "wb") : stdout;
+ if(!fp) {
+ perror("fopen");
+ return 1;
+ }
+ fwrite(dst, 1, dst_len, fp);
+ if(output_path)
+ fclose(fp);
+
+ free(dst);
+ free(src);
+ return 0;
+}
diff --git a/scripts/turbo_dump_xorjit_body_avx2.c b/scripts/turbo_dump_xorjit_body_avx2.c
new file mode 100644
index 00000000..88ac235e
--- /dev/null
+++ b/scripts/turbo_dump_xorjit_body_avx2.c
@@ -0,0 +1,51 @@
+#include
+#include
+#include
+
+#include "../../par2cmdline-turbo/parpar/gf16/gf16_xor_avx2.c"
+
+int main(int argc, char** argv) {
+ uint16_t coefficient = (uint16_t)strtoul(argc > 1 ? argv[1] : "0xc814", NULL, 0);
+ int prefetch = argc > 2 ? atoi(argv[2]) : 0;
+ const char* output_path = argc > 3 ? argv[3] : NULL;
+
+ struct gf16_xor_scratch* scratch =
+ (struct gf16_xor_scratch*)gf16_xor_jit_init_avx2(0x1100b, GF16_XOR_JIT_STRAT_NONE);
+ jit_wx_pair* jit = (jit_wx_pair*)gf16_xor_jit_init_mut_avx2();
+ if(!scratch || !jit) {
+ fprintf(stderr, "failed to initialize turbo xor-jit scratch\n");
+ return 1;
+ }
+
+ uint8_t* body_start = (uint8_t*)jit->w + scratch->codeStart;
+ uint8_t* end =
+ xor_write_jit_avx(scratch, body_start, coefficient, XORDEP_JIT_MODE_MULADD, prefetch ? _MM_HINT_T1 : 0);
+ write32(end, (int32_t)((uint8_t*)jit->w - end - 4));
+ end[4] = 0xC3;
+ end += 5;
+
+ size_t total_len = (size_t)(end - (uint8_t*)jit->w);
+ size_t dynamic_len = (size_t)(end - body_start);
+ fprintf(
+ stderr,
+ "turbo xor-jit coeff=%#06x prefetch=%d code_start=%u total_len=%zu dynamic_len=%zu\n",
+ coefficient,
+ prefetch,
+ (unsigned)scratch->codeStart,
+ total_len,
+ dynamic_len
+ );
+
+ FILE* fp = output_path ? fopen(output_path, "wb") : stdout;
+ if(!fp) {
+ perror("fopen");
+ return 1;
+ }
+ fwrite(jit->w, 1, total_len, fp);
+ if(output_path)
+ fclose(fp);
+
+ jit_free(jit);
+ ALIGN_FREE(scratch);
+ return 0;
+}
diff --git a/src/checksum.rs b/src/checksum.rs
index 422cecb9..b91c3867 100644
--- a/src/checksum.rs
+++ b/src/checksum.rs
@@ -82,15 +82,15 @@ pub fn compute_crc32_padded(data: &[u8], block_size: usize) -> Crc32Value {
// Combined MD5 + CRC32 (Performance Optimization)
// ============================================================================
-/// Compute both MD5 hash and CRC32 checksum in a single pass
+/// Compute both MD5 hash and CRC32 checksum with cache-local chunking.
///
/// This is more efficient than calling `compute_md5()` and `compute_crc32()`
-/// separately, as it only reads the data once and processes it in parallel.
+/// separately, as each chunk is fed to both hashers while still hot in cache.
///
/// PAR2 frequently needs both checksums for block verification (CRC32 for
/// fast pre-screening, MD5 for cryptographic verification).
///
-/// Uses simultaneous computation for optimal performance (40-60% faster than separate calls).
+/// Uses paired chunk updates for better cache locality than separate calls.
#[inline]
pub fn compute_block_checksums(data: &[u8]) -> (Md5Hash, Crc32Value) {
compute_md5_crc32_simultaneous(data)
@@ -107,35 +107,88 @@ pub fn compute_block_checksums_padded(data: &[u8], block_size: usize) -> (Md5Has
compute_md5_crc32_simultaneous_padded(data, block_size)
}
-/// Compute MD5 and CRC32 simultaneously in a single pass (par2cmdline style)
+/// Sub-slice size used by the fused MD5+CRC32 stream updater.
///
-/// This is the most efficient way to compute both checksums as it:
-/// - Reads data only once (50% less memory bandwidth)
-/// - Processes data while still in CPU cache
-/// - Updates both hash states in the same loop
+/// Sized to fit comfortably in L1d (typical 32-48 KiB) while remaining large
+/// enough to amortize the per-slice call overhead of the underlying SIMD
+/// compress routines in `md-5` and `crc32fast`.
+const FUSED_HASH_CHUNK: usize = 16 * 1024;
+
+/// Update an MD5 hasher and a CRC32 hasher over the same buffer with
+/// cache-resident sub-slices.
///
-/// Based on par2cmdline-turbo's MD5CRC_Calc implementation which showed
-/// ~40-60% performance improvement over separate computation.
+/// Both `md-5` and `crc32fast` use vectorized compress loops internally, so we
+/// can't literally interleave their inner instructions from Rust. What we *can*
+/// do is feed each cache-line-sized region to both hashers back-to-back, so the
+/// CRC pass reads bytes that the MD5 pass just pulled into L1 (or vice versa).
+/// This avoids the worst case where a large `data` buffer is scanned end-to-end
+/// by MD5 (evicting itself from L1), then scanned end-to-end again by CRC32 and
+/// re-fetched from L2/L3.
///
-/// # Performance
+/// Mirrors the design intent of par2cmdline-turbo's `HasherInput::update`,
+/// which fuses MD5 and CRC32 updates per 64-byte block via hand-tuned ASM.
+/// Without 2-lane MD5 we can't match the ILP, but we can still kill the
+/// double-scan memory traffic.
+#[inline]
+pub fn update_md5_crc32_fused(
+ md5_hasher: &mut D,
+ crc_hasher: &mut crc32fast::Hasher,
+ data: &[u8],
+) {
+ for chunk in data.chunks(FUSED_HASH_CHUNK) {
+ md5_hasher.update(chunk);
+ crc_hasher.update(chunk);
+ }
+}
+
+/// Update three hashers (file-MD5, block-MD5, block-CRC32) over the same
+/// buffer with cache-resident sub-slices.
+///
+/// Used by the create path's encode_and_hash_files where each chunk is
+/// simultaneously contributing to a per-file rolling MD5 and the current
+/// block's MD5+CRC32. Without a 2-lane MD5 (md5x2) we still pay two MD5
+/// compress passes, but we keep the data hot between all three hash updates.
+///
+/// Reference: par2cmdline-turbo/parpar/hasher/hasher_input_base.h
+/// (HasherInput::update fuses block-MD5 lane, file-MD5 lane and CRC32 per 64B)
+#[inline]
+pub fn update_file_md5_block_md5_crc32_fused(
+ file_md5_hasher: &mut D,
+ block_md5_hasher: &mut D,
+ crc_hasher: &mut crc32fast::Hasher,
+ data: &[u8],
+) {
+ for chunk in data.chunks(FUSED_HASH_CHUNK) {
+ block_md5_hasher.update(chunk);
+ crc_hasher.update(chunk);
+ file_md5_hasher.update(chunk);
+ }
+}
+
+/// Compute MD5 and CRC32 from one source-buffer traversal using cache-local chunking.
+///
+/// This helper operates on an in-memory slice, not directly on a file handle.
+/// The create path can still use it as part of a single-read file pipeline, but
+/// at this layer we are simply feeding two independent hashers from the same
+/// chunk stream while the data is still hot in cache.
///
-/// For 1MB blocks:
-/// - Separate: 2 passes through data, ~poor cache reuse
-/// - Simultaneous: 1 pass through data, excellent cache reuse
+/// Based on par2cmdline-turbo's MD5CRC_Calc-style chunking approach.
///
-/// Expected speedup: 1.5-2x for block verification workloads
+/// # Performance
+///
+/// For large blocks:
+/// - Separate whole-buffer passes: weaker cache locality between hashers
+/// - Chunked paired updates: better cache reuse across both hash states
#[inline]
pub fn compute_md5_crc32_simultaneous(data: &[u8]) -> (Md5Hash, Crc32Value) {
use crc32fast::Hasher as Crc32Hasher;
- use md5::Digest;
let mut md5_hasher = Md5::new();
let mut crc_hasher = Crc32Hasher::new();
- // Process data in a single pass, updating both hash states
- // This keeps data hot in CPU cache for both operations
- md5_hasher.update(data);
- crc_hasher.update(data);
+ // Process data in cache-resident sub-slices so neither hasher evicts
+ // the other's working set from L1.
+ update_md5_crc32_fused(&mut md5_hasher, &mut crc_hasher, data);
(
Md5Hash::new(md5_hasher.finalize().into()),
@@ -143,13 +196,14 @@ pub fn compute_md5_crc32_simultaneous(data: &[u8]) -> (Md5Hash, Crc32Value) {
)
}
-/// Compute MD5 and CRC32 simultaneously with zero-padding (par2cmdline style)
+/// Compute MD5 and CRC32 over one padded buffer using the same chunked updates.
///
/// For partial blocks at the end of files, this computes both checksums
-/// with zero-padding to the target block size, all in a single pass.
+/// with zero-padding to the target block size from one temporary buffer.
///
/// This is significantly more efficient than the padded separate version
-/// because it only allocates and processes the padded buffer once.
+/// because it only allocates the padded buffer once and then reuses cache-local
+/// chunking for both hashers.
///
/// # Arguments
///
@@ -159,17 +213,14 @@ pub fn compute_md5_crc32_simultaneous(data: &[u8]) -> (Md5Hash, Crc32Value) {
/// # Performance
///
/// For partial blocks:
-/// - Old: allocate padding, compute MD5, compute CRC32 (3 operations)
-/// - New: allocate padding, compute both simultaneously (2 operations)
-///
-/// Expected speedup: 1.3-1.5x for partial block processing
+/// - Separate padded hashing: allocate once, then run distinct whole-buffer hashes
+/// - Chunked paired updates: allocate once, then update both hashers per chunk
#[inline]
pub fn compute_md5_crc32_simultaneous_padded(
data: &[u8],
target_size: usize,
) -> (Md5Hash, Crc32Value) {
use crc32fast::Hasher as Crc32Hasher;
- use md5::Digest;
if data.len() >= target_size {
// No padding needed, use direct simultaneous computation
@@ -183,9 +234,8 @@ pub fn compute_md5_crc32_simultaneous_padded(
let mut md5_hasher = Md5::new();
let mut crc_hasher = Crc32Hasher::new();
- // Single pass through padded data
- md5_hasher.update(&padded);
- crc_hasher.update(&padded);
+ // Cache-resident sub-slice walk over the padded buffer.
+ update_md5_crc32_fused(&mut md5_hasher, &mut crc_hasher, &padded);
(
Md5Hash::new(md5_hasher.finalize().into()),
diff --git a/src/create/backend.rs b/src/create/backend.rs
index 7a266f9b..04e9f977 100644
--- a/src/create/backend.rs
+++ b/src/create/backend.rs
@@ -5,56 +5,428 @@ use crate::reed_solomon::codec::{
};
use crate::reed_solomon::galois::Galois16;
use crate::reed_solomon::AlignedVec;
-use rayon::prelude::*;
+use std::sync::{
+ atomic::{AtomicUsize, Ordering},
+ Arc, Condvar, Mutex,
+};
+use std::thread::JoinHandle;
#[cfg(target_arch = "x86_64")]
use crate::reed_solomon::simd::{
- detect_simd_support, prepare_avx2_coeff, process_slice_multiply_add_prepared_avx2,
- process_slices_multiply_add_prepared_avx2_x2, Avx2PreparedCoeff, SimdLevel,
+ detect_simd_support, finish_xor_jit_bitplane_packed_output_cksum, prepare_avx2_coeff,
+ prepare_xor_jit_bitplane_packed_input_cksum, process_slice_multiply_add_prepared_avx2,
+ process_slice_multiply_add_xor_jit, process_slices_multiply_add_prepared_avx2_x2,
+ process_slices_multiply_add_prepared_avx2_x4, process_slices_multiply_add_xor_jit_x2,
+ process_slices_multiply_add_xor_jit_x4,
+ process_slices_multiply_add_xor_jit_x4_inputs_x2_outputs,
+ process_slices_multiply_add_xor_jit_x4_inputs_x4_outputs, xor_jit_create_avx2_method_info,
+ xor_jit_create_prefetch_plan, xor_packed_multi_region_v16i1_ptr, Avx2PreparedCoeff, SimdLevel,
+ XorJitBitplaneScratch, XorJitCreateMethodInfo, XorJitFlavor, XorJitPreparedCoeff,
+ XorJitPreparedCoeffCache,
};
-const DEFAULT_INPUT_BATCH_SIZE: usize = 12;
+const DEFAULT_INPUT_GROUPING: usize = 12;
const TRANSFER_BUFFER_COUNT: usize = 2;
+const CREATE_SEGMENT_SIZE: usize = 256 * 1024;
+// The prepared x1 PSHUFB path currently retires fewer instructions on the
+// large-file create proxy than the x2/x4 packed kernels on this CPU.
+const PSHUFB_PACKED_INPUTS: usize = 1;
+const AVX2_ALIGNMENT: usize = 32;
+#[cfg(target_arch = "x86_64")]
+const XOR_JIT_SEGMENT_LEN_ENV: &str = "PAR2RS_CREATE_XOR_JIT_SEGMENT_BYTES";
+#[cfg(target_arch = "x86_64")]
+const XOR_JIT_PREFETCH_MAX_FACTOR: usize = 3;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum CreateGf16Method {
+ Auto,
+ Avx2PshufbPrepared,
+ #[cfg(target_arch = "x86_64")]
+ Avx2XorJit,
+ Scalar,
+}
+
+impl CreateGf16Method {
+ fn from_env() -> Self {
+ match std::env::var("PAR2RS_CREATE_GF16") {
+ Ok(value) => match value.to_ascii_lowercase().as_str() {
+ "auto" => Self::Auto,
+ "pshufb" | "avx2-pshufb" | "avx2_pshufb" => Self::Avx2PshufbPrepared,
+ "xor-jit" | "xor_jit" | "xorit" | "avx2-xor-jit" | "xor-jit-port"
+ | "xor_jit_port" | "avx2-xor-jit-port" => {
+ #[cfg(target_arch = "x86_64")]
+ {
+ return Self::Avx2XorJit;
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ Self::Auto
+ }
+ "scalar" => Self::Scalar,
+ _ => Self::Auto,
+ },
+ Err(_) => Self::Auto,
+ }
+ }
+
+ fn resolve(self) -> Self {
+ match self {
+ Self::Auto => {
+ #[cfg(target_arch = "x86_64")]
+ {
+ if matches!(detect_simd_support(), SimdLevel::Avx2) {
+ return Self::Avx2PshufbPrepared;
+ }
+ }
+ Self::Scalar
+ }
+ Self::Avx2PshufbPrepared => {
+ #[cfg(target_arch = "x86_64")]
+ {
+ if matches!(detect_simd_support(), SimdLevel::Avx2) {
+ return Self::Avx2PshufbPrepared;
+ }
+ }
+ Self::Scalar
+ }
+ #[cfg(target_arch = "x86_64")]
+ Self::Avx2XorJit => {
+ if matches!(detect_simd_support(), SimdLevel::Avx2)
+ && is_x86_feature_detected!("vpclmulqdq")
+ {
+ return self;
+ }
+ panic!("XOR-JIT create backend requires x86_64 AVX2 and VPCLMULQDQ support");
+ }
+ Self::Scalar => Self::Scalar,
+ }
+ }
+
+ #[inline]
+ fn ideal_input_multiple(self) -> usize {
+ #[cfg(target_arch = "x86_64")]
+ if let Some(info) = self.xor_jit_create_method_info() {
+ return info.ideal_input_multiple;
+ }
+
+ match self {
+ Self::Auto | Self::Avx2PshufbPrepared => 4,
+ Self::Scalar => 1,
+ #[cfg(target_arch = "x86_64")]
+ Self::Avx2XorJit => unreachable!("handled by xor_jit_create_method_info"),
+ }
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[inline]
+ fn xor_jit_create_method_info(self) -> Option {
+ match self {
+ Self::Avx2XorJit => Some(xor_jit_create_avx2_method_info()),
+ _ => None,
+ }
+ }
+
+ #[inline]
+ fn ideal_segment_len(self) -> usize {
+ match self {
+ Self::Auto | Self::Avx2PshufbPrepared => CREATE_SEGMENT_SIZE,
+ #[cfg(target_arch = "x86_64")]
+ Self::Avx2XorJit => xor_jit_segment_len_override().unwrap_or(128 * 1024),
+ Self::Scalar => CREATE_SEGMENT_SIZE / 2,
+ }
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[inline]
+ fn xor_jit_flavor(self) -> Option {
+ match self {
+ Self::Avx2XorJit => Some(XorJitFlavor::Jit),
+ _ => None,
+ }
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn xor_jit_segment_len_override() -> Option {
+ match std::env::var(XOR_JIT_SEGMENT_LEN_ENV) {
+ Ok(value) => {
+ let parsed = value.parse::().unwrap_or_else(|_| {
+ panic!(
+ "{XOR_JIT_SEGMENT_LEN_ENV} must be a positive integer byte count, got {value:?}"
+ )
+ });
+ assert!(
+ parsed > 0,
+ "{XOR_JIT_SEGMENT_LEN_ENV} must be greater than zero"
+ );
+ Some(parsed)
+ }
+ Err(std::env::VarError::NotPresent) => None,
+ Err(std::env::VarError::NotUnicode(_)) => {
+ panic!("{XOR_JIT_SEGMENT_LEN_ENV} must be valid UTF-8")
+ }
+ }
+}
+
+#[cfg(not(target_arch = "x86_64"))]
+fn xor_jit_segment_len_override() -> Option {
+ None
+}
/// Prepared coefficient for one `(recovery output, source input)` pair.
-pub struct Gf16Coeff {
+pub struct CreateCoeff {
pub value: u16,
pub split: SplitMulTable,
#[cfg(target_arch = "x86_64")]
pub avx2: Option,
+ #[cfg(target_arch = "x86_64")]
+ pub xor_jit: Option,
+ #[cfg(target_arch = "x86_64")]
+ pub xor_jit_bitplane: Option,
}
-impl Gf16Coeff {
+pub type Gf16Coeff = CreateCoeff;
+
+impl CreateCoeff {
#[inline]
- fn new(value: u16) -> Self {
+ #[cfg(target_arch = "x86_64")]
+ fn new(
+ value: u16,
+ prepare_pshufb: bool,
+ prepare_bitplane: bool,
+ xor_jit_cache: &mut XorJitPreparedCoeffCache,
+ ) -> Self {
let split = build_split_mul_table(Galois16::new(value));
- #[cfg(target_arch = "x86_64")]
- let avx2 = Some(prepare_avx2_coeff(&split));
+ let avx2 = prepare_pshufb.then(|| prepare_avx2_coeff(&split));
+ let xor_jit = prepare_bitplane.then(|| {
+ let prepared = xor_jit_cache.prepare(value);
+ prepared.ensure_bitplane_emitted();
+ xor_jit_cache.cache_bitplane_handle(value, prepared.bitplane_handle());
+ prepared
+ });
+ let xor_jit_bitplane = xor_jit.clone();
Self {
value,
split,
- #[cfg(target_arch = "x86_64")]
avx2,
+ xor_jit,
+ xor_jit_bitplane,
+ }
+ }
+
+ #[cfg(not(target_arch = "x86_64"))]
+ fn new(value: u16, _prepare_pshufb: bool) -> Self {
+ Self {
+ value,
+ split: build_split_mul_table(Galois16::new(value)),
+ }
+ }
+}
+
+pub struct StagingArea {
+ inputs: AlignedVec,
+ source_indices: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_coeffs: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_out_non_zero: Vec,
+ #[cfg(target_arch = "x86_64")]
+ proc_refs: AtomicUsize,
+ batch_len: usize,
+}
+
+impl StagingArea {
+ #[cfg(target_arch = "x86_64")]
+ fn new(input_grouping: usize, input_storage_len: usize, recovery_count: usize) -> Self {
+ Self {
+ inputs: AlignedVec::new_zeroed(input_storage_len),
+ source_indices: vec![0; input_grouping],
+ xor_jit_coeffs: vec![0; input_grouping * recovery_count],
+ xor_jit_out_non_zero: vec![0; recovery_count],
+ proc_refs: AtomicUsize::new(0),
+ batch_len: 0,
+ }
+ }
+
+ #[cfg(not(target_arch = "x86_64"))]
+ fn new(input_grouping: usize, input_storage_len: usize) -> Self {
+ Self {
+ inputs: AlignedVec::new_zeroed(input_storage_len),
+ source_indices: vec![0; input_grouping],
+ batch_len: 0,
+ }
+ }
+
+ #[inline]
+ fn slot_mut(&mut self, slot: usize, aligned_chunk_len: usize, chunk_len: usize) -> &mut [u8] {
+ let start = slot * aligned_chunk_len;
+ let end = start + chunk_len;
+ &mut self.inputs[start..end]
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+struct XorJitBitplaneLayout {
+ aligned_slice_len: usize,
+ stride: usize,
+ chunk_len: usize,
+ input_batch_size: usize,
+ recovery_count: usize,
+}
+
+#[cfg(target_arch = "x86_64")]
+impl XorJitBitplaneLayout {
+ fn new(
+ max_slice_len: usize,
+ chunk_len: usize,
+ input_batch_size: usize,
+ recovery_count: usize,
+ method_info: XorJitCreateMethodInfo,
+ ) -> Self {
+ let stride = method_info.stride;
+ let aligned_slice_len = align_up(max_slice_len, stride) + stride;
+ debug_assert!(chunk_len > 0);
+ debug_assert!(chunk_len.is_multiple_of(method_info.stride));
+ debug_assert!(aligned_slice_len.is_multiple_of(method_info.stride));
+ Self {
+ aligned_slice_len,
+ stride,
+ chunk_len,
+ input_batch_size,
+ recovery_count,
}
}
+
+ #[inline]
+ fn align_to_stride(&self, len: usize) -> usize {
+ align_up(len, self.stride)
+ }
+
+ #[inline]
+ fn aligned_current_slice_size(&self, slice_len: usize) -> usize {
+ self.align_to_stride(slice_len) + self.stride
+ }
+
+ #[inline]
+ fn segment_count_for(&self, aligned_current_slice_size: usize) -> usize {
+ aligned_current_slice_size.div_ceil(self.chunk_len)
+ }
+
+ #[inline]
+ #[cfg_attr(not(test), allow(dead_code))]
+ fn input_offset(&self, slice_offset: usize, batch_idx: usize, proc_size: usize) -> usize {
+ debug_assert!(batch_idx < self.input_batch_size);
+ let offset = slice_offset * self.input_batch_size + batch_idx * proc_size;
+ debug_assert!(offset.is_multiple_of(self.stride));
+ offset
+ }
+
+ #[inline]
+ #[cfg_attr(not(test), allow(dead_code))]
+ fn output_offset(&self, slice_offset: usize, recovery_idx: usize, proc_size: usize) -> usize {
+ debug_assert!(recovery_idx < self.recovery_count);
+ let offset = slice_offset * self.recovery_count + recovery_idx * proc_size;
+ debug_assert!(offset.is_multiple_of(self.stride));
+ offset
+ }
+
+ #[inline]
+ #[cfg_attr(not(test), allow(dead_code))]
+ fn slice_offset(&self, segment_idx: usize) -> usize {
+ segment_idx * self.chunk_len
+ }
+
+ #[inline]
+ #[cfg_attr(not(test), allow(dead_code))]
+ fn segment_len_for(&self, slice_offset: usize, aligned_current_slice_size: usize) -> usize {
+ debug_assert!(slice_offset < aligned_current_slice_size);
+ let len = (aligned_current_slice_size - slice_offset).min(self.chunk_len);
+ debug_assert!(len.is_multiple_of(self.stride));
+ len
+ }
+
+ #[inline]
+ fn input_storage_len(&self) -> usize {
+ self.input_batch_size * self.aligned_slice_len
+ }
+
+ #[inline]
+ fn output_storage_len(&self) -> usize {
+ self.recovery_count * self.aligned_slice_len
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn round_div(a: usize, b: usize) -> usize {
+ (a + (b >> 1)) / b
+}
+
+#[cfg(target_arch = "x86_64")]
+fn xor_jit_runtime_chunk_len(
+ slice_len: usize,
+ worker_count: usize,
+ method: CreateGf16Method,
+) -> usize {
+ let method_info = method
+ .xor_jit_create_method_info()
+ .expect("XOR-JIT runtime chunk length requires XOR-JIT method info");
+ let aligned_current_slice_size = align_up(slice_len, method_info.stride) + method_info.stride;
+ let ideal_chunk_len = align_down(
+ method
+ .ideal_segment_len()
+ .min(aligned_current_slice_size)
+ .max(method_info.stride),
+ method_info.stride,
+ )
+ .max(method_info.stride);
+ let target_thread_chunk = aligned_current_slice_size.div_ceil(worker_count.max(1));
+
+ let mut num_chunks = if target_thread_chunk <= ideal_chunk_len / 2 {
+ round_div(aligned_current_slice_size, ideal_chunk_len).max(1)
+ } else {
+ round_div(target_thread_chunk, ideal_chunk_len).max(1) * worker_count.max(1)
+ };
+
+ let chunk_len = align_up(
+ aligned_current_slice_size.div_ceil(num_chunks),
+ method_info.stride,
+ );
+ num_chunks = aligned_current_slice_size.div_ceil(chunk_len);
+ debug_assert!(num_chunks > 0);
+ chunk_len
}
/// Create-side recovery backend with all hot-path storage owned up front.
pub struct CreateRecoveryBackend {
- pub recovery_exponents: Vec,
pub source_count: usize,
+ pub recovery_exponents: Vec,
+ pub max_chunk_len: usize,
pub chunk_len: usize,
- pub output_chunks: Vec,
- pub input_staging: Vec,
- pub coeffs: Vec,
+ pub method: CreateGf16Method,
+ pub input_grouping: usize,
#[cfg(target_arch = "x86_64")]
- pub batch_coeffs: Vec,
- transfer_buffers: Vec,
- batch_source_indices: Vec,
- batch_len: usize,
+ xor_jit_bitplane: bool,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_layout: Option,
+ transfer_buffers: [AlignedVec; TRANSFER_BUFFER_COUNT],
+ pub staging: Vec,
+ pub output_chunks: AlignedVec,
+ pub coeffs: Vec,
+ workers: CreateWorkerPool,
+ aligned_chunk_len: usize,
+ active_staging: usize,
+ compute_in_flight: bool,
#[cfg(target_arch = "x86_64")]
- simd_level: SimdLevel,
+ processing_add: bool,
+ job_storage: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_req_storage: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_req_thread_starts: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_req_thread_counts: Vec,
}
impl CreateRecoveryBackend {
@@ -63,12 +435,55 @@ impl CreateRecoveryBackend {
first_recovery_block: u32,
recovery_count: usize,
max_chunk_len: usize,
+ thread_count: usize,
) -> Self {
+ let requested_method = CreateGf16Method::from_env();
+ let method = requested_method.resolve();
let source_count = base_values.len();
+ #[cfg(target_arch = "x86_64")]
+ let xor_jit_bitplane = method.xor_jit_flavor().is_some();
+ #[cfg(target_arch = "x86_64")]
+ let chunk_alignment = method
+ .xor_jit_create_method_info()
+ .map(|info| info.alignment)
+ .unwrap_or(AVX2_ALIGNMENT);
+ #[cfg(not(target_arch = "x86_64"))]
+ let chunk_alignment = AVX2_ALIGNMENT;
+ let aligned_chunk_len = align_up(max_chunk_len, chunk_alignment);
+ let input_grouping = input_grouping(source_count, method);
+ let worker_count = thread_count.max(1);
+ #[cfg(target_arch = "x86_64")]
+ let xor_jit_layout = xor_jit_bitplane.then(|| {
+ let method_info = method
+ .xor_jit_create_method_info()
+ .expect("xor-jit bitplane layout requires XOR-JIT method info");
+ let chunk_len = xor_jit_runtime_chunk_len(max_chunk_len, worker_count, method);
+ XorJitBitplaneLayout::new(
+ aligned_chunk_len,
+ chunk_len,
+ input_grouping,
+ recovery_count,
+ method_info,
+ )
+ });
+ #[cfg(target_arch = "x86_64")]
+ let staging_storage_len = xor_jit_layout
+ .map(|layout| layout.input_storage_len())
+ .unwrap_or(input_grouping * aligned_chunk_len);
+ #[cfg(not(target_arch = "x86_64"))]
+ let staging_storage_len = input_grouping * aligned_chunk_len;
+ #[cfg(target_arch = "x86_64")]
+ let output_storage_len = xor_jit_layout
+ .map(|layout| layout.output_storage_len())
+ .unwrap_or(recovery_count * aligned_chunk_len);
+ #[cfg(not(target_arch = "x86_64"))]
+ let output_storage_len = recovery_count * aligned_chunk_len;
let recovery_exponents = (0..recovery_count)
.map(|offset| (first_recovery_block + offset as u32) as u16)
.collect::>();
-
+ let prepare_pshufb = matches!(method, CreateGf16Method::Avx2PshufbPrepared);
+ #[cfg(target_arch = "x86_64")]
+ let mut xor_jit_cache = XorJitPreparedCoeffCache::new();
let coeffs = recovery_exponents
.iter()
.flat_map(|&exponent| {
@@ -76,117 +491,322 @@ impl CreateRecoveryBackend {
.iter()
.map(move |&base| Galois16::new(base).pow(exponent).value())
})
- .map(Gf16Coeff::new)
+ .map(|value| {
+ #[cfg(target_arch = "x86_64")]
+ {
+ CreateCoeff::new(value, prepare_pshufb, xor_jit_bitplane, &mut xor_jit_cache)
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ CreateCoeff::new(value, prepare_pshufb)
+ }
+ })
.collect::>();
-
+ let max_job_count = max_compute_jobs(
+ aligned_chunk_len.max(max_chunk_len),
+ recovery_count,
+ worker_count,
+ method,
+ );
+ let mut staging = vec![
+ {
+ #[cfg(target_arch = "x86_64")]
+ {
+ StagingArea::new(input_grouping, staging_storage_len, recovery_count)
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ StagingArea::new(input_grouping, staging_storage_len)
+ }
+ },
+ {
+ #[cfg(target_arch = "x86_64")]
+ {
+ StagingArea::new(input_grouping, staging_storage_len, recovery_count)
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ StagingArea::new(input_grouping, staging_storage_len)
+ }
+ },
+ ];
#[cfg(target_arch = "x86_64")]
- let batch_coeffs = coeffs
- .iter()
- .filter_map(|coeff| coeff.avx2.clone())
- .collect::>();
+ if xor_jit_bitplane {
+ let out_non_zero = recovery_exponents
+ .iter()
+ .map(|&exponent| u8::from(exponent != 0))
+ .collect::>();
+ staging.iter_mut().for_each(|staging| {
+ staging.xor_jit_out_non_zero.copy_from_slice(&out_non_zero);
+ });
+ }
Self {
- recovery_exponents,
source_count,
+ recovery_exponents,
+ max_chunk_len,
chunk_len: 0,
- output_chunks: (0..recovery_count)
- .map(|_| AlignedVec::new_zeroed(max_chunk_len))
- .collect(),
- input_staging: (0..DEFAULT_INPUT_BATCH_SIZE)
- .map(|_| AlignedVec::new_zeroed(max_chunk_len))
- .collect(),
+ method,
+ input_grouping,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_bitplane,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_layout,
+ transfer_buffers: [
+ AlignedVec::new_zeroed(aligned_chunk_len),
+ AlignedVec::new_zeroed(aligned_chunk_len),
+ ],
+ staging,
+ output_chunks: AlignedVec::new_zeroed(output_storage_len),
coeffs,
+ workers: CreateWorkerPool::new(worker_count, max_job_count),
+ aligned_chunk_len,
+ active_staging: 0,
+ compute_in_flight: false,
#[cfg(target_arch = "x86_64")]
- batch_coeffs,
- transfer_buffers: (0..TRANSFER_BUFFER_COUNT)
- .map(|_| AlignedVec::new_zeroed(max_chunk_len))
- .collect(),
- batch_source_indices: vec![0; DEFAULT_INPUT_BATCH_SIZE],
- batch_len: 0,
+ processing_add: false,
+ job_storage: vec![ComputeJob::default(); max_job_count],
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_req_storage: vec![XorJitComputeReq::default(); max_job_count],
#[cfg(target_arch = "x86_64")]
- simd_level: detect_simd_support(),
+ xor_jit_req_thread_starts: vec![0; worker_count],
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_req_thread_counts: vec![0; worker_count],
}
}
#[inline]
pub fn begin_chunk(&mut self, chunk_len: usize) {
self.chunk_len = chunk_len;
- self.batch_len = 0;
+ self.active_staging = 0;
+ debug_assert!(!self.compute_in_flight);
+ #[cfg(target_arch = "x86_64")]
+ {
+ self.processing_add = false;
+ if self.xor_jit_bitplane {
+ let method_info = self
+ .method
+ .xor_jit_create_method_info()
+ .expect("xor-jit layout refresh requires XOR-JIT method info");
+ let chunk_len =
+ xor_jit_runtime_chunk_len(chunk_len, self.workers.worker_count(), self.method);
+ self.xor_jit_layout = Some(XorJitBitplaneLayout::new(
+ self.aligned_chunk_len,
+ chunk_len,
+ self.input_grouping,
+ self.recovery_exponents.len(),
+ method_info,
+ ));
+ }
+ }
+ self.staging.iter_mut().for_each(|staging| {
+ staging.batch_len = 0;
+ #[cfg(target_arch = "x86_64")]
+ staging.proc_refs.store(0, Ordering::Relaxed);
+ });
+ debug_assert!(chunk_len <= self.max_chunk_len);
debug_assert_eq!(
self.coeffs.len(),
self.recovery_exponents.len() * self.source_count
);
- #[cfg(target_arch = "x86_64")]
- debug_assert_eq!(self.batch_coeffs.len(), self.coeffs.len());
- debug_assert!(self
- .input_staging
- .iter()
- .all(|buffer| (buffer.as_ptr() as usize).is_multiple_of(32)));
debug_assert!(self
- .output_chunks
+ .staging
.iter()
- .all(|buffer| chunk_len <= buffer.len()));
+ .all(|staging| (staging.inputs.as_ptr() as usize).is_multiple_of(AVX2_ALIGNMENT)));
+ debug_assert!((self.output_chunks.as_ptr() as usize).is_multiple_of(AVX2_ALIGNMENT));
+ #[cfg(target_arch = "x86_64")]
+ if let Some(layout) = self.xor_jit_layout {
+ debug_assert!(self.output_chunks.len() >= layout.output_storage_len());
+ } else {
+ debug_assert!(
+ self.output_chunks.len() >= self.recovery_exponents.len() * self.aligned_chunk_len
+ );
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ debug_assert!(
+ self.output_chunks.len() >= self.recovery_exponents.len() * self.aligned_chunk_len
+ );
+ debug_assert!(self.workers.capacity() >= self.job_storage.len());
+ #[cfg(target_arch = "x86_64")]
+ debug_assert!(self.workers.capacity() >= self.xor_jit_req_storage.len());
+ #[cfg(target_arch = "x86_64")]
+ debug_assert!(
+ self.method != CreateGf16Method::Avx2PshufbPrepared
+ || self.coeffs.iter().all(|c| c.avx2.is_some())
+ );
+ #[cfg(target_arch = "x86_64")]
+ debug_assert!(
+ !self.xor_jit_bitplane || self.coeffs.iter().all(|c| c.xor_jit_bitplane.is_some())
+ );
- self.output_chunks
- .iter_mut()
- .for_each(|chunk| chunk[..chunk_len].fill(0));
+ #[cfg(target_arch = "x86_64")]
+ if !self.xor_jit_bitplane {
+ self.output_chunks.fill(0);
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ self.output_chunks.fill(0);
}
#[inline]
- pub fn prepare_transfer_buffer(&mut self, ring_index: usize) -> &mut [u8] {
- let idx = ring_index % self.transfer_buffers.len();
+ pub fn transfer_buffer(&mut self, ring_index: usize) -> &mut [u8] {
+ let idx = ring_index % TRANSFER_BUFFER_COUNT;
let chunk = &mut self.transfer_buffers[idx][..self.chunk_len];
chunk.fill(0);
chunk
}
+ #[inline]
+ pub fn prepare_transfer_buffer(&mut self, ring_index: usize) -> &mut [u8] {
+ self.transfer_buffer(ring_index)
+ }
+
#[inline]
pub fn add_input(&mut self, source_idx: usize, input_chunk: &[u8]) {
debug_assert!(source_idx < self.source_count);
debug_assert_eq!(input_chunk.len(), self.chunk_len);
- debug_assert!(self.batch_len < self.input_staging.len());
+ let staging_idx = self.active_staging;
+ let staging = &mut self.staging[staging_idx];
+ debug_assert!(staging.batch_len < self.input_grouping);
- let slot = self.batch_len;
- self.input_staging[slot][..self.chunk_len].copy_from_slice(input_chunk);
- self.batch_source_indices[slot] = source_idx;
- self.batch_len += 1;
+ let slot = staging.batch_len;
+ #[cfg(target_arch = "x86_64")]
+ if self.xor_jit_bitplane {
+ let layout = self
+ .xor_jit_layout
+ .expect("XOR-JIT bitplane layout initialized");
+ prepare_xor_jit_bitplane_staging(layout, staging, slot, self.chunk_len, input_chunk);
+ } else {
+ staging
+ .slot_mut(slot, self.aligned_chunk_len, self.chunk_len)
+ .copy_from_slice(input_chunk);
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ staging
+ .slot_mut(slot, self.aligned_chunk_len, self.chunk_len)
+ .copy_from_slice(input_chunk);
+ staging.source_indices[slot] = source_idx;
+ #[cfg(target_arch = "x86_64")]
+ if self.xor_jit_bitplane {
+ pack_xor_jit_bitplane_coeffs(
+ &self.coeffs,
+ self.source_count,
+ self.recovery_exponents.len(),
+ self.input_grouping,
+ staging,
+ slot,
+ source_idx,
+ );
+ }
+ staging.batch_len += 1;
- if self.batch_len == self.input_staging.len() {
- self.flush_batch();
+ if staging.batch_len == self.input_grouping {
+ self.flush_active_staging();
}
}
#[inline]
pub fn add_transfer_input(&mut self, source_idx: usize, ring_index: usize) {
- let idx = ring_index % self.transfer_buffers.len();
+ let idx = ring_index % TRANSFER_BUFFER_COUNT;
+ debug_assert!(source_idx < self.source_count);
debug_assert!(self.chunk_len <= self.transfer_buffers[idx].len());
- debug_assert!(self.batch_len < self.input_staging.len());
+ let staging_idx = self.active_staging;
+ let staging = &mut self.staging[staging_idx];
+ debug_assert!(staging.batch_len < self.input_grouping);
- let slot = self.batch_len;
- self.input_staging[slot][..self.chunk_len]
+ let slot = staging.batch_len;
+ #[cfg(target_arch = "x86_64")]
+ if self.xor_jit_bitplane {
+ let layout = self
+ .xor_jit_layout
+ .expect("XOR-JIT bitplane layout initialized");
+ prepare_xor_jit_bitplane_staging(
+ layout,
+ staging,
+ slot,
+ self.chunk_len,
+ &self.transfer_buffers[idx][..self.chunk_len],
+ );
+ } else {
+ staging
+ .slot_mut(slot, self.aligned_chunk_len, self.chunk_len)
+ .copy_from_slice(&self.transfer_buffers[idx][..self.chunk_len]);
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ staging
+ .slot_mut(slot, self.aligned_chunk_len, self.chunk_len)
.copy_from_slice(&self.transfer_buffers[idx][..self.chunk_len]);
- self.batch_source_indices[slot] = source_idx;
- self.batch_len += 1;
+ staging.source_indices[slot] = source_idx;
+ #[cfg(target_arch = "x86_64")]
+ if self.xor_jit_bitplane {
+ pack_xor_jit_bitplane_coeffs(
+ &self.coeffs,
+ self.source_count,
+ self.recovery_exponents.len(),
+ self.input_grouping,
+ staging,
+ slot,
+ source_idx,
+ );
+ }
+ staging.batch_len += 1;
- if self.batch_len == self.input_staging.len() {
- self.flush_batch();
+ if staging.batch_len == self.input_grouping {
+ self.flush_active_staging();
}
}
#[inline]
- pub fn finish_chunk(&mut self, recovery_blocks: &mut [(u16, Vec)], block_size: usize) {
- self.flush_batch();
+ pub fn end_input(&mut self) {
+ self.flush_active_staging();
+ self.wait_for_compute();
+ }
- self.output_chunks
- .iter()
- .zip(recovery_blocks.iter_mut())
- .for_each(|(output_chunk, (_, recovery_data))| {
+ #[inline]
+ pub fn finish_chunk(
+ &mut self,
+ recovery_blocks: &mut [(u16, Vec)],
+ block_size: usize,
+ ) -> bool {
+ self.end_input();
+ let mut all_checksum_ok = true;
+
+ recovery_blocks
+ .iter_mut()
+ .enumerate()
+ .for_each(|(recovery_idx, (_, recovery_data))| {
debug_assert!(recovery_data.capacity() >= block_size);
debug_assert!(recovery_data.len() + self.chunk_len <= recovery_data.capacity());
- debug_assert!(self.chunk_len <= output_chunk.len());
- recovery_data.extend_from_slice(&output_chunk[..self.chunk_len]);
+ #[cfg(target_arch = "x86_64")]
+ if self.xor_jit_bitplane {
+ let layout = self
+ .xor_jit_layout
+ .expect("XOR-JIT bitplane layout initialized");
+ let output_start = recovery_data.len();
+ recovery_data.resize(output_start + self.chunk_len, 0);
+ let output_checksum_ok = finish_xor_jit_bitplane_packed_output_cksum(
+ &mut recovery_data[output_start..output_start + self.chunk_len],
+ &self.output_chunks,
+ layout.recovery_count,
+ recovery_idx,
+ layout.chunk_len,
+ );
+ all_checksum_ok &= output_checksum_ok;
+ } else {
+ let start = recovery_idx * self.aligned_chunk_len;
+ let end = start + self.chunk_len;
+ debug_assert!(end <= self.output_chunks.len());
+ recovery_data.extend_from_slice(&self.output_chunks[start..end]);
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ let start = recovery_idx * self.aligned_chunk_len;
+ let end = start + self.chunk_len;
+ debug_assert!(end <= self.output_chunks.len());
+ recovery_data.extend_from_slice(&self.output_chunks[start..end]);
+ }
});
+ all_checksum_ok
}
#[inline]
@@ -198,138 +818,1547 @@ impl CreateRecoveryBackend {
}
#[inline]
- fn flush_batch(&mut self) {
- if self.batch_len == 0 {
+ pub fn selected_method(&self) -> CreateGf16Method {
+ self.method
+ }
+
+ #[inline]
+ fn flush_active_staging(&mut self) {
+ let staging_idx = self.active_staging;
+ if self.staging[staging_idx].batch_len == 0 {
return;
}
- let chunk_len = self.chunk_len;
- let source_count = self.source_count;
- let batch_len = self.batch_len;
- let coeffs = &self.coeffs;
- let input_staging = &self.input_staging;
- let source_indices = &self.batch_source_indices;
+ self.wait_for_compute();
#[cfg(target_arch = "x86_64")]
- let simd_level = self.simd_level;
+ if self.xor_jit_bitplane {
+ let processing_add = self.processing_add;
+ let request_count = self.build_xor_jit_requests(staging_idx, processing_add);
+ self.staging[staging_idx]
+ .proc_refs
+ .store(request_count, Ordering::Release);
+ self.workers.submit_xor_jit(
+ &self.xor_jit_req_storage[..request_count],
+ &self.xor_jit_req_thread_starts,
+ &self.xor_jit_req_thread_counts,
+ );
+ self.processing_add = true;
+ } else {
+ let job_count = self.build_compute_jobs(staging_idx);
+ self.workers.submit(&self.job_storage[..job_count]);
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ {
+ let job_count = self.build_compute_jobs(staging_idx);
+ self.workers.submit(&self.job_storage[..job_count]);
+ }
+ self.compute_in_flight = true;
+ self.staging[staging_idx].batch_len = 0;
+ self.active_staging = (self.active_staging + 1) % self.staging.len();
+ }
- self.output_chunks
- .par_iter_mut()
- .enumerate()
- .for_each(|(recovery_idx, output_chunk)| {
- let output = &mut output_chunk[..chunk_len];
- #[cfg(target_arch = "x86_64")]
- if matches!(simd_level, SimdLevel::Avx2) {
- Self::process_batch_add_avx2_x2(
- recovery_idx,
- source_count,
- batch_len,
- input_staging,
- source_indices,
- coeffs,
- output,
- );
- return;
- }
+ #[inline]
+ fn wait_for_compute(&mut self) {
+ if self.compute_in_flight {
+ self.workers.wait();
+ #[cfg(target_arch = "x86_64")]
+ debug_assert!(self
+ .staging
+ .iter()
+ .all(|staging| staging.proc_refs.load(Ordering::Acquire) == 0));
+ self.compute_in_flight = false;
+ }
+ }
- (0..batch_len).for_each(|batch_idx| {
- let source_idx = source_indices[batch_idx];
- let coeff = &coeffs[gf_coeff_index(recovery_idx, source_idx, source_count)];
- let input = &input_staging[batch_idx][..chunk_len];
- #[cfg(target_arch = "x86_64")]
- Self::process_input_add(input, output, coeff, simd_level);
- #[cfg(not(target_arch = "x86_64"))]
- Self::process_input_add(input, output, coeff);
- });
- });
+ fn build_compute_jobs(&mut self, staging_idx: usize) -> usize {
+ let worker_count = self.workers.worker_count();
+ let recovery_count = self.recovery_exponents.len();
+ let compute_len = self.chunk_len;
+ // Mirror turbo `calcChunkSize`: split the *aligned* slice across
+ // segments. Computing on the unaligned `chunk_len` lets the segment_len
+ // round down below what `max_compute_jobs` predicted, producing more
+ // segments than the preallocated job_storage can hold.
+ let segment_basis = self.aligned_chunk_len.max(compute_len);
+ let segment_len = align_down(
+ self.method
+ .ideal_segment_len()
+ .min(segment_basis)
+ .max(AVX2_ALIGNMENT),
+ AVX2_ALIGNMENT,
+ )
+ .max(AVX2_ALIGNMENT);
+ let segment_count = segment_basis.div_ceil(segment_len);
+ let output_groups = worker_count.min(recovery_count).max(1);
+ let outputs_per_group = recovery_count.div_ceil(output_groups);
+ let staging = &self.staging[staging_idx];
+ debug_assert!(staging.batch_len <= self.input_grouping);
+ debug_assert!(self.coeffs.len() == recovery_count * self.source_count);
- self.batch_len = 0;
+ let mut job_count = 0;
+ for segment_idx in 0..segment_count {
+ let start = segment_idx * segment_len;
+ if start >= compute_len {
+ break;
+ }
+ let len = (compute_len - start).min(segment_len);
+ for output_group in 0..output_groups {
+ let output_start = output_group * outputs_per_group;
+ let output_end = ((output_group + 1) * outputs_per_group).min(recovery_count);
+ if output_start == output_end {
+ continue;
+ }
+ debug_assert!(job_count < self.job_storage.len());
+ self.job_storage[job_count] = ComputeJob {
+ method: self.method,
+ input_base: staging.inputs.as_ptr() as usize,
+ output_base: self.output_chunks.as_ptr() as usize,
+ coeffs: self.coeffs.as_ptr() as usize,
+ source_indices: staging.source_indices.as_ptr() as usize,
+ source_count: self.source_count,
+ batch_len: staging.batch_len,
+ aligned_chunk_len: self.aligned_chunk_len,
+ segment_start: start,
+ segment_len: len,
+ output_start,
+ output_end,
+ };
+ job_count += 1;
+ }
+ }
+ job_count
}
#[cfg(target_arch = "x86_64")]
- #[inline]
- fn process_batch_add_avx2_x2(
- recovery_idx: usize,
- source_count: usize,
- batch_len: usize,
- input_staging: &[AlignedVec],
- source_indices: &[usize],
- coeffs: &[Gf16Coeff],
- output: &mut [u8],
- ) {
- let mut batch_idx = 0;
- while batch_idx + 1 < batch_len {
- let source_a = source_indices[batch_idx];
- let source_b = source_indices[batch_idx + 1];
- let coeff_a = &coeffs[gf_coeff_index(recovery_idx, source_a, source_count)];
- let coeff_b = &coeffs[gf_coeff_index(recovery_idx, source_b, source_count)];
-
- match (&coeff_a.avx2, &coeff_b.avx2) {
- (Some(prepared_a), Some(prepared_b)) => unsafe {
- process_slices_multiply_add_prepared_avx2_x2(
- &input_staging[batch_idx][..output.len()],
- prepared_a,
- &coeff_a.split,
- &input_staging[batch_idx + 1][..output.len()],
- prepared_b,
- &coeff_b.split,
- output,
- );
- },
- _ => {
- Self::process_input_add(
- &input_staging[batch_idx][..output.len()],
- output,
- coeff_a,
- SimdLevel::None,
- );
- Self::process_input_add(
- &input_staging[batch_idx + 1][..output.len()],
- output,
- coeff_b,
- SimdLevel::None,
- );
- }
- }
+ fn build_xor_jit_requests(&mut self, staging_idx: usize, add: bool) -> usize {
+ let staging = &self.staging[staging_idx];
+ let layout = self
+ .xor_jit_layout
+ .expect("XOR-JIT bitplane layout initialized");
+ let worker_count = self.workers.worker_count();
+ let recovery_count = self.recovery_exponents.len();
+ let aligned_current_slice_size = layout.aligned_current_slice_size(self.chunk_len);
+ let full_chunks_per_thread =
+ layout.segment_count_for(aligned_current_slice_size) / worker_count.max(1);
+ let leftover_chunks =
+ layout.segment_count_for(aligned_current_slice_size) % worker_count.max(1);
+ self.xor_jit_req_thread_counts.fill(0);
+ let mut chunk = 0usize;
+ let input_base = staging.inputs.as_ptr() as usize;
+ let output_base = self.output_chunks.as_ptr() as usize;
+ let coeffs_base = staging.xor_jit_coeffs.as_ptr() as usize;
+ let out_non_zero_base = staging.xor_jit_out_non_zero.as_ptr() as usize;
+ let proc_refs = &staging.proc_refs as *const AtomicUsize as usize;
- batch_idx += 2;
+ if leftover_chunks != 0 {
+ let threads_per_chunk = (worker_count / leftover_chunks).min(recovery_count).max(1);
+ let used_threads = threads_per_chunk * leftover_chunks;
+ self.xor_jit_req_thread_counts[..used_threads]
+ .iter_mut()
+ .for_each(|count| *count += 1);
+ }
+ if full_chunks_per_thread != 0 {
+ self.xor_jit_req_thread_counts
+ .iter_mut()
+ .for_each(|count| *count += 1);
}
- if batch_idx < batch_len {
- let source_idx = source_indices[batch_idx];
- let coeff = &coeffs[gf_coeff_index(recovery_idx, source_idx, source_count)];
- Self::process_input_add(
- &input_staging[batch_idx][..output.len()],
- output,
- coeff,
- SimdLevel::Avx2,
- );
+ let mut request_count = 0usize;
+ for (thread, count) in self.xor_jit_req_thread_counts.iter().enumerate() {
+ self.xor_jit_req_thread_starts[thread] = request_count;
+ request_count += *count;
+ }
+ let mut thread_positions = self.xor_jit_req_thread_starts.clone();
+
+ if leftover_chunks != 0 {
+ let threads_per_chunk = (worker_count / leftover_chunks).min(recovery_count).max(1);
+ let outputs_per_thread = recovery_count as f64 / threads_per_chunk as f64;
+ let mut thread = 0usize;
+
+ for _ in 0..leftover_chunks {
+ let slice_offset = chunk * layout.chunk_len;
+ let req_len = (aligned_current_slice_size - slice_offset).min(layout.chunk_len);
+ let local_input = input_base + slice_offset * self.input_grouping;
+ let local_output_base = output_base + slice_offset * recovery_count;
+ let mut output_start = 0usize;
+ for thread_chunk in 0..threads_per_chunk {
+ let output_end = (((thread_chunk + 1) as f64 * outputs_per_thread).round()
+ as usize)
+ .min(recovery_count)
+ .max(output_start + 1);
+ let num_outputs = output_end - output_start;
+ let position = thread_positions[thread];
+ thread_positions[thread] += 1;
+ debug_assert!(position < self.xor_jit_req_storage.len());
+ self.xor_jit_req_storage[position] = XorJitComputeReq {
+ method: self.method,
+ input: local_input,
+ output: local_output_base + output_start * req_len,
+ coeffs: coeffs_base
+ + output_start * self.input_grouping * std::mem::size_of::(),
+ out_non_zero: out_non_zero_base + output_start * std::mem::size_of::(),
+ proc_refs,
+ num_inputs: staging.batch_len,
+ input_grouping: self.input_grouping,
+ chunk_size: layout.chunk_len,
+ num_chunks: 1,
+ len: req_len,
+ num_outputs,
+ add,
+ };
+ output_start = output_end;
+ thread += 1;
+ }
+ debug_assert_eq!(output_start, recovery_count);
+ chunk += 1;
+ }
}
+
+ if full_chunks_per_thread != 0 {
+ for thread in 0..worker_count {
+ debug_assert!(chunk < layout.segment_count_for(aligned_current_slice_size));
+ let slice_offset = chunk * layout.chunk_len;
+ let req_len = (aligned_current_slice_size - slice_offset)
+ .min(layout.chunk_len * full_chunks_per_thread);
+ let position = thread_positions[thread];
+ thread_positions[thread] += 1;
+ debug_assert!(position < self.xor_jit_req_storage.len());
+ self.xor_jit_req_storage[position] = XorJitComputeReq {
+ method: self.method,
+ input: input_base + slice_offset * self.input_grouping,
+ output: output_base + slice_offset * recovery_count,
+ coeffs: coeffs_base,
+ out_non_zero: out_non_zero_base,
+ proc_refs,
+ num_inputs: staging.batch_len,
+ input_grouping: self.input_grouping,
+ chunk_size: layout.chunk_len,
+ num_chunks: full_chunks_per_thread,
+ len: req_len,
+ num_outputs: recovery_count,
+ add,
+ };
+ chunk += full_chunks_per_thread;
+ }
+ }
+
+ debug_assert_eq!(chunk, layout.segment_count_for(aligned_current_slice_size));
+ debug_assert!(thread_positions
+ .iter()
+ .zip(
+ self.xor_jit_req_thread_starts
+ .iter()
+ .zip(self.xor_jit_req_thread_counts.iter())
+ )
+ .all(|(position, (start, count))| *position == *start + *count));
+ debug_assert!(request_count > 0);
+ request_count
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn prepare_xor_jit_bitplane_staging(
+ layout: XorJitBitplaneLayout,
+ staging: &mut StagingArea,
+ slot: usize,
+ slice_len: usize,
+ input_chunk: &[u8],
+) {
+ let aligned_slice_len = layout.align_to_stride(slice_len);
+ debug_assert!(aligned_slice_len.is_multiple_of(layout.stride));
+ debug_assert!(input_chunk.len() <= slice_len);
+ prepare_xor_jit_bitplane_packed_input_cksum(
+ &mut staging.inputs,
+ input_chunk,
+ aligned_slice_len,
+ layout.input_batch_size,
+ slot,
+ layout.chunk_len,
+ );
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn pack_xor_jit_bitplane_coeffs(
+ coeffs: &[CreateCoeff],
+ source_count: usize,
+ recovery_count: usize,
+ input_grouping: usize,
+ staging: &mut StagingArea,
+ slot: usize,
+ source_idx: usize,
+) {
+ for recovery_idx in 0..recovery_count {
+ let coeff = &coeffs[gf_coeff_index(recovery_idx, source_idx, source_count)];
+ staging.xor_jit_coeffs[recovery_idx * input_grouping + slot] = coeff.value;
}
+}
+
+#[cfg(target_arch = "x86_64")]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+struct XorJitComputeReq {
+ method: CreateGf16Method,
+ input: usize,
+ output: usize,
+ coeffs: usize,
+ out_non_zero: usize,
+ proc_refs: usize,
+ num_inputs: usize,
+ input_grouping: usize,
+ chunk_size: usize,
+ num_chunks: usize,
+ len: usize,
+ num_outputs: usize,
+ add: bool,
+}
+
+#[cfg(target_arch = "x86_64")]
+impl XorJitComputeReq {
+ #[inline]
+ fn method_info(self) -> XorJitCreateMethodInfo {
+ self.method
+ .xor_jit_create_method_info()
+ .expect("xor-jit request requires XOR-JIT method info")
+ }
+}
+
+#[derive(Clone, Copy, Default)]
+struct ComputeJob {
+ method: CreateGf16Method,
+ input_base: usize,
+ output_base: usize,
+ coeffs: usize,
+ source_indices: usize,
+ source_count: usize,
+ batch_len: usize,
+ aligned_chunk_len: usize,
+ segment_start: usize,
+ segment_len: usize,
+ output_start: usize,
+ output_end: usize,
+}
+
+impl Default for CreateGf16Method {
+ fn default() -> Self {
+ Self::Scalar
+ }
+}
+
+struct CreateWorkerPool {
+ shared: Arc,
+ handles: Vec>,
+}
+
+struct WorkerShared {
+ state: Mutex,
+ ready: Condvar,
+ done: Condvar,
+}
+
+struct WorkerState {
+ jobs: Vec,
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_worker_reqs: Vec,
+ task_kind: WorkerTaskKind,
+ task_count: usize,
+ next_task: usize,
+ remaining_tasks: usize,
+ generation: u64,
+ stop: bool,
+}
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
+enum WorkerTaskKind {
+ #[default]
+ ComputeJob,
#[cfg(target_arch = "x86_64")]
+ XorJit,
+}
+
+#[cfg(target_arch = "x86_64")]
+enum WorkerTask {
+ ComputeJob(ComputeJob),
+ XorJitRequest(*const XorJitComputeReq, *const AtomicUsize, bool),
+}
+
+#[cfg(target_arch = "x86_64")]
+#[derive(Clone, Copy, Debug, Default)]
+struct XorJitWorkerReqState {
+ reqs: usize,
+ len: usize,
+ next_req: usize,
+}
+
+#[cfg(target_arch = "x86_64")]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum XorJitWorkerBranch {
+ MulAddMultiPackpf,
+ AddMultiPackpf,
+ MulAddMultiPacked,
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn xor_jit_worker_branch(
+ final_round: bool,
+ last_local_output: bool,
+ out_non_zero: bool,
+) -> XorJitWorkerBranch {
+ if final_round && last_local_output {
+ XorJitWorkerBranch::MulAddMultiPacked
+ } else if out_non_zero {
+ XorJitWorkerBranch::MulAddMultiPackpf
+ } else {
+ XorJitWorkerBranch::AddMultiPackpf
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[derive(Default)]
+struct WorkerContext {
+ xor_jit_bitplane_scratch: Option,
+}
+
+impl CreateWorkerPool {
+ fn new(worker_count: usize, max_jobs: usize) -> Self {
+ let shared = Arc::new(WorkerShared {
+ state: Mutex::new(WorkerState {
+ jobs: vec![ComputeJob::default(); max_jobs.max(1)],
+ #[cfg(target_arch = "x86_64")]
+ xor_jit_worker_reqs: vec![XorJitWorkerReqState::default(); worker_count.max(1)],
+ task_kind: WorkerTaskKind::ComputeJob,
+ task_count: 0,
+ next_task: 0,
+ remaining_tasks: 0,
+ generation: 0,
+ stop: false,
+ }),
+ ready: Condvar::new(),
+ done: Condvar::new(),
+ });
+ let handles = (0..worker_count)
+ .map(|worker_id| {
+ let shared = Arc::clone(&shared);
+ std::thread::spawn(move || worker_loop(shared, worker_id))
+ })
+ .collect::>();
+
+ Self { shared, handles }
+ }
+
+ #[inline]
+ fn worker_count(&self) -> usize {
+ self.handles.len().max(1)
+ }
+
#[inline]
- fn process_input_add(
- input: &[u8],
- output: &mut [u8],
- coeff: &Gf16Coeff,
- simd_level: SimdLevel,
- ) {
- if matches!(simd_level, SimdLevel::Avx2) {
- if let Some(prepared) = &coeff.avx2 {
+ fn capacity(&self) -> usize {
+ self.shared.state.lock().unwrap().jobs.len()
+ }
+
+ fn submit(&self, jobs: &[ComputeJob]) {
+ if jobs.is_empty() {
+ return;
+ }
+
+ let mut state = self.shared.state.lock().unwrap();
+ debug_assert_eq!(state.remaining_tasks, 0);
+ debug_assert!(jobs.len() <= state.jobs.len());
+ state.jobs[..jobs.len()].copy_from_slice(jobs);
+ state.task_kind = WorkerTaskKind::ComputeJob;
+ state.task_count = jobs.len();
+ state.next_task = 0;
+ state.remaining_tasks = jobs.len();
+ state.generation = state.generation.wrapping_add(1);
+ self.shared.ready.notify_all();
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ fn submit_xor_jit(&self, reqs: &[XorJitComputeReq], starts: &[usize], counts: &[usize]) {
+ if reqs.is_empty() {
+ return;
+ }
+
+ let mut state = self.shared.state.lock().unwrap();
+ debug_assert_eq!(state.remaining_tasks, 0);
+ debug_assert_eq!(starts.len(), state.xor_jit_worker_reqs.len());
+ debug_assert_eq!(counts.len(), state.xor_jit_worker_reqs.len());
+ let mut active_workers = 0usize;
+ for worker_id in 0..state.xor_jit_worker_reqs.len() {
+ let start = starts[worker_id];
+ let count = counts[worker_id];
+ if count != 0 {
+ state.xor_jit_worker_reqs[worker_id] = XorJitWorkerReqState {
+ reqs: reqs[start..start + count].as_ptr() as usize,
+ len: count,
+ next_req: 0,
+ };
+ active_workers += 1;
+ } else {
+ state.xor_jit_worker_reqs[worker_id] = XorJitWorkerReqState::default();
+ }
+ }
+ state.task_kind = WorkerTaskKind::XorJit;
+ state.task_count = active_workers;
+ state.next_task = 0;
+ state.remaining_tasks = active_workers;
+ state.generation = state.generation.wrapping_add(1);
+ self.shared.ready.notify_all();
+ }
+
+ fn wait(&self) {
+ let mut state = self.shared.state.lock().unwrap();
+ while state.remaining_tasks != 0 {
+ state = self.shared.done.wait(state).unwrap();
+ }
+ }
+}
+
+impl Drop for CreateWorkerPool {
+ fn drop(&mut self) {
+ self.wait();
+ {
+ let mut state = self.shared.state.lock().unwrap();
+ state.stop = true;
+ state.generation = state.generation.wrapping_add(1);
+ }
+ self.shared.ready.notify_all();
+ while let Some(handle) = self.handles.pop() {
+ let _ = handle.join();
+ }
+ }
+}
+
+fn worker_loop(shared: Arc, worker_id: usize) {
+ #[cfg(target_arch = "x86_64")]
+ let mut context = WorkerContext::default();
+ let mut seen_generation = 0u64;
+ loop {
+ #[cfg(target_arch = "x86_64")]
+ let task = {
+ let mut state = shared.state.lock().unwrap();
+ loop {
+ if state.stop {
+ return;
+ }
+ if state.generation != seen_generation {
+ match state.task_kind {
+ WorkerTaskKind::ComputeJob if state.next_task < state.task_count => {
+ let task = WorkerTask::ComputeJob(state.jobs[state.next_task]);
+ state.next_task += 1;
+ break task;
+ }
+ WorkerTaskKind::ComputeJob => {
+ seen_generation = state.generation;
+ }
+ WorkerTaskKind::XorJit => {
+ let worker_reqs = state.xor_jit_worker_reqs[worker_id];
+ if worker_reqs.next_req < worker_reqs.len {
+ let req_ptr = (worker_reqs.reqs as *const XorJitComputeReq)
+ .wrapping_add(worker_reqs.next_req);
+ let req = unsafe { &*req_ptr };
+ state.xor_jit_worker_reqs[worker_id].next_req += 1;
+ let last_for_worker = worker_reqs.next_req + 1 == worker_reqs.len;
+ break WorkerTask::XorJitRequest(
+ req_ptr,
+ req.proc_refs as *const AtomicUsize,
+ last_for_worker,
+ );
+ }
+ seen_generation = state.generation;
+ }
+ }
+ }
+ state = shared.ready.wait(state).unwrap();
+ }
+ };
+ #[cfg(not(target_arch = "x86_64"))]
+ let job = {
+ let mut state = shared.state.lock().unwrap();
+ loop {
+ if state.stop {
+ return;
+ }
+ if state.generation != seen_generation && state.next_task < state.task_count {
+ let job = state.jobs[state.next_task];
+ state.next_task += 1;
+ break job;
+ }
+ if state.generation != seen_generation && state.next_task >= state.task_count {
+ seen_generation = state.generation;
+ }
+ state = shared.ready.wait(state).unwrap();
+ }
+ };
+
+ #[cfg(target_arch = "x86_64")]
+ match task {
+ WorkerTask::ComputeJob(job) => process_compute_job(job, &mut context),
+ WorkerTask::XorJitRequest(req, proc_refs, last_for_worker) => {
+ let req = unsafe { *req };
+ process_xor_jit_request(req, &mut context);
+ unsafe { (*proc_refs).fetch_sub(1, Ordering::AcqRel) };
+ if !last_for_worker {
+ continue;
+ }
+ }
+ }
+ #[cfg(not(target_arch = "x86_64"))]
+ process_compute_job(job);
+
+ let mut state = shared.state.lock().unwrap();
+ state.remaining_tasks -= 1;
+ if state.remaining_tasks == 0 {
+ seen_generation = state.generation;
+ shared.done.notify_one();
+ }
+ }
+}
+
+#[cfg(not(target_arch = "x86_64"))]
+fn process_compute_job(job: ComputeJob) {
+ for recovery_idx in job.output_start..job.output_end {
+ let output_start = recovery_idx * job.aligned_chunk_len + job.segment_start;
+ let output = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_start),
+ job.segment_len,
+ )
+ };
+ debug_assert!(output.as_ptr() as usize >= job.output_base);
+
+ for batch_idx in 0..job.batch_len {
+ let source_idx = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let coeff = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_idx,
+ job.source_count,
+ ))
+ };
+ let input_start = batch_idx * job.aligned_chunk_len + job.segment_start;
+ let input = unsafe {
+ std::slice::from_raw_parts(
+ (job.input_base as *const u8).add(input_start),
+ job.segment_len,
+ )
+ };
+ process_slice_multiply_add(input, output, &coeff.split);
+ }
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_compute_job(job: ComputeJob, context: &mut WorkerContext) {
+ #[cfg(target_arch = "x86_64")]
+ if let Some(flavor) = job.method.xor_jit_flavor() {
+ process_compute_job_xor_jit(job, flavor, context);
+ return;
+ }
+
+ for recovery_idx in job.output_start..job.output_end {
+ let output_start = recovery_idx * job.aligned_chunk_len + job.segment_start;
+ let output = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_start),
+ job.segment_len,
+ )
+ };
+ debug_assert!(output.as_ptr() as usize >= job.output_base);
+
+ if matches!(job.method, CreateGf16Method::Avx2PshufbPrepared) {
+ process_batch_add_avx2_pshufb(job, recovery_idx, output);
+ continue;
+ }
+
+ for batch_idx in 0..job.batch_len {
+ let source_idx = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let coeff = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_idx,
+ job.source_count,
+ ))
+ };
+ let input_start = batch_idx * job.aligned_chunk_len + job.segment_start;
+ let input = unsafe {
+ std::slice::from_raw_parts(
+ (job.input_base as *const u8).add(input_start),
+ job.segment_len,
+ )
+ };
+ process_slice_multiply_add(input, output, &coeff.split);
+ }
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_compute_job_xor_jit(
+ job: ComputeJob,
+ flavor: XorJitFlavor,
+ _context: &mut WorkerContext,
+) {
+ let mut recovery_idx = job.output_start;
+ while recovery_idx + 3 < job.output_end {
+ let output_a_start = recovery_idx * job.aligned_chunk_len + job.segment_start;
+ let output_b_start = (recovery_idx + 1) * job.aligned_chunk_len + job.segment_start;
+ let output_c_start = (recovery_idx + 2) * job.aligned_chunk_len + job.segment_start;
+ let output_d_start = (recovery_idx + 3) * job.aligned_chunk_len + job.segment_start;
+ let output_a = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_a_start),
+ job.segment_len,
+ )
+ };
+ let output_b = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_b_start),
+ job.segment_len,
+ )
+ };
+ let output_c = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_c_start),
+ job.segment_len,
+ )
+ };
+ let output_d = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_d_start),
+ job.segment_len,
+ )
+ };
+ process_batch_add_avx2_xor_jit_x4_outputs(
+ job,
+ recovery_idx,
+ output_a,
+ recovery_idx + 1,
+ output_b,
+ recovery_idx + 2,
+ output_c,
+ recovery_idx + 3,
+ output_d,
+ flavor,
+ );
+ recovery_idx += 4;
+ }
+
+ while recovery_idx + 1 < job.output_end {
+ let output_a_start = recovery_idx * job.aligned_chunk_len + job.segment_start;
+ let output_b_start = (recovery_idx + 1) * job.aligned_chunk_len + job.segment_start;
+ let output_a = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_a_start),
+ job.segment_len,
+ )
+ };
+ let output_b = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_b_start),
+ job.segment_len,
+ )
+ };
+ process_batch_add_avx2_xor_jit_x2_outputs(
+ job,
+ recovery_idx,
+ output_a,
+ recovery_idx + 1,
+ output_b,
+ flavor,
+ );
+ recovery_idx += 2;
+ }
+
+ if recovery_idx < job.output_end {
+ let output_start = recovery_idx * job.aligned_chunk_len + job.segment_start;
+ let output = unsafe {
+ std::slice::from_raw_parts_mut(
+ (job.output_base as *mut u8).add(output_start),
+ job.segment_len,
+ )
+ };
+ process_batch_add_avx2_xor_jit(job, recovery_idx, output, flavor);
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_xor_jit_request(req: XorJitComputeReq, context: &mut WorkerContext) {
+ let scratch = context
+ .xor_jit_bitplane_scratch
+ .get_or_insert_with(|| XorJitBitplaneScratch::new().expect("allocate xor-jit scratch"));
+ let method_info = req.method_info();
+ let (inputs_prefetched_per_invok, input_prefetch_out_offset) =
+ xor_jit_input_prefetch_schedule(req);
+ let input_base = req.input as *const u8;
+ let output_base = req.output as *mut u8;
+ let coeffs_base = req.coeffs as *const u16;
+ let out_non_zero_base = req.out_non_zero as *const u8;
+
+ for round in 0..req.num_chunks {
+ let proc_size = (req.len - round * req.chunk_size).min(req.chunk_size);
+ debug_assert!(proc_size.is_multiple_of(method_info.stride));
+ let final_round = round + 1 == req.num_chunks;
+ let input_ptr = unsafe { input_base.add(round * req.chunk_size * req.input_grouping) };
+ let round_output_base =
+ unsafe { output_base.add(round * req.num_outputs * req.chunk_size) };
+
+ for output_idx in 0..req.num_outputs {
+ let output_ptr = unsafe { round_output_base.add(output_idx * proc_size) };
+ if !req.add {
unsafe {
- process_slice_multiply_add_prepared_avx2(input, output, prepared, &coeff.split);
+ std::ptr::write_bytes(output_ptr, 0, proc_size);
}
- return;
+ }
+ let coeff_ptr = unsafe { coeffs_base.add(output_idx * req.input_grouping) };
+ let out_non_zero = unsafe { *out_non_zero_base.add(output_idx) != 0 };
+ let input_prefetch = if !final_round && output_idx >= input_prefetch_out_offset {
+ Some(unsafe {
+ input_base.add(
+ (round + 1) * req.chunk_size * req.input_grouping
+ + ((inputs_prefetched_per_invok
+ * (output_idx - input_prefetch_out_offset)
+ * proc_size)
+ >> XOR_JIT_PREFETCH_MAX_FACTOR),
+ )
+ })
+ } else {
+ None
+ };
+ let output_prefetch = xor_jit_output_prefetch_ptr(req, round, output_idx);
+
+ match xor_jit_worker_branch(
+ final_round,
+ output_idx + 1 == req.num_outputs,
+ out_non_zero,
+ ) {
+ XorJitWorkerBranch::MulAddMultiPackpf => xor_jit_mul_add_multi_packpf_ptr(
+ req,
+ coeff_ptr,
+ input_ptr,
+ output_ptr,
+ proc_size,
+ input_prefetch,
+ output_prefetch,
+ scratch,
+ ),
+ XorJitWorkerBranch::AddMultiPackpf => xor_jit_add_multi_packpf_ptr(
+ req,
+ input_ptr,
+ output_ptr,
+ proc_size,
+ input_prefetch,
+ output_prefetch,
+ ),
+ XorJitWorkerBranch::MulAddMultiPacked => xor_jit_mul_add_multi_packed_ptr(
+ req, coeff_ptr, input_ptr, output_ptr, proc_size, scratch,
+ ),
}
}
+ }
+}
- process_slice_multiply_add(input, output, &coeff.split);
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn xor_jit_input_prefetch_schedule(req: XorJitComputeReq) -> (usize, usize) {
+ let method_info = req.method_info();
+ let mut inputs_prefetched_per_invok = req.num_inputs / method_info.ideal_input_multiple;
+ let mut input_prefetch_out_offset = req.num_outputs.saturating_sub(1);
+ let prefetch_downscale = method_info.prefetch_downscale;
+
+ if inputs_prefetched_per_invok > (1usize << prefetch_downscale) {
+ inputs_prefetched_per_invok -= 1usize << prefetch_downscale;
+ inputs_prefetched_per_invok <<= XOR_JIT_PREFETCH_MAX_FACTOR - prefetch_downscale;
+ input_prefetch_out_offset =
+ (req.num_inputs << XOR_JIT_PREFETCH_MAX_FACTOR).div_ceil(inputs_prefetched_per_invok);
+ input_prefetch_out_offset = req.num_outputs.saturating_sub(input_prefetch_out_offset);
}
- #[cfg(not(target_arch = "x86_64"))]
- #[inline]
- fn process_input_add(input: &[u8], output: &mut [u8], coeff: &Gf16Coeff) {
- process_slice_multiply_add(input, output, &coeff.split);
+ (inputs_prefetched_per_invok, input_prefetch_out_offset)
+}
+
+#[cfg(target_arch = "x86_64")]
+fn xor_jit_mul_add_multi_packpf_ptr(
+ req: XorJitComputeReq,
+ coeff_ptr: *const u16,
+ input_ptr: *const u8,
+ output: *mut u8,
+ round_len: usize,
+ input_prefetch: Option<*const u8>,
+ output_prefetch: Option<*const u8>,
+ scratch: &mut XorJitBitplaneScratch,
+) {
+ unsafe {
+ xor_jit_multi_region_packpf_ptr(
+ req,
+ scratch,
+ coeff_ptr,
+ input_ptr,
+ req.num_inputs,
+ output,
+ round_len,
+ input_prefetch,
+ output_prefetch,
+ );
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn xor_jit_mul_add_multi_packed_ptr(
+ req: XorJitComputeReq,
+ coeff_ptr: *const u16,
+ input_ptr: *const u8,
+ output: *mut u8,
+ round_len: usize,
+ scratch: &mut XorJitBitplaneScratch,
+) {
+ unsafe {
+ xor_jit_multi_region_ptr(
+ scratch,
+ coeff_ptr,
+ input_ptr,
+ req.num_inputs,
+ output,
+ round_len,
+ );
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+unsafe fn xor_jit_multi_region_ptr(
+ scratch: &mut XorJitBitplaneScratch,
+ coeffs: *const u16,
+ src: *const u8,
+ regions: usize,
+ output: *mut u8,
+ len: usize,
+) {
+ debug_assert!(regions > 0);
+ for region in 0..regions {
+ let coefficient = unsafe { *coeffs.add(region) };
+ let input = unsafe { src.add(region * len) };
+ unsafe { scratch.multiply_add_ptr_coefficient(coefficient, input, output, len) };
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+unsafe fn xor_jit_multi_region_packpf_ptr(
+ req: XorJitComputeReq,
+ scratch: &mut XorJitBitplaneScratch,
+ coeffs: *const u16,
+ src: *const u8,
+ regions: usize,
+ output: *mut u8,
+ len: usize,
+ prefetch_in: Option<*const u8>,
+ prefetch_out: Option<*const u8>,
+) {
+ debug_assert!(regions > 0);
+
+ let prefetch_plan = xor_jit_create_prefetch_plan(req.method_info(), len);
+ let mut region = 0usize;
+ let mut prefetch_ptr = prefetch_out;
+ let mut output_pf_rounds = prefetch_plan.output_prefetch_rounds;
+ while region < regions && output_pf_rounds > 0 {
+ let coefficient = unsafe { *coeffs.add(region) };
+ let input = unsafe { src.add(region * len) };
+ let current_prefetch = prefetch_ptr.unwrap_or(std::ptr::null());
+ unsafe {
+ scratch.multiply_add_ptr_coefficient_prefetch(
+ coefficient,
+ input,
+ output,
+ len,
+ current_prefetch,
+ );
+ }
+ region += 1;
+ output_pf_rounds -= 1;
+ prefetch_ptr = prefetch_ptr.map(|ptr| ptr.wrapping_add(prefetch_plan.pf_len));
+ }
+
+ if let Some(mut prefetch_ptr) = prefetch_in {
+ while region < regions {
+ let coefficient = unsafe { *coeffs.add(region) };
+ let input = unsafe { src.add(region * len) };
+ unsafe {
+ scratch.multiply_add_ptr_coefficient_prefetch(
+ coefficient,
+ input,
+ output,
+ len,
+ prefetch_ptr,
+ );
+ }
+ region += 1;
+ prefetch_ptr = prefetch_ptr.wrapping_add(prefetch_plan.pf_len);
+ }
+ } else {
+ unsafe {
+ xor_jit_multi_region_ptr(
+ scratch,
+ coeffs.add(region),
+ src.add(region * len),
+ regions - region,
+ output,
+ len,
+ );
+ }
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn xor_jit_add_multi_packpf_ptr(
+ req: XorJitComputeReq,
+ input_ptr: *const u8,
+ output: *mut u8,
+ round_len: usize,
+ input_prefetch: Option<*const u8>,
+ output_prefetch: Option<*const u8>,
+) {
+ xor_packed_multi_region_v16i1_ptr(
+ input_ptr,
+ req.num_inputs,
+ output,
+ round_len,
+ req.method_info(),
+ input_prefetch,
+ output_prefetch,
+ );
+}
+
+#[cfg(target_arch = "x86_64")]
+#[cfg_attr(not(test), allow(dead_code))]
+fn xor_jit_output_prefetch_ptr(
+ req: XorJitComputeReq,
+ round: usize,
+ output_idx: usize,
+) -> Option<*const u8> {
+ if round + 1 == req.num_chunks && output_idx + 1 == req.num_outputs {
+ return None;
+ }
+
+ let proc_size = (req.len - round * req.chunk_size).min(req.chunk_size);
+ Some((req.output as *const u8).wrapping_add(
+ output_idx * proc_size + round * req.num_outputs * req.chunk_size + proc_size,
+ ))
+}
+
+#[cfg(all(target_arch = "x86_64", test))]
+fn xor_jit_input_prefetch_ptr(
+ req: XorJitComputeReq,
+ round: usize,
+ output_idx: usize,
+) -> Option<*const u8> {
+ if round + 1 >= req.num_chunks {
+ return None;
+ }
+
+ let (inputs_prefetched_per_invok, input_prefetch_out_offset) =
+ xor_jit_input_prefetch_schedule(req);
+ if output_idx < input_prefetch_out_offset {
+ return None;
+ }
+
+ let proc_size = (req.len - round * req.chunk_size).min(req.chunk_size);
+ Some((req.input as *const u8).wrapping_add(
+ (round + 1) * req.chunk_size * req.input_grouping
+ + ((inputs_prefetched_per_invok
+ * (output_idx - input_prefetch_out_offset)
+ * proc_size)
+ >> XOR_JIT_PREFETCH_MAX_FACTOR),
+ ))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[allow(clippy::too_many_arguments)]
+fn process_batch_add_avx2_xor_jit_x4_outputs(
+ job: ComputeJob,
+ recovery_a: usize,
+ output_a: &mut [u8],
+ recovery_b: usize,
+ output_b: &mut [u8],
+ recovery_c: usize,
+ output_c: &mut [u8],
+ recovery_d: usize,
+ output_d: &mut [u8],
+ flavor: XorJitFlavor,
+) {
+ let mut batch_idx = 0;
+ while batch_idx + 3 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let source_c = unsafe { *(job.source_indices as *const usize).add(batch_idx + 2) };
+ let source_d = unsafe { *(job.source_indices as *const usize).add(batch_idx + 3) };
+ let coeff_a0 = coeff_for(job, recovery_a, source_a);
+ let coeff_b0 = coeff_for(job, recovery_a, source_b);
+ let coeff_c0 = coeff_for(job, recovery_a, source_c);
+ let coeff_d0 = coeff_for(job, recovery_a, source_d);
+ let coeff_a1 = coeff_for(job, recovery_b, source_a);
+ let coeff_b1 = coeff_for(job, recovery_b, source_b);
+ let coeff_c1 = coeff_for(job, recovery_b, source_c);
+ let coeff_d1 = coeff_for(job, recovery_b, source_d);
+ let coeff_a2 = coeff_for(job, recovery_c, source_a);
+ let coeff_b2 = coeff_for(job, recovery_c, source_b);
+ let coeff_c2 = coeff_for(job, recovery_c, source_c);
+ let coeff_d2 = coeff_for(job, recovery_c, source_d);
+ let coeff_a3 = coeff_for(job, recovery_d, source_a);
+ let coeff_b3 = coeff_for(job, recovery_d, source_b);
+ let coeff_c3 = coeff_for(job, recovery_d, source_c);
+ let coeff_d3 = coeff_for(job, recovery_d, source_d);
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+ let input_c = input_segment(job, batch_idx + 2);
+ let input_d = input_segment(job, batch_idx + 3);
+
+ match (
+ &coeff_a0.xor_jit,
+ &coeff_b0.xor_jit,
+ &coeff_c0.xor_jit,
+ &coeff_d0.xor_jit,
+ &coeff_a1.xor_jit,
+ &coeff_b1.xor_jit,
+ &coeff_c1.xor_jit,
+ &coeff_d1.xor_jit,
+ &coeff_a2.xor_jit,
+ &coeff_b2.xor_jit,
+ &coeff_c2.xor_jit,
+ &coeff_d2.xor_jit,
+ &coeff_a3.xor_jit,
+ &coeff_b3.xor_jit,
+ &coeff_c3.xor_jit,
+ &coeff_d3.xor_jit,
+ ) {
+ (
+ Some(prepared_a0),
+ Some(prepared_b0),
+ Some(prepared_c0),
+ Some(prepared_d0),
+ Some(prepared_a1),
+ Some(prepared_b1),
+ Some(prepared_c1),
+ Some(prepared_d1),
+ Some(prepared_a2),
+ Some(prepared_b2),
+ Some(prepared_c2),
+ Some(prepared_d2),
+ Some(prepared_a3),
+ Some(prepared_b3),
+ Some(prepared_c3),
+ Some(prepared_d3),
+ ) => unsafe {
+ process_slices_multiply_add_xor_jit_x4_inputs_x4_outputs(
+ input_a,
+ input_b,
+ input_c,
+ input_d,
+ prepared_a0,
+ prepared_b0,
+ prepared_c0,
+ prepared_d0,
+ output_a,
+ prepared_a1,
+ prepared_b1,
+ prepared_c1,
+ prepared_d1,
+ output_b,
+ prepared_a2,
+ prepared_b2,
+ prepared_c2,
+ prepared_d2,
+ output_c,
+ prepared_a3,
+ prepared_b3,
+ prepared_c3,
+ prepared_d3,
+ output_d,
+ flavor,
+ );
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 4;
+ }
+
+ if batch_idx < job.batch_len {
+ process_batch_add_avx2_xor_jit_x2_outputs(
+ ComputeJob {
+ batch_len: job.batch_len - batch_idx,
+ input_base: unsafe {
+ (job.input_base as *const u8).add(batch_idx * job.aligned_chunk_len) as usize
+ },
+ source_indices: unsafe {
+ (job.source_indices as *const usize).add(batch_idx) as usize
+ },
+ ..job
+ },
+ recovery_a,
+ output_a,
+ recovery_b,
+ output_b,
+ flavor,
+ );
+ process_batch_add_avx2_xor_jit_x2_outputs(
+ ComputeJob {
+ batch_len: job.batch_len - batch_idx,
+ input_base: unsafe {
+ (job.input_base as *const u8).add(batch_idx * job.aligned_chunk_len) as usize
+ },
+ source_indices: unsafe {
+ (job.source_indices as *const usize).add(batch_idx) as usize
+ },
+ ..job
+ },
+ recovery_c,
+ output_c,
+ recovery_d,
+ output_d,
+ flavor,
+ );
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_batch_add_avx2_xor_jit_x2_outputs(
+ job: ComputeJob,
+ recovery_a: usize,
+ output_a: &mut [u8],
+ recovery_b: usize,
+ output_b: &mut [u8],
+ flavor: XorJitFlavor,
+) {
+ let mut batch_idx = 0;
+ while batch_idx + 3 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let source_c = unsafe { *(job.source_indices as *const usize).add(batch_idx + 2) };
+ let source_d = unsafe { *(job.source_indices as *const usize).add(batch_idx + 3) };
+ let coeff_a0 = coeff_for(job, recovery_a, source_a);
+ let coeff_b0 = coeff_for(job, recovery_a, source_b);
+ let coeff_c0 = coeff_for(job, recovery_a, source_c);
+ let coeff_d0 = coeff_for(job, recovery_a, source_d);
+ let coeff_a1 = coeff_for(job, recovery_b, source_a);
+ let coeff_b1 = coeff_for(job, recovery_b, source_b);
+ let coeff_c1 = coeff_for(job, recovery_b, source_c);
+ let coeff_d1 = coeff_for(job, recovery_b, source_d);
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+ let input_c = input_segment(job, batch_idx + 2);
+ let input_d = input_segment(job, batch_idx + 3);
+
+ match (
+ &coeff_a0.xor_jit,
+ &coeff_b0.xor_jit,
+ &coeff_c0.xor_jit,
+ &coeff_d0.xor_jit,
+ &coeff_a1.xor_jit,
+ &coeff_b1.xor_jit,
+ &coeff_c1.xor_jit,
+ &coeff_d1.xor_jit,
+ ) {
+ (
+ Some(prepared_a0),
+ Some(prepared_b0),
+ Some(prepared_c0),
+ Some(prepared_d0),
+ Some(prepared_a1),
+ Some(prepared_b1),
+ Some(prepared_c1),
+ Some(prepared_d1),
+ ) => unsafe {
+ process_slices_multiply_add_xor_jit_x4_inputs_x2_outputs(
+ input_a,
+ input_b,
+ input_c,
+ input_d,
+ prepared_a0,
+ prepared_b0,
+ prepared_c0,
+ prepared_d0,
+ output_a,
+ prepared_a1,
+ prepared_b1,
+ prepared_c1,
+ prepared_d1,
+ output_b,
+ flavor,
+ );
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 4;
+ }
+
+ while batch_idx + 1 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let coeff_a0 = coeff_for(job, recovery_a, source_a);
+ let coeff_b0 = coeff_for(job, recovery_a, source_b);
+ let coeff_a1 = coeff_for(job, recovery_b, source_a);
+ let coeff_b1 = coeff_for(job, recovery_b, source_b);
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+
+ match (
+ &coeff_a0.xor_jit,
+ &coeff_b0.xor_jit,
+ &coeff_a1.xor_jit,
+ &coeff_b1.xor_jit,
+ ) {
+ (Some(prepared_a0), Some(prepared_b0), Some(prepared_a1), Some(prepared_b1)) => unsafe {
+ process_slices_multiply_add_xor_jit_x2(
+ input_a,
+ prepared_a0,
+ input_b,
+ prepared_b0,
+ output_a,
+ flavor,
+ );
+ process_slices_multiply_add_xor_jit_x2(
+ input_a,
+ prepared_a1,
+ input_b,
+ prepared_b1,
+ output_b,
+ flavor,
+ );
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 2;
+ }
+
+ while batch_idx < job.batch_len {
+ let source_idx = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let coeff_a = coeff_for(job, recovery_a, source_idx);
+ let coeff_b = coeff_for(job, recovery_b, source_idx);
+ let input = input_segment(job, batch_idx);
+ match (&coeff_a.xor_jit, &coeff_b.xor_jit) {
+ (Some(prepared_a), Some(prepared_b)) => unsafe {
+ process_slice_multiply_add_xor_jit(input, output_a, prepared_a, flavor);
+ process_slice_multiply_add_xor_jit(input, output_b, prepared_b, flavor);
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 1;
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_batch_add_avx2_xor_jit(
+ job: ComputeJob,
+ recovery_idx: usize,
+ output: &mut [u8],
+ flavor: XorJitFlavor,
+) {
+ let mut batch_idx = 0;
+ while batch_idx + 3 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let source_c = unsafe { *(job.source_indices as *const usize).add(batch_idx + 2) };
+ let source_d = unsafe { *(job.source_indices as *const usize).add(batch_idx + 3) };
+ let coeff_a = coeff_for(job, recovery_idx, source_a);
+ let coeff_b = coeff_for(job, recovery_idx, source_b);
+ let coeff_c = coeff_for(job, recovery_idx, source_c);
+ let coeff_d = coeff_for(job, recovery_idx, source_d);
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+ let input_c = input_segment(job, batch_idx + 2);
+ let input_d = input_segment(job, batch_idx + 3);
+
+ match (
+ &coeff_a.xor_jit,
+ &coeff_b.xor_jit,
+ &coeff_c.xor_jit,
+ &coeff_d.xor_jit,
+ ) {
+ (Some(prepared_a), Some(prepared_b), Some(prepared_c), Some(prepared_d)) => unsafe {
+ process_slices_multiply_add_xor_jit_x4(
+ input_a, prepared_a, input_b, prepared_b, input_c, prepared_c, input_d,
+ prepared_d, output, flavor,
+ );
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 4;
+ }
+
+ while batch_idx + 1 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let coeff_a = coeff_for(job, recovery_idx, source_a);
+ let coeff_b = coeff_for(job, recovery_idx, source_b);
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+
+ match (&coeff_a.xor_jit, &coeff_b.xor_jit) {
+ (Some(prepared_a), Some(prepared_b)) => unsafe {
+ process_slices_multiply_add_xor_jit_x2(
+ input_a, prepared_a, input_b, prepared_b, output, flavor,
+ );
+ },
+ _ => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 2;
+ }
+
+ while batch_idx < job.batch_len {
+ let source_idx = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let coeff = coeff_for(job, recovery_idx, source_idx);
+ let input = input_segment(job, batch_idx);
+ match &coeff.xor_jit {
+ Some(prepared) => unsafe {
+ process_slice_multiply_add_xor_jit(input, output, prepared, flavor);
+ },
+ None => panic!("XOR-JIT create backend missing prepared coefficient"),
+ }
+ batch_idx += 1;
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+fn process_batch_add_avx2_pshufb(job: ComputeJob, recovery_idx: usize, output: &mut [u8]) {
+ let mut batch_idx = 0;
+ while PSHUFB_PACKED_INPUTS >= 4 && batch_idx + 3 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let source_c = unsafe { *(job.source_indices as *const usize).add(batch_idx + 2) };
+ let source_d = unsafe { *(job.source_indices as *const usize).add(batch_idx + 3) };
+ let coeff_a = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_a,
+ job.source_count,
+ ))
+ };
+ let coeff_b = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_b,
+ job.source_count,
+ ))
+ };
+ let coeff_c = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_c,
+ job.source_count,
+ ))
+ };
+ let coeff_d = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_d,
+ job.source_count,
+ ))
+ };
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+ let input_c = input_segment(job, batch_idx + 2);
+ let input_d = input_segment(job, batch_idx + 3);
+
+ match (&coeff_a.avx2, &coeff_b.avx2, &coeff_c.avx2, &coeff_d.avx2) {
+ (Some(prepared_a), Some(prepared_b), Some(prepared_c), Some(prepared_d)) => unsafe {
+ process_slices_multiply_add_prepared_avx2_x4(
+ input_a,
+ prepared_a,
+ &coeff_a.split,
+ input_b,
+ prepared_b,
+ &coeff_b.split,
+ input_c,
+ prepared_c,
+ &coeff_c.split,
+ input_d,
+ prepared_d,
+ &coeff_d.split,
+ output,
+ );
+ },
+ _ => {
+ process_slice_multiply_add(input_a, output, &coeff_a.split);
+ process_slice_multiply_add(input_b, output, &coeff_b.split);
+ process_slice_multiply_add(input_c, output, &coeff_c.split);
+ process_slice_multiply_add(input_d, output, &coeff_d.split);
+ }
+ }
+ batch_idx += 4;
+ }
+
+ while PSHUFB_PACKED_INPUTS >= 2 && batch_idx + 1 < job.batch_len {
+ let source_a = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let source_b = unsafe { *(job.source_indices as *const usize).add(batch_idx + 1) };
+ let coeff_a = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_a,
+ job.source_count,
+ ))
+ };
+ let coeff_b = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_b,
+ job.source_count,
+ ))
+ };
+ let input_a = input_segment(job, batch_idx);
+ let input_b = input_segment(job, batch_idx + 1);
+
+ match (&coeff_a.avx2, &coeff_b.avx2) {
+ (Some(prepared_a), Some(prepared_b)) => unsafe {
+ process_slices_multiply_add_prepared_avx2_x2(
+ input_a,
+ prepared_a,
+ &coeff_a.split,
+ input_b,
+ prepared_b,
+ &coeff_b.split,
+ output,
+ );
+ },
+ _ => {
+ process_slice_multiply_add(input_a, output, &coeff_a.split);
+ process_slice_multiply_add(input_b, output, &coeff_b.split);
+ }
+ }
+ batch_idx += 2;
+ }
+
+ while batch_idx < job.batch_len {
+ let source_idx = unsafe { *(job.source_indices as *const usize).add(batch_idx) };
+ let coeff = unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_idx,
+ job.source_count,
+ ))
+ };
+ let input = input_segment(job, batch_idx);
+ match &coeff.avx2 {
+ Some(prepared) => unsafe {
+ process_slice_multiply_add_prepared_avx2(input, output, prepared, &coeff.split);
+ },
+ None => process_slice_multiply_add(input, output, &coeff.split),
+ }
+ batch_idx += 1;
+ }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn input_segment(job: ComputeJob, batch_idx: usize) -> &'static [u8] {
+ let start = batch_idx * job.aligned_chunk_len + job.segment_start;
+ unsafe { std::slice::from_raw_parts((job.input_base as *const u8).add(start), job.segment_len) }
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn coeff_for(job: ComputeJob, recovery_idx: usize, source_idx: usize) -> &'static CreateCoeff {
+ unsafe {
+ &*(job.coeffs as *const CreateCoeff).add(gf_coeff_index(
+ recovery_idx,
+ source_idx,
+ job.source_count,
+ ))
}
}
@@ -338,32 +2367,135 @@ pub fn gf_coeff_index(recovery_idx: usize, source_idx: usize, source_count: usiz
recovery_idx * source_count + source_idx
}
+#[inline]
+fn input_grouping(source_count: usize, method: CreateGf16Method) -> usize {
+ let default = DEFAULT_INPUT_GROUPING;
+ let small = source_count.div_ceil(2).max(1);
+ let requested = if small < default { small } else { default };
+ let multiple = method.ideal_input_multiple();
+ align_up(requested, multiple)
+ .max(multiple)
+ .min(source_count.max(1))
+}
+
+#[cfg(target_arch = "x86_64")]
+#[inline]
+fn xor_jit_request_capacity(worker_count: usize) -> usize {
+ // Turbo dispatches at most two requests per worker for one slice:
+ // one leftover-split request in the first phase and one full-chunk
+ // request in the second phase.
+ worker_count.max(1) * 2
+}
+
+#[inline]
+fn max_compute_jobs(
+ max_chunk_len: usize,
+ recovery_count: usize,
+ worker_count: usize,
+ method: CreateGf16Method,
+) -> usize {
+ #[cfg(target_arch = "x86_64")]
+ if method.xor_jit_flavor().is_some() {
+ return xor_jit_request_capacity(worker_count);
+ }
+
+ let segment_len = align_down(
+ method
+ .ideal_segment_len()
+ .min(max_chunk_len.max(AVX2_ALIGNMENT))
+ .max(AVX2_ALIGNMENT),
+ AVX2_ALIGNMENT,
+ )
+ .max(AVX2_ALIGNMENT);
+ let segment_count = max_chunk_len.max(1).div_ceil(segment_len);
+ let output_groups = worker_count.min(recovery_count).max(1);
+ (segment_count * output_groups).max(1)
+}
+
+#[inline]
+fn align_up(value: usize, alignment: usize) -> usize {
+ if value == 0 {
+ 0
+ } else {
+ value.div_ceil(alignment) * alignment
+ }
+}
+
+#[inline]
+fn align_down(value: usize, alignment: usize) -> usize {
+ value & !(alignment - 1)
+}
+
#[cfg(test)]
mod tests {
use super::*;
use crate::reed_solomon::RecoveryBlockEncoder;
+ use std::sync::{Mutex, OnceLock};
+
+ fn env_lock() -> &'static Mutex<()> {
+ static LOCK: OnceLock> = OnceLock::new();
+ LOCK.get_or_init(|| Mutex::new(()))
+ }
+
+ fn deterministic_inputs(source_count: usize, block_size: usize) -> Vec> {
+ (0..source_count)
+ .map(|src| {
+ (0..block_size)
+ .map(|byte| (src * 31 + byte * 17 + (byte / 7)) as u8)
+ .collect::>()
+ })
+ .collect::>()
+ }
+
+ fn assert_forced_backend_matches_encoder(method: &str, expected_method: CreateGf16Method) {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", method);
+
+ let block_size = 1024;
+ let source_count = 12;
+ let recovery_count = 5;
+ let encoder = RecoveryBlockEncoder::new(block_size, source_count);
+ let inputs = deterministic_inputs(source_count, block_size);
+ let mut backend =
+ CreateRecoveryBackend::new(encoder.base_values(), 3, recovery_count, block_size, 4);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != expected_method {
+ return;
+ }
+
+ let mut recovery_blocks = backend.recovery_blocks(block_size);
+ backend.begin_chunk(block_size);
+ inputs
+ .iter()
+ .enumerate()
+ .for_each(|(idx, input)| backend.add_input(idx, input));
+ assert!(backend.finish_chunk(&mut recovery_blocks, block_size));
+
+ recovery_blocks
+ .iter()
+ .for_each(|(exponent, recovery_data)| {
+ let refs = inputs.iter().map(Vec::as_slice).collect::>();
+ let expected = encoder.encode_recovery_block(*exponent, &refs).unwrap();
+ assert_eq!(recovery_data, &expected);
+ });
+ }
#[test]
fn backend_output_matches_encoder_for_partial_batch() {
let block_size = 64;
let source_count = 5;
let encoder = RecoveryBlockEncoder::new(block_size, source_count);
- let inputs = (0..source_count)
- .map(|src| {
- (0..block_size)
- .map(|byte| (src * 17 + byte) as u8)
- .collect::>()
- })
- .collect::>();
+ let inputs = deterministic_inputs(source_count, block_size);
- let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 3, block_size);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 3, block_size, 2);
let mut recovery_blocks = backend.recovery_blocks(block_size);
backend.begin_chunk(block_size);
inputs
.iter()
.enumerate()
.for_each(|(idx, input)| backend.add_input(idx, input));
- backend.finish_chunk(&mut recovery_blocks, block_size);
+ assert!(backend.finish_chunk(&mut recovery_blocks, block_size));
recovery_blocks
.iter()
@@ -374,10 +2506,42 @@ mod tests {
});
}
+ #[test]
+ fn forced_pshufb_backend_matches_encoder_for_full_batch() {
+ assert_forced_backend_matches_encoder("pshufb", CreateGf16Method::Avx2PshufbPrepared);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn forced_xor_jit_backend_matches_encoder_for_full_batch() {
+ assert_forced_backend_matches_encoder("xor-jit", CreateGf16Method::Avx2XorJit);
+ }
+
+ #[test]
+ #[cfg(target_arch = "x86_64")]
+ fn forced_xor_jit_backend_uses_bitplane_without_legacy_fallback() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let encoder = RecoveryBlockEncoder::new(1024, 12);
+ let backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 5, 1024, 4);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ assert!(backend.coeffs.iter().all(|coeff| coeff.xor_jit.is_some()));
+ assert!(backend
+ .coeffs
+ .iter()
+ .all(|coeff| coeff.xor_jit_bitplane.is_some()));
+ }
+
#[test]
fn backend_reuses_fixed_transfer_buffers() {
let encoder = RecoveryBlockEncoder::new(64, 2);
- let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 7, 1, 64);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 7, 1, 64, 1);
backend.begin_chunk(32);
let first = backend.prepare_transfer_buffer(0).as_ptr();
let second = backend.prepare_transfer_buffer(1).as_ptr();
@@ -388,4 +2552,697 @@ mod tests {
assert_eq!(first as usize % 32, 0);
assert_eq!(second as usize % 32, 0);
}
+
+ #[test]
+ fn env_method_override_selects_scalar() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "scalar");
+ let encoder = RecoveryBlockEncoder::new(64, 2);
+ let backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 1, 64, 1);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+ assert_eq!(backend.selected_method(), CreateGf16Method::Scalar);
+ }
+
+ #[test]
+ fn recovery_vectors_keep_exact_reserved_capacity() {
+ let encoder = RecoveryBlockEncoder::new(96, 4);
+ let backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 2, 32, 1);
+ let blocks = backend.recovery_blocks(96);
+ assert!(blocks.iter().all(|(_, bytes)| bytes.capacity() == 96));
+ }
+
+ #[test]
+ fn input_grouping_handles_non_power_of_two_multiple() {
+ assert_eq!(align_up(12, 12), 12);
+ assert_eq!(align_up(13, 12), 24);
+ #[cfg(target_arch = "x86_64")]
+ assert_eq!(input_grouping(256, CreateGf16Method::Avx2XorJit), 12);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_runtime_chunk_len_matches_turbo_calc_chunksize() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::remove_var(XOR_JIT_SEGMENT_LEN_ENV);
+ assert_eq!(
+ xor_jit_runtime_chunk_len(1024 * 1024, 1, CreateGf16Method::Avx2XorJit),
+ 131_584
+ );
+ assert_eq!(
+ xor_jit_runtime_chunk_len(1024 * 1024, 16, CreateGf16Method::Avx2XorJit),
+ 66_048
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_bitplane_layout_offsets_are_segment_major() {
+ let layout = XorJitBitplaneLayout::new(1024, 512, 3, 4, xor_jit_create_avx2_method_info());
+ let aligned_current_slice_size = layout.aligned_slice_len;
+ let first_slice_offset = layout.slice_offset(0);
+ let second_slice_offset = layout.slice_offset(1);
+ let first_proc_size =
+ layout.segment_len_for(first_slice_offset, aligned_current_slice_size);
+ let second_proc_size =
+ layout.segment_len_for(second_slice_offset, aligned_current_slice_size);
+
+ assert_eq!(
+ layout.input_offset(first_slice_offset, 0, first_proc_size),
+ 0
+ );
+ assert_eq!(
+ layout.input_offset(first_slice_offset, 1, first_proc_size),
+ 512
+ );
+ assert_eq!(
+ layout.input_offset(first_slice_offset, 2, first_proc_size),
+ 1024
+ );
+ assert_eq!(
+ layout.input_offset(second_slice_offset, 0, second_proc_size),
+ 1536
+ );
+ assert_eq!(
+ layout.input_offset(second_slice_offset, 1, second_proc_size),
+ 2048
+ );
+
+ assert_eq!(
+ layout.output_offset(first_slice_offset, 0, first_proc_size),
+ 0
+ );
+ assert_eq!(
+ layout.output_offset(first_slice_offset, 1, first_proc_size),
+ 512
+ );
+ assert_eq!(
+ layout.output_offset(first_slice_offset, 3, first_proc_size),
+ 1536
+ );
+ assert_eq!(
+ layout.output_offset(second_slice_offset, 0, second_proc_size),
+ 2048
+ );
+ assert_eq!(
+ layout.output_offset(second_slice_offset, 1, second_proc_size),
+ 2560
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_bitplane_layout_offsets_do_not_overlap() {
+ use std::collections::HashSet;
+
+ let layout = XorJitBitplaneLayout::new(2048, 512, 12, 5, xor_jit_create_avx2_method_info());
+ let aligned_current_slice_size = layout.aligned_slice_len;
+ let segment_count = layout.segment_count_for(aligned_current_slice_size);
+ let mut input_offsets = HashSet::new();
+ for segment_idx in 0..segment_count {
+ let slice_offset = layout.slice_offset(segment_idx);
+ let proc_size = layout.segment_len_for(slice_offset, aligned_current_slice_size);
+ for batch_idx in 0..layout.input_batch_size {
+ assert!(input_offsets.insert(layout.input_offset(
+ slice_offset,
+ batch_idx,
+ proc_size
+ )));
+ }
+ }
+ assert_eq!(input_offsets.len(), segment_count * layout.input_batch_size);
+
+ let mut output_offsets = HashSet::new();
+ for segment_idx in 0..segment_count {
+ let slice_offset = layout.slice_offset(segment_idx);
+ let proc_size = layout.segment_len_for(slice_offset, aligned_current_slice_size);
+ for recovery_idx in 0..layout.recovery_count {
+ assert!(output_offsets.insert(layout.output_offset(
+ slice_offset,
+ recovery_idx,
+ proc_size
+ )));
+ }
+ }
+ assert_eq!(output_offsets.len(), segment_count * layout.recovery_count);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_bitplane_layout_handles_partial_final_segment() {
+ let layout = XorJitBitplaneLayout::new(1024, 1024, 3, 2, xor_jit_create_avx2_method_info());
+
+ assert_eq!(layout.segment_count_for(layout.aligned_slice_len), 2);
+ assert_eq!(layout.slice_offset(0), 0);
+ assert_eq!(layout.slice_offset(1), 1024);
+ assert_eq!(layout.segment_len_for(0, layout.aligned_slice_len), 1024);
+ assert_eq!(layout.segment_len_for(1024, layout.aligned_slice_len), 512);
+ assert_eq!(layout.input_storage_len(), 3 * 1024 + 3 * 512);
+ assert_eq!(layout.output_storage_len(), 2 * 1024 + 2 * 512);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_localized_request_build_matches_turbo_single_thread() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let block_size = 1024 * 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 8, block_size, 1);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ let request_count = backend.build_xor_jit_requests(0, false);
+ let req = backend.xor_jit_req_storage[0];
+ let out_non_zero = unsafe { std::slice::from_raw_parts(req.out_non_zero as *const u8, 8) };
+ let staging = &backend.staging[0];
+
+ assert_eq!(request_count, 1);
+ assert_eq!(req.method, CreateGf16Method::Avx2XorJit);
+ assert_eq!(req.input, staging.inputs.as_ptr() as usize);
+ assert_eq!(req.output, backend.output_chunks.as_ptr() as usize);
+ assert_eq!(req.coeffs, staging.xor_jit_coeffs.as_ptr() as usize);
+ assert_eq!(
+ req.out_non_zero,
+ staging.xor_jit_out_non_zero.as_ptr() as usize
+ );
+ assert_eq!(req.num_inputs, 3);
+ assert_eq!(req.input_grouping, 12);
+ assert_eq!(req.chunk_size, 131_584);
+ assert_eq!(req.num_chunks, 8);
+ assert_eq!(req.len, 1_049_088);
+ assert_eq!(req.num_outputs, 8);
+ assert!(!req.add);
+ assert_eq!(out_non_zero[0], 0);
+ assert!(out_non_zero[1..].iter().all(|&value| value == 1));
+
+ backend.build_xor_jit_requests(0, true);
+ assert!(backend.xor_jit_req_storage[0].add);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_localized_request_build_matches_turbo_leftover_split() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let block_size = 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, block_size, 4);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ let request_count = backend.build_xor_jit_requests(0, false);
+ let requests = &backend.xor_jit_req_storage[..request_count];
+ let staging = &backend.staging[0];
+ let layout = backend
+ .xor_jit_layout
+ .expect("XOR-JIT bitplane layout initialized");
+ let req_len = layout.aligned_current_slice_size(block_size);
+ let expected_output_starts = [0usize, 2, 4, 5];
+ let expected_output_counts = [2usize, 2, 1, 2];
+
+ assert_eq!(request_count, 4);
+ assert_eq!(requests.iter().map(|req| req.num_outputs).sum::(), 7);
+
+ for (idx, req) in requests.iter().enumerate() {
+ let output_start = expected_output_starts[idx];
+ assert_eq!(req.method, CreateGf16Method::Avx2XorJit);
+ assert_eq!(req.input, staging.inputs.as_ptr() as usize);
+ assert_eq!(
+ req.output,
+ backend.output_chunks.as_ptr() as usize + output_start * req_len
+ );
+ assert_eq!(
+ req.coeffs,
+ staging.xor_jit_coeffs.as_ptr() as usize
+ + output_start * backend.input_grouping * std::mem::size_of::()
+ );
+ assert_eq!(
+ req.out_non_zero,
+ staging.xor_jit_out_non_zero.as_ptr() as usize + output_start
+ );
+ assert_eq!(req.num_inputs, 3);
+ assert_eq!(req.input_grouping, 12);
+ assert_eq!(req.chunk_size, req_len);
+ assert_eq!(req.num_chunks, 1);
+ assert_eq!(req.len, req_len);
+ assert_eq!(req.num_outputs, expected_output_counts[idx]);
+ assert!(!req.add);
+ }
+
+ assert_eq!(
+ requests[0].output + requests[0].num_outputs * requests[0].len,
+ requests[1].output
+ );
+ assert_eq!(
+ requests[1].output + requests[1].num_outputs * requests[1].len,
+ requests[2].output
+ );
+ assert_eq!(
+ requests[2].output + requests[2].num_outputs * requests[2].len,
+ requests[3].output
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_local_output_prefetch_stops_at_worker_partition_boundary() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let block_size = 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, block_size, 4);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ let request_count = backend.build_xor_jit_requests(0, false);
+ let requests = &backend.xor_jit_req_storage[..request_count];
+ let first = requests[0];
+ let second = requests[1];
+
+ assert!(first.num_outputs < backend.recovery_exponents.len());
+ assert_eq!(
+ xor_jit_output_prefetch_ptr(first, 0, 0).map(|ptr| ptr as usize),
+ Some(first.output + first.len)
+ );
+ assert_eq!(first.output + first.num_outputs * first.len, second.output);
+ assert_eq!(
+ xor_jit_output_prefetch_ptr(first, 0, first.num_outputs - 1).map(|ptr| ptr as usize),
+ None
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_localized_requests_keep_turbo_worker_ownership_order() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let block_size = 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, block_size, 8);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ let request_count = backend.build_xor_jit_requests(0, false);
+
+ assert_eq!(request_count, 7);
+ assert_eq!(
+ backend.xor_jit_req_thread_starts,
+ vec![0, 1, 2, 3, 4, 5, 6, 7]
+ );
+ assert_eq!(
+ backend.xor_jit_req_thread_counts,
+ vec![1, 1, 1, 1, 1, 1, 1, 0]
+ );
+ assert!(backend.xor_jit_req_storage[..7]
+ .iter()
+ .all(|req| req.num_chunks == 1));
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_input_prefetch_uses_turbo_stream_offsets() {
+ let segment_len = 128 * 1024;
+ let method_info = xor_jit_create_avx2_method_info();
+ let layout = XorJitBitplaneLayout::new(segment_len * 2, segment_len, 12, 512, method_info);
+ let req = XorJitComputeReq {
+ method: CreateGf16Method::Avx2XorJit,
+ input: 4096,
+ num_inputs: 12,
+ input_grouping: layout.input_batch_size,
+ chunk_size: layout.chunk_len,
+ num_chunks: layout.segment_count_for(layout.aligned_slice_len),
+ len: layout.aligned_slice_len,
+ num_outputs: layout.recovery_count,
+ ..XorJitComputeReq::default()
+ };
+ assert_eq!(
+ xor_jit_input_prefetch_ptr(req, 0, 508).map(|ptr| ptr as usize),
+ None
+ );
+ assert_eq!(
+ xor_jit_input_prefetch_ptr(req, 0, 509).map(|ptr| ptr as usize),
+ Some(4096 + layout.input_offset(segment_len, 0, segment_len))
+ );
+ assert_eq!(
+ xor_jit_input_prefetch_ptr(req, 0, 509).map(|ptr| ptr as usize),
+ Some(4096 + layout.input_offset(segment_len, 0, segment_len))
+ );
+ assert_eq!(
+ xor_jit_input_prefetch_ptr(req, 0, 510).map(|ptr| ptr as usize),
+ Some(
+ 4096 + layout.input_offset(segment_len, 0, segment_len)
+ + 10 * (segment_len >> method_info.prefetch_downscale)
+ )
+ );
+ assert_eq!(
+ xor_jit_input_prefetch_ptr(req, 0, 511).map(|ptr| ptr as usize),
+ Some(
+ 4096 + layout.input_offset(segment_len, 0, segment_len)
+ + 20 * (segment_len >> method_info.prefetch_downscale)
+ )
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_worker_prefetch_schedule_matches_turbo_avx2() {
+ let req = XorJitComputeReq {
+ method: CreateGf16Method::Avx2XorJit,
+ num_inputs: 12,
+ input_grouping: 12,
+ num_outputs: 8,
+ ..XorJitComputeReq::default()
+ };
+
+ let (inputs_prefetched_per_invok, input_prefetch_out_offset) =
+ xor_jit_input_prefetch_schedule(req);
+
+ assert_eq!(inputs_prefetched_per_invok, 40);
+ assert_eq!(input_prefetch_out_offset, 5);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_create_method_info_matches_turbo_avx2() {
+ let info = CreateGf16Method::Avx2XorJit
+ .xor_jit_create_method_info()
+ .expect("xor-jit method info");
+ assert_eq!(info.ideal_input_multiple, 1);
+ assert_eq!(info.prefetch_downscale, 1);
+ assert_eq!(info.alignment, 32);
+ assert_eq!(info.stride, 512);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_worker_branch_shape_matches_turbo() {
+ assert_eq!(
+ xor_jit_worker_branch(false, false, true),
+ XorJitWorkerBranch::MulAddMultiPackpf
+ );
+ assert_eq!(
+ xor_jit_worker_branch(false, false, false),
+ XorJitWorkerBranch::AddMultiPackpf
+ );
+ assert_eq!(
+ xor_jit_worker_branch(true, false, true),
+ XorJitWorkerBranch::MulAddMultiPackpf
+ );
+ assert_eq!(
+ xor_jit_worker_branch(true, false, false),
+ XorJitWorkerBranch::AddMultiPackpf
+ );
+ assert_eq!(
+ xor_jit_worker_branch(true, true, true),
+ XorJitWorkerBranch::MulAddMultiPacked
+ );
+ assert_eq!(
+ xor_jit_worker_branch(true, true, false),
+ XorJitWorkerBranch::MulAddMultiPacked
+ );
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_output_prefetch_rounds_are_method_info_driven() {
+ let info = xor_jit_create_avx2_method_info();
+ let prefetch_plan = xor_jit_create_prefetch_plan(info, 128 * 1024);
+ assert_eq!(
+ prefetch_plan.output_prefetch_rounds,
+ crate::reed_solomon::simd::xor_jit_create_output_prefetch_rounds(info)
+ );
+ assert_eq!(
+ prefetch_plan.output_prefetch_rounds,
+ 1 << info.prefetch_downscale
+ );
+ assert_eq!(prefetch_plan.pf_len, 128 * 1024 >> info.prefetch_downscale);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn xor_jit_add_only_local_request_stays_within_partition_boundaries() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let block_size = 1024 * 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 8, block_size, 1);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ backend.build_xor_jit_requests(0, false);
+ let req = backend.xor_jit_req_storage[0];
+ let output_prefetch = xor_jit_output_prefetch_ptr(req, 0, 0).map(|ptr| ptr as usize);
+ let last_prefetch = xor_jit_input_prefetch_ptr(req, 0, req.num_outputs - 1)
+ .map(|ptr| ptr as usize)
+ .expect("final output should schedule input prefetch on non-final round");
+ let request_input_end = req.input + req.num_chunks * req.chunk_size * req.input_grouping;
+ let request_output_end = req.output + req.num_outputs * req.len;
+
+ assert_eq!(output_prefetch, Some(req.output + req.chunk_size));
+ assert!(output_prefetch.unwrap() < request_output_end);
+ assert!(last_prefetch >= req.input + req.chunk_size * req.input_grouping);
+ assert!(last_prefetch < request_input_end);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn forced_xor_jit_backend_uses_packed_bitplane_layout() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+
+ let encoder = RecoveryBlockEncoder::new(1024 * 1024, 24);
+ let backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, 1024 * 1024, 1);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let layout = backend
+ .xor_jit_layout
+ .expect("forced XOR-JIT should initialize packed layout");
+ assert_eq!(layout.chunk_len, 131_584);
+ assert_eq!(layout.segment_count_for(layout.aligned_slice_len), 8);
+ assert_eq!(
+ layout.input_offset(layout.chunk_len, 0, layout.chunk_len),
+ backend.input_grouping * layout.chunk_len
+ );
+ assert_eq!(
+ layout.output_offset(layout.chunk_len, 0, layout.chunk_len),
+ 7 * layout.chunk_len
+ );
+ assert_eq!(backend.staging[0].inputs.len(), layout.input_storage_len());
+ assert_eq!(backend.output_chunks.len(), layout.output_storage_len());
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn forced_xor_jit_backend_honors_segment_len_override() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+ std::env::set_var(XOR_JIT_SEGMENT_LEN_ENV, "65536");
+
+ let encoder = RecoveryBlockEncoder::new(1024 * 1024, 24);
+ let backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, 1024 * 1024, 1);
+ std::env::remove_var(XOR_JIT_SEGMENT_LEN_ENV);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ return;
+ }
+
+ let layout = backend
+ .xor_jit_layout
+ .expect("forced XOR-JIT should initialize packed layout");
+ assert_eq!(layout.chunk_len, 66_048);
+ assert_eq!(layout.segment_count_for(layout.aligned_slice_len), 16);
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn forced_xor_jit_backend_reserves_turbo_two_phase_request_capacity() {
+ let _guard = env_lock().lock().unwrap();
+ std::env::set_var("PAR2RS_CREATE_GF16", "xor-jit");
+ std::env::set_var(XOR_JIT_SEGMENT_LEN_ENV, "8192");
+
+ let block_size = 256 * 1024;
+ let encoder = RecoveryBlockEncoder::new(block_size, 24);
+ let mut backend = CreateRecoveryBackend::new(encoder.base_values(), 0, 7, block_size, 2);
+
+ if backend.selected_method() != CreateGf16Method::Avx2XorJit {
+ std::env::remove_var(XOR_JIT_SEGMENT_LEN_ENV);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+ return;
+ }
+
+ let input = vec![0u8; block_size];
+ backend.begin_chunk(block_size);
+ std::env::remove_var(XOR_JIT_SEGMENT_LEN_ENV);
+ std::env::remove_var("PAR2RS_CREATE_GF16");
+ for source_idx in 0..3 {
+ backend.add_input(source_idx, &input);
+ }
+
+ let request_count = backend.build_xor_jit_requests(0, false);
+ assert_eq!(request_count, 4);
+ assert_eq!(
+ backend.xor_jit_req_storage.len(),
+ xor_jit_request_capacity(backend.workers.worker_count())
+ );
+ assert!(request_count <= backend.xor_jit_req_storage.len());
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ fn build_xor_jit_test_coeffs(base_values: &[u16], recovery_count: usize) -> Vec {
+ let mut cache = XorJitPreparedCoeffCache::new();
+ (0..recovery_count)
+ .flat_map(|exponent| {
+ base_values
+ .iter()
+ .map(move |&base| Galois16::new(base).pow(exponent as u16).value())
+ })
+ .map(|value| CreateCoeff::new(value, false, true, &mut cache))
+ .collect()
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ fn direct_xor_jit_compute_job_matches_encoder_across_x4_x2_and_x1_paths() {
+ let source_count = 7;
+ let recovery_count = 7;
+ let block_size = 241;
+ let encoder = RecoveryBlockEncoder::new(block_size, source_count);
+ let coeffs = build_xor_jit_test_coeffs(encoder.base_values(), recovery_count);
+ let inputs = deterministic_inputs(source_count, block_size);
+ let refs = inputs.iter().map(Vec::as_slice).collect::>();
+ let source_indices = (0..source_count).collect::>();
+ let mut packed_inputs = vec![0u8; source_count * 256];
+ let mut outputs = vec![0u8; recovery_count * 256];
+
+ for (idx, input) in inputs.iter().enumerate() {
+ let start = idx * 256;
+ packed_inputs[start..start + input.len()].copy_from_slice(input);
+ }
+
+ let job = ComputeJob {
+ method: CreateGf16Method::Avx2XorJit,
+ input_base: packed_inputs.as_ptr() as usize,
+ output_base: outputs.as_mut_ptr() as usize,
+ coeffs: coeffs.as_ptr() as usize,
+ source_indices: source_indices.as_ptr() as usize,
+ source_count,
+ batch_len: source_count,
+ aligned_chunk_len: 256,
+ segment_start: 0,
+ segment_len: block_size,
+ output_start: 0,
+ output_end: recovery_count,
+ };
+
+ process_compute_job(job, &mut WorkerContext::default());
+
+ for recovery_idx in 0..recovery_count {
+ let expected = encoder
+ .encode_recovery_block(recovery_idx as u16, &refs)
+ .expect("encode recovery block");
+ let start = recovery_idx * 256;
+ assert_eq!(&outputs[start..start + block_size], expected.as_slice());
+ assert!(outputs[start + block_size..start + 256]
+ .iter()
+ .all(|&byte| byte == 0));
+ }
+ }
+
+ #[cfg(target_arch = "x86_64")]
+ #[test]
+ #[ignore]
+ fn dump_xor_jit_prepared_staging_for_compare() {
+ let output_path = std::env::var("PAR2RS_XOR_JIT_PREPARED_DUMP_PATH")
+ .expect("PAR2RS_XOR_JIT_PREPARED_DUMP_PATH");
+ let slice_len = std::env::var("PAR2RS_XOR_JIT_PREPARED_SLICE_LEN")
+ .ok()
+ .and_then(|value| value.parse::().ok())
+ .unwrap_or(1024 * 1024);
+ let chunk_len = std::env::var("PAR2RS_XOR_JIT_PREPARED_CHUNK_LEN")
+ .ok()
+ .and_then(|value| value.parse::().ok())
+ .unwrap_or(128 * 1024);
+ let input_grouping = std::env::var("PAR2RS_XOR_JIT_PREPARED_INPUT_GROUPING")
+ .ok()
+ .and_then(|value| value.parse::().ok())
+ .unwrap_or(12);
+ let slot = std::env::var("PAR2RS_XOR_JIT_PREPARED_SLOT")
+ .ok()
+ .and_then(|value| value.parse::().ok())
+ .unwrap_or(5);
+ let src_len = std::env::var("PAR2RS_XOR_JIT_PREPARED_SRC_LEN")
+ .ok()
+ .and_then(|value| value.parse::().ok())
+ .unwrap_or(slice_len);
+
+ let layout = XorJitBitplaneLayout::new(
+ slice_len,
+ chunk_len,
+ input_grouping,
+ 1,
+ xor_jit_create_avx2_method_info(),
+ );
+ let mut staging = StagingArea::new(input_grouping, layout.input_storage_len(), 1);
+ let input = (0..src_len)
+ .map(|idx| ((idx * 37 + 11) & 0xff) as u8)
+ .collect::>();
+
+ prepare_xor_jit_bitplane_staging(layout, &mut staging, slot, slice_len, &input);
+ std::fs::write(output_path, &staging.inputs[..]).expect("write prepared staging dump");
+ }
}
diff --git a/src/create/context.rs b/src/create/context.rs
index fb8550cd..936893a8 100644
--- a/src/create/context.rs
+++ b/src/create/context.rs
@@ -7,6 +7,7 @@ use super::error_helpers::{
create_file, create_new_output_file, get_metadata, open_for_reading, packet_write_error,
};
use super::packet_generator::generate_recovery_set_id;
+use super::profile::{CreateProfile, CreateProfileCounters, CreateProfilePhase};
use super::progress::CreateReporter;
use super::source_file::{normalize_packet_path, packet_name_from_path, SourceFileInfo};
use super::types::CreateConfig;
@@ -37,6 +38,16 @@ fn write_recovery_slice_packet(
recovery_data: &[u8],
recovery_set_id: RecoverySetId,
) -> std::io::Result<()> {
+ let header = build_recovery_slice_header(exponent, recovery_data, recovery_set_id);
+ writer.write_all(&header)?;
+ writer.write_all(recovery_data)
+}
+
+fn build_recovery_slice_header(
+ exponent: u32,
+ recovery_data: &[u8],
+ recovery_set_id: RecoverySetId,
+) -> [u8; 68] {
use md5::{Digest, Md5};
let packet_length = 8 + 8 + 16 + 16 + 16 + 4 + recovery_data.len() as u64;
@@ -49,13 +60,14 @@ fn write_recovery_slice_packet(
hasher.update(recovery_data);
let computed_md5 = hasher.finalize();
- writer.write_all(crate::packets::MAGIC_BYTES)?;
- writer.write_all(&packet_length.to_le_bytes())?;
- writer.write_all(&computed_md5)?;
- writer.write_all(recovery_set_id.as_bytes())?;
- writer.write_all(RECOVERY_PACKET_TYPE)?;
- writer.write_all(&exponent.to_le_bytes())?;
- writer.write_all(recovery_data)
+ let mut header = [0u8; 68];
+ header[0..8].copy_from_slice(crate::packets::MAGIC_BYTES);
+ header[8..16].copy_from_slice(&packet_length.to_le_bytes());
+ header[16..32].copy_from_slice(&computed_md5);
+ header[32..48].copy_from_slice(recovery_set_id.as_bytes());
+ header[48..64].copy_from_slice(RECOVERY_PACKET_TYPE);
+ header[64..68].copy_from_slice(&exponent.to_le_bytes());
+ header
}
/// Compute the chunk size for chunked processing.
@@ -81,6 +93,33 @@ fn calculate_chunk_size_impl(
aligned.clamp(4, block_size.min(MAX_CREATE_CHUNK_SIZE))
}
+#[derive(Debug, Clone, Copy)]
+struct CreateProcessConfig {
+ chunk_size: ChunkSize,
+ defer_hash_computation: bool,
+}
+
+impl CreateProcessConfig {
+ fn new(
+ block_size: BlockSize,
+ source_block_count: u32,
+ recovery_block_count: u32,
+ memory_limit: usize,
+ ) -> Self {
+ let chunk_size = ChunkSize::new(calculate_chunk_size_impl(
+ block_size.as_usize(),
+ source_block_count as usize,
+ recovery_block_count as usize,
+ memory_limit,
+ ));
+ let defer_hash_computation = chunk_size.as_usize() >= block_size.as_usize();
+ Self {
+ chunk_size,
+ defer_hash_computation,
+ }
+ }
+}
+
/// Per-file hash data computed during `encode_and_hash_files`.
struct FileHashState {
hash_16k: crate::domain::Md5Hash,
@@ -94,6 +133,12 @@ struct FileHashState {
/// Recovery blocks paired with their exponents, as returned by `encode_and_hash_files`.
type RecoveryBlockVec = Vec<(u16, Vec)>;
+struct EncodeProfile {
+ counters: CreateProfileCounters,
+ source_open_hash_prepass: std::time::Duration,
+ recovery_chunk_processing: std::time::Duration,
+}
+
/// Encode all source files into recovery blocks while simultaneously computing
/// file/block hashes in a single pass.
///
@@ -111,10 +156,12 @@ fn encode_and_hash_files(
recovery_count: usize,
thread_count: usize,
reporter: &dyn CreateReporter,
-) -> CreateResult<(RecoveryBlockVec, Vec)> {
+ profile_enabled: bool,
+) -> CreateResult<(RecoveryBlockVec, Vec, Option)> {
use crate::checksum::compute_file_id;
use crate::create::source_file::BlockChecksum;
- use crc32fast::Hasher as Crc32Hasher;
+ use crate::parpar_hasher::hasher_input_dyn::HasherInputDyn;
+ use crate::parpar_hasher::BlockHash;
use md5::{Digest, Md5};
use std::fs::File;
use std::io::{Read, Seek};
@@ -124,50 +171,212 @@ fn encode_and_hash_files(
.build()
.map_err(|err| CreateError::Other(format!("failed to create thread pool: {err}")))?;
+ // True when the recovery loop traverses each file in strict
+ // sequential byte order (chunk_len == block_size for every block),
+ // letting us run HasherInputDyn inline with recovery generation —
+ // mirroring par2cmdline-turbo's `deferhashcomputation = true` path.
+ // Otherwise (slim mode), the recovery loop is chunk-offset-major
+ // across blocks, so per-file bytes are non-sequential there and
+ // we must hash each file in a separate sequential pass — that's
+ // turbo's `deferhashcomputation = false` path
+ // (`Par2CreatorSourceFile::Open` non-defer branch).
+ let defer_hash_to_recovery_loop = chunk_size >= block_size as usize;
+
+ let source_open_hash_start = profile_enabled.then(std::time::Instant::now);
let mut file_handles: Vec = Vec::with_capacity(source_files.len());
- let mut file_md5_states: Vec = Vec::with_capacity(source_files.len());
let mut file_16k_buffers: Vec> = Vec::with_capacity(source_files.len());
- let mut block_md5_states: Vec = Vec::with_capacity(source_block_count as usize);
- let mut block_crc32_states: Vec = Vec::with_capacity(source_block_count as usize);
+ // One HasherInputDyn per source file when we're going to drive it
+ // from the recovery loop. In slim mode we still allocate the slot
+ // but don't use it — kept symmetric for indexing.
+ let mut file_hashers: Vec