From 9f68632dfa2e1ed7a35fa47dfdcc0613fe1dff0f Mon Sep 17 00:00:00 2001 From: Paullo9 Date: Fri, 6 Nov 2026 13:50:39 +0100 Subject: [PATCH] refactor: remove unused imports and unreachable code (Periphery pass) Code that nothing reaches, found with Periphery (--retain-public). No public API, behaviour or fixture changes. - 6 unused imports (2 in Sources, 4 in tests) - DM4ImageObject.elementCount and VeloxEventStore.storageBytes, package-level computed properties nothing reads - arguments private functions never read, with what their callers passed: preferredDetector of VeloxEMDReader.readScanImage, memory of scalarDataset in BraggVectorsEMD, path of checkParents and makeGroups in HDF5AppendWriter - the path field of the private VeloxSpectrumRead, assigned and never read Periphery: 24 findings before, 12 after; the rest are fixture fields, a field of a synthesized ==, the libhdf5 symbol table and a C callback argument. Done: the dead-code pass for this repo, tools/test.sh green. Next: a PR check against a Periphery baseline so new dead code is caught. Co-Authored-By: Claude --- Sources/FormatoDM/DM4Experiment.swift | 7 ------- Sources/FormatoEMD/VeloxEMDReader.swift | 6 ++---- Sources/FormatoEMD/VeloxEventStore.swift | 3 --- Sources/FormatoEMD/VeloxMetadata.swift | 1 - Sources/FormatoEMD/VeloxSpectrumSignals.swift | 2 -- Sources/FormatoEMD/VeloxTitles.swift | 1 - Sources/FormatoHDF5/BraggVectorsEMD.swift | 10 +++++----- Sources/FormatoHDF5/HDF5AppendWriter.swift | 18 +++++++++--------- .../VeloxMetadataBoundTests.swift | 1 - .../VeloxMetadataFieldsTests.swift | 1 - .../WriteLockAfterCloseTests.swift | 1 - .../TIFFSeriesRefusalTests.swift | 1 - 12 files changed, 16 insertions(+), 36 deletions(-) diff --git a/Sources/FormatoDM/DM4Experiment.swift b/Sources/FormatoDM/DM4Experiment.swift index fdeda75..5c0dd82 100644 --- a/Sources/FormatoDM/DM4Experiment.swift +++ b/Sources/FormatoDM/DM4Experiment.swift @@ -144,13 +144,6 @@ public nonisolated struct DM4ImageObject: Sendable, Equatable { public let dataByteCount: Int package var dataTypeName: String { DM4Experiment.dataTypeName(dataType) } - /// Saturates at `Int.max` rather than trapping on absurd tag dimensions. - package var elementCount: Int { - dimensions.reduce(1) { acc, d in - let (p, o) = acc.multipliedReportingOverflow(by: d) - return o ? Int.max : p - } - } } /// A decoded 2D image object. diff --git a/Sources/FormatoEMD/VeloxEMDReader.swift b/Sources/FormatoEMD/VeloxEMDReader.swift index d73320c..eb54e5e 100644 --- a/Sources/FormatoEMD/VeloxEMDReader.swift +++ b/Sources/FormatoEMD/VeloxEMDReader.swift @@ -589,8 +589,7 @@ public nonisolated struct VeloxEMDReader: Sendable { } let image = try Self.readScanImage( - file: file, shapes: imageShapes, grid: grid, range: range, preferredDetector: metadata.binaryResultDetector, - cancelled: cancelled) + file: file, shapes: imageShapes, grid: grid, range: range, cancelled: cancelled) tracker.finish() return VeloxSpectrumImage( eventStore: store, scanImage: image, metadata: metadata, version: version.version, @@ -707,8 +706,7 @@ public nonisolated struct VeloxEMDReader: Sendable { /// integer image; summed over the same frame range, clipped to the frames the image has. private static func readScanImage( file: VeloxFile, shapes: [(name: String, ny: Int, nx: Int, frames: Int, isInteger: Bool)], - grid: (ny: Int, nx: Int), range: Range, preferredDetector: String?, - cancelled: @Sendable () -> Bool + grid: (ny: Int, nx: Int), range: Range, cancelled: @Sendable () -> Bool ) throws -> VeloxScanImage? { let onGrid = shapes.filter { $0.ny == grid.ny && $0.nx == grid.nx && $0.isInteger } guard !onGrid.isEmpty else { return nil } diff --git a/Sources/FormatoEMD/VeloxEventStore.swift b/Sources/FormatoEMD/VeloxEventStore.swift index 39a3cb5..51138b3 100644 --- a/Sources/FormatoEMD/VeloxEventStore.swift +++ b/Sources/FormatoEMD/VeloxEventStore.swift @@ -58,9 +58,6 @@ public nonisolated struct VeloxEventStore: Sendable { /// inside the frame range (prediction P4). public var totalCounts: Int { counts.reduce(0) { $0 + Int($1) } } package var distinctEntries: Int { channelIndex.count } - package var storageBytes: Int { - rowOffsets.count * MemoryLayout.stride + channelIndex.count * 2 + counts.count * 4 - } /// Frames of dose that `pixel` received. public func framesCovering(pixel: Int) -> Int { diff --git a/Sources/FormatoEMD/VeloxMetadata.swift b/Sources/FormatoEMD/VeloxMetadata.swift index edf5bc8..4988fe1 100644 --- a/Sources/FormatoEMD/VeloxMetadata.swift +++ b/Sources/FormatoEMD/VeloxMetadata.swift @@ -5,7 +5,6 @@ // import Foundation -import FormatoCore // MARK: - Metadata diff --git a/Sources/FormatoEMD/VeloxSpectrumSignals.swift b/Sources/FormatoEMD/VeloxSpectrumSignals.swift index 9d03748..60a8713 100644 --- a/Sources/FormatoEMD/VeloxSpectrumSignals.swift +++ b/Sources/FormatoEMD/VeloxSpectrumSignals.swift @@ -34,7 +34,6 @@ public nonisolated struct VeloxSpectrumSignals: Sendable { } private nonisolated struct VeloxSpectrumRead { - let path: String let zone: TimeZone? let policy: VeloxReadPolicy let cancelled: @Sendable () -> Bool @@ -42,7 +41,6 @@ private nonisolated struct VeloxSpectrumRead { let fileName: String init(path: String, zone: TimeZone?, policy: VeloxReadPolicy, cancelled: @escaping @Sendable () -> Bool) throws { - self.path = path self.zone = zone self.policy = policy self.cancelled = cancelled diff --git a/Sources/FormatoEMD/VeloxTitles.swift b/Sources/FormatoEMD/VeloxTitles.swift index 39b061e..a52783d 100644 --- a/Sources/FormatoEMD/VeloxTitles.swift +++ b/Sources/FormatoEMD/VeloxTitles.swift @@ -10,7 +10,6 @@ import Foundation import FormatoCore -import FormatoHDF5 /// The title of each image of a Velox file, and where it came from. nonisolated struct VeloxTitles: Sendable { diff --git a/Sources/FormatoHDF5/BraggVectorsEMD.swift b/Sources/FormatoHDF5/BraggVectorsEMD.swift index 06e60fe..965adb2 100644 --- a/Sources/FormatoHDF5/BraggVectorsEMD.swift +++ b/Sources/FormatoHDF5/BraggVectorsEMD.swift @@ -474,8 +474,8 @@ private nonisolated struct EMDWriter { guard withUnsafePointer(to: &v, { h5.h5awrite(attribute, h5.int64, $0) }) >= 0 else { throw fail("writing attribute \(name)") } } - /// Creates a scalar dataset, writes `bytes` from memory type `memory`, and tags it with py4DSTEM's `type` attribute. - private func scalarDataset(_ name: String, fileType: hid_t, memory: hid_t, tag: String?, in parent: hid_t, + /// Creates a scalar dataset, lets `write` fill it, and tags it with py4DSTEM's `type` attribute. + private func scalarDataset(_ name: String, fileType: hid_t, tag: String?, in parent: hid_t, write: (hid_t) -> herr_t) throws { let space = h5.h5screate(h5Scalar); guard space >= 0 else { throw fail("dataset \(name)") } defer { _ = h5.h5sclose(space) } @@ -488,7 +488,7 @@ private nonisolated struct EMDWriter { private func doubleDataset(_ name: String, _ value: Double, in parent: hid_t) throws { var v = value - try scalarDataset(name, fileType: h5.float64, memory: h5.float64, tag: "number", in: parent) { ds in + try scalarDataset(name, fileType: h5.float64, tag: "number", in: parent) { ds in withUnsafePointer(to: &v) { h5.h5dwrite(ds, h5.float64, h5EntireSpace, h5EntireSpace, h5Default, $0) } } } @@ -501,7 +501,7 @@ private nonisolated struct EMDWriter { guard "FALSE".withCString({ h5.h5tenumInsert(type, $0, &f) }) >= 0, "TRUE".withCString({ h5.h5tenumInsert(type, $0, &t) }) >= 0 else { throw fail("defining the bool type") } var v: Int8 = value ? 1 : 0 - try scalarDataset(name, fileType: type, memory: type, tag: "bool", in: parent) { ds in + try scalarDataset(name, fileType: type, tag: "bool", in: parent) { ds in withUnsafePointer(to: &v) { h5.h5dwrite(ds, type, h5EntireSpace, h5EntireSpace, h5Default, $0) } } } @@ -509,7 +509,7 @@ private nonisolated struct EMDWriter { /// py4DSTEM stores its strings as variable-length ASCII; units and paths stay ASCII-typed for it. private func stringDataset(_ name: String, _ value: String, type tag: String?, in parent: hid_t) throws { let type = try stringType(utf8: false); defer { _ = h5.h5tclose(type) } - try scalarDataset(name, fileType: type, memory: type, tag: tag, in: parent) { ds in + try scalarDataset(name, fileType: type, tag: tag, in: parent) { ds in value.withCString { characters -> herr_t in var pointer: UnsafePointer? = characters return withUnsafePointer(to: &pointer) { h5.h5dwrite(ds, type, h5EntireSpace, h5EntireSpace, h5Default, $0) } diff --git a/Sources/FormatoHDF5/HDF5AppendWriter.swift b/Sources/FormatoHDF5/HDF5AppendWriter.swift index b3b2c31..fe77d60 100644 --- a/Sources/FormatoHDF5/HDF5AppendWriter.swift +++ b/Sources/FormatoHDF5/HDF5AppendWriter.swift @@ -151,7 +151,7 @@ public nonisolated final class HDF5WriteSession { /// object anywhere along the path throws `wrongKind`. public func createGroup(_ path: String) throws { let parts = try HDF5Ops.components(path) - try open { try makeGroups(parts, $0, path) } + try open { try makeGroups(parts, $0) } } /// Writes `array` as a new dataset at `path` (missing groups above it are created), stored in the array's own type and @@ -196,7 +196,7 @@ public nonisolated final class HDF5WriteSession { } guard shape.count <= HDF5Ops.maximumRank else { throw HDF5FileError.limitExceeded("rank \(shape.count)") } try open { file in - try checkParents(Array(parts.dropLast()), file, path) + try checkParents(Array(parts.dropLast()), file) if HDF5Ops.linkExists(h5, file, parts) { guard replace else { throw HDF5FileError.alreadyExists(path) } guard HDF5Ops.kind(h5, file, parts) != .group else { throw HDF5FileError.alreadyExists("\(path) (a group; use deleteLink)") } @@ -204,7 +204,7 @@ public nonisolated final class HDF5WriteSession { throw HDF5RootAttributes.failure("unlinking \(path)", h5) } } - try makeGroups(Array(parts.dropLast()), file, path) + try makeGroups(Array(parts.dropLast()), file) let dims = shape.map(UInt64.init) let space = dims.isEmpty ? h5.h5screate(0) : h5.h5screateSimple(Int32(dims.count), dims, nil) guard space >= 0 else { throw HDF5RootAttributes.failure("making the space of \(path)", h5) } @@ -404,7 +404,7 @@ public nonisolated final class HDF5WriteSession { let other = try HDF5RootAttributes.openForReading(source, h5) defer { _ = h5.h5fclose(other) } guard HDF5Ops.linkExists(h5, other, sourceParts) else { throw HDF5FileError.notFound(sourcePath) } - try makeGroups(Array(parts.dropLast()), file, path) + try makeGroups(Array(parts.dropLast()), file) if HDF5Ops.linkExists(h5, file, parts) { guard replace else { throw HDF5FileError.alreadyExists(path) } guard HDF5Ops.absolute(parts).withCString({ h5.h5ldelete(file, $0, 0) }) >= 0 else { @@ -421,7 +421,7 @@ public nonisolated final class HDF5WriteSession { // MARK: Internals (all under open) /// Throws `wrongKind` when a component of `parts` that exists is not a group. - private func checkParents(_ parts: [String], _ file: hid_t, _ path: String) throws { + private func checkParents(_ parts: [String], _ file: hid_t) throws { guard !parts.isEmpty else { return } for count in 1...parts.count { let prefix = Array(parts.prefix(count)) @@ -432,9 +432,9 @@ public nonisolated final class HDF5WriteSession { } } - private func makeGroups(_ parts: [String], _ file: hid_t, _ path: String) throws { + private func makeGroups(_ parts: [String], _ file: hid_t) throws { guard !parts.isEmpty else { return } - try checkParents(parts, file, path) + try checkParents(parts, file) for count in 1...parts.count { let prefix = Array(parts.prefix(count)) if HDF5Ops.linkExists(h5, file, prefix) { continue } @@ -457,7 +457,7 @@ public nonisolated final class HDF5WriteSession { /// The checks and unlinking shared by the scalar dataset writers (all under `open`). private func prepareScalar(_ parts: [String], _ file: hid_t, _ path: String, replace: Bool) throws { - try checkParents(Array(parts.dropLast()), file, path) + try checkParents(Array(parts.dropLast()), file) if HDF5Ops.linkExists(h5, file, parts) { guard replace else { throw HDF5FileError.alreadyExists(path) } guard HDF5Ops.kind(h5, file, parts) != .group else { throw HDF5FileError.alreadyExists("\(path) (a group; use deleteLink)") } @@ -465,7 +465,7 @@ public nonisolated final class HDF5WriteSession { throw HDF5RootAttributes.failure("unlinking \(path)", h5) } } - try makeGroups(Array(parts.dropLast()), file, path) + try makeGroups(Array(parts.dropLast()), file) } private func createScalar(_ parts: [String], _ file: hid_t, _ path: String, type: hid_t, diff --git a/Tests/FormatoEMDTests/VeloxMetadataBoundTests.swift b/Tests/FormatoEMDTests/VeloxMetadataBoundTests.swift index 15936fa..c6cd94d 100644 --- a/Tests/FormatoEMDTests/VeloxMetadataBoundTests.swift +++ b/Tests/FormatoEMDTests/VeloxMetadataBoundTests.swift @@ -2,7 +2,6 @@ import Testing import Foundation import FormatoCore import FormatoEMD -import FormatoHDF5 extension VeloxEMDSerialized { /// A Velox file's metadata row count comes out of the file; a corrupt or hostile one must be refused before it is allocated. diff --git a/Tests/FormatoEMDTests/VeloxMetadataFieldsTests.swift b/Tests/FormatoEMDTests/VeloxMetadataFieldsTests.swift index 94b94ec..af85bbe 100644 --- a/Tests/FormatoEMDTests/VeloxMetadataFieldsTests.swift +++ b/Tests/FormatoEMDTests/VeloxMetadataFieldsTests.swift @@ -44,7 +44,6 @@ import Foundation import Testing import FormatoCore import FormatoEMD -import FormatoHDF5 extension VeloxEMDSerialized { @Suite(.serialized) struct VeloxMetadataFieldsTests { diff --git a/Tests/FormatoHDF5Tests/WriteLockAfterCloseTests.swift b/Tests/FormatoHDF5Tests/WriteLockAfterCloseTests.swift index 67a7032..655f55e 100644 --- a/Tests/FormatoHDF5Tests/WriteLockAfterCloseTests.swift +++ b/Tests/FormatoHDF5Tests/WriteLockAfterCloseTests.swift @@ -23,7 +23,6 @@ import Darwin import Foundation import Synchronization import Testing -import FormatoCore import FormatoHDF5 @Suite struct WriteLockAfterCloseTests { diff --git a/Tests/FormatoTIFFTests/TIFFSeriesRefusalTests.swift b/Tests/FormatoTIFFTests/TIFFSeriesRefusalTests.swift index 50e41e1..70a6060 100644 --- a/Tests/FormatoTIFFTests/TIFFSeriesRefusalTests.swift +++ b/Tests/FormatoTIFFTests/TIFFSeriesRefusalTests.swift @@ -6,7 +6,6 @@ import Foundation import Testing -import FormatoCore @testable import FormatoTIFF @Suite struct TIFFSeriesRefusalTests {