Skip to content

Commit

Permalink
prune unnecessarily public API, drop some low-value abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift committed Feb 1, 2024
1 parent 78fed0e commit fb8dc3c
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Sources/LZ77/Deflator/LZ77.DeflatorBuffers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension LZ77
{
@frozen public
@frozen @usableFromInline
struct DeflatorBuffers<Format> where Format:LZ77.FormatType
{
var stream:Stream
Expand Down
4 changes: 2 additions & 2 deletions Sources/LZ77/Deflator/LZ77.DeflatorTables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ extension LZ77.DeflatorTables
runliteral.codewords(initializing: $0, count: 288)
{
$0 > 256 ?
.init(LZ77.Composites[run: .init(truncatingIfNeeded: $0)].extra) : 0
UInt8.init(LZ77.Composites[run: .init(truncatingIfNeeded: $0)].extra) : 0
}
distance.codewords( initializing: $0 + 288, count: 32)
{
.init(LZ77.Composites[distance: $0].extra)
UInt8.init(LZ77.Composites[distance: $0].extra)
}
meta?.codewords( initializing: $0 + 320, count: 19)
{
Expand Down
29 changes: 0 additions & 29 deletions Sources/LZ77/General.Storage.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CRC

extension Gzip.Format
{
@frozen public
@frozen @usableFromInline
struct Integral
{
@usableFromInline
Expand All @@ -20,10 +20,10 @@ extension Gzip.Format
}
extension Gzip.Format.Integral:LZ77.StreamIntegral
{
@inlinable public
@inlinable
var checksum:UInt32 { self.crc32.checksum }

@inlinable public mutating
@inlinable mutating
func update(from buffer:UnsafePointer<UInt8>, count:Int)
{
self.crc32.update(with: UnsafeBufferPointer.init(start: buffer, count: count))
Expand Down
25 changes: 17 additions & 8 deletions Sources/LZ77/HuffmanCoding/LZ77.Codeword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ extension LZ77
{
struct Codeword
{
private
let shape:(length:UInt8, extra:UInt8)
// bits are stored starting from least-significant bit to most-significant bit
let bits:UInt16
@General.Storage<UInt8>
var length:Int
@General.Storage<UInt8>
var extra:Int

init(shape:(length:UInt8, extra:UInt8), bits:UInt16)
{
self.shape = shape
self.bits = bits
}
}
}
extension LZ77.Codeword
{
init(counter:UInt16, length:Int, extra:Int)
init(counter:UInt16, shape:(length:UInt8, extra:UInt8))
{
// this branch should be well-predicted
if length <= 8
if shape.length <= 8
{
let low:UInt16 = LZ77.Reversed[counter]
self.init(bits: low &>> ( 8 - length), length: length, extra: extra)
self.init(shape: shape, bits: low &>> ( 8 - shape.length))
}
else
{
let high:UInt16 = LZ77.Reversed[counter & 0xff] << 8,
low:UInt16 = LZ77.Reversed[counter >> 8]
self.init(bits: (high | low) &>> (16 - length), length: length, extra: extra)
self.init(shape: shape, bits: (high | low) &>> (16 - shape.length))
}
}
}
extension LZ77.Codeword
{
var length:Int { .init(self.shape.length) }
var extra:Int { .init(self.shape.extra) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

extension General
extension LZ77
{
struct Heap<Key, Value> where Key:Comparable
{
Expand Down Expand Up @@ -48,7 +48,7 @@ extension General
}
}
}
extension General.Heap
extension LZ77.Heap
{
@inline(__always)
private static
Expand Down Expand Up @@ -174,7 +174,7 @@ extension General.Heap
}
}
}
extension General.Heap:ExpressibleByArrayLiteral
extension LZ77.Heap:ExpressibleByArrayLiteral
{
init(arrayLiteral:(key:Key, value:Value)...)
{
Expand Down
16 changes: 9 additions & 7 deletions Sources/LZ77/HuffmanCoding/LZ77.HuffmanTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,25 @@ extension LZ77.HuffmanTree where Symbol:BinaryInteger
extension LZ77.HuffmanTree where Symbol:BinaryInteger
{
func codewords(initializing destination:UnsafeMutablePointer<LZ77.Codeword>,
count:Int, extra:(Symbol) -> Int)
count:Int,
extra:(Symbol) -> UInt8)
{
// initialize all entries to 0, as symbols with frequency 0 are omitted
// from self.symbols
destination.initialize(repeating: .init(bits: 0, length: 0, extra: 0),
destination.initialize(
repeating: .init(shape: (length: 0, extra: 0), bits: 0),
count: count)

var counter:UInt16 = 0
for (length, level):(Int, Range<Int>) in zip(1 ... 15, self.levels)
for (length, level):(UInt8, Range<Int>) in zip(1 ... 15, self.levels)
{
for symbol:Symbol in self.symbols[level]
{
assert(.init(symbol) < count, "symbol out of range")

destination[.init(symbol)] =
.init(counter: counter, length: length, extra: extra(symbol))
counter += 1
destination[.init(symbol)] = .init(counter: counter,
shape: (length: length, extra: extra(symbol)))
counter += 1
}

counter <<= 1
Expand Down Expand Up @@ -265,7 +267,7 @@ extension LZ77.HuffmanTree where Symbol:BinaryInteger

// reversing (to get canonically sorted array) gets the heapify below
// to its best-case O(n) time, not that O matters for n = 256
var heap:General.Heap<Int, [Int]> = .init(symbols.reversed().map
var heap:LZ77.Heap<Int, [Int]> = .init(symbols.reversed().map
{
(frequencies[frequencies.startIndex + .init($0)], [1])
})
Expand Down
2 changes: 1 addition & 1 deletion Sources/LZ77/Wrappers/LZ77.Format.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension LZ77
}
extension LZ77.Format:LZ77.FormatType
{
public
@usableFromInline
typealias Integral = LZ77.MRC32
}
extension LZ77.Format
Expand Down
7 changes: 2 additions & 5 deletions Sources/LZ77/Wrappers/LZ77.FormatType.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
extension LZ77
{
public
@usableFromInline
typealias FormatType = _LZ77FormatType
}
public
@usableFromInline
protocol _LZ77FormatType
{
associatedtype Integral:LZ77.StreamIntegral

// func begin(inflating input:inout LZ77.InflatorIn, at bit:inout Int) throws -> Header?
// func check(inflating input:inout LZ77.InflatorIn, at bit:inout Int) -> UInt32??
}
8 changes: 4 additions & 4 deletions Sources/LZ77/Wrappers/LZ77.MRC32.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extension LZ77
{
/// Modular redundancy check (similar to ``CRC32``)
@frozen public
@frozen @usableFromInline
struct MRC32
{
@usableFromInline
var single:UInt32
@usableFromInline
var double:UInt32

@inlinable public
@inlinable
init()
{
self.single = 1
Expand All @@ -22,7 +22,7 @@ extension LZ77.MRC32:LZ77.StreamIntegral
// software.intel.com/content/www/us/en/develop/articles/fast-computation-of-adler32-checksums
// link also says to use simd vectorization, but that just seems to slow
// things down (probably because llvm is already autovectorizing it)
@inlinable public mutating
@inlinable mutating
func update(from start:UnsafePointer<UInt8>, count:Int)
{
let (q, r):(Int, Int) = count.quotientAndRemainder(dividingBy: 5552)
Expand All @@ -46,6 +46,6 @@ extension LZ77.MRC32:LZ77.StreamIntegral
self.double %= 65521
}

@inlinable public
@inlinable
var checksum:UInt32 { self.double << 16 | self.single }
}
4 changes: 2 additions & 2 deletions Sources/LZ77/Wrappers/LZ77.StreamIntegral.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extension LZ77
{
public
@usableFromInline
typealias StreamIntegral = _LZ77StreamIntegral
}
public
@usableFromInline
protocol _LZ77StreamIntegral
{
init()
Expand Down
File renamed without changes.

0 comments on commit fb8dc3c

Please sign in to comment.