Skip to content

Commit 0a852f9

Browse files
committed
Compress emoji resource.
~10x file size reduction, 299KB → 27KB https://tijo.link/D28aSM. Speed-wise I'm not concerned with this change. Did some light benchmarking and parsing is still _by far_ the slowest part of this, followed by reading from disk and then the decompression step. If anything, this is probably faster than before since 10x less is being read from disk. Benchmark code: /// Returns an array of all available emojis. Use this method to retrieve emojis for your own collection. /// - Returns: Array of all emojis. static public func getAllEmoji () -> [Emoji] { let readStart = CACurrentMediaTime() guard let url = Bundle.module.url(forResource: "Emoji Unicode 15.0", withExtension: "json.br"), let compressedEmojiData = try? Data(contentsOf: url), let brotliAlgorithm = NSData.CompressionAlgorithm(rawValue: NSData.CompressionAlgorithm.zlib.rawValue + 1) else { // let emojiData = try? (compressedEmojiData as NSData).decompressed(using: brotliAlgorithm) as Data, // Brotli decompress https://tijo.link/WEQjXF // let emoji = try? JSONDecoder().decode([Emoji].self, from: emojiData) else { return [] } print("Read: \(CACurrentMediaTime() - readStart)") let decompStart = CACurrentMediaTime() guard let emojiData = try? (compressedEmojiData as NSData).decompressed(using: brotliAlgorithm) as Data else { // Brotli decompress https://tijo.link/WEQjXF return [] } print("Decompress: \(CACurrentMediaTime() - decompStart)") let parseStart = CACurrentMediaTime() guard let emoji = try? JSONDecoder().decode([Emoji].self, from: emojiData) else { return [] } print("Parse: \(CACurrentMediaTime() - parseStart)") return emoji } Benchmark results (debug build, sim, M3 Pro): https://tijo.link/a11f1B
1 parent 6e1425d commit 0a852f9

4 files changed

Lines changed: 9 additions & 4 deletions

File tree

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let package = Package(
2020
name: "ElegantEmojiPicker",
2121
path: "Sources",
2222
resources: [
23-
.process("Resources/Emoji Unicode 15.0.json"),
23+
.process("Resources/Emoji Unicode 15.0.json.br"),
2424
.process("Resources/Icons.xcassets")
2525
])
2626
]

Sources/ElegantEmojiPicker.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,14 @@ extension ElegantEmojiPicker {
520520
/// Returns an array of all available emojis. Use this method to retrieve emojis for your own collection.
521521
/// - Returns: Array of all emojis.
522522
static public func getAllEmoji () -> [Emoji] {
523-
let emojiData = (try? Data(contentsOf: Bundle.module.url(forResource: "Emoji Unicode 15.0", withExtension: "json")!))!
524-
return try! JSONDecoder().decode([Emoji].self, from: emojiData)
523+
guard let url = Bundle.module.url(forResource: "Emoji Unicode 15.0", withExtension: "json.br"),
524+
let compressedEmojiData = try? Data(contentsOf: url),
525+
let brotliAlgorithm = NSData.CompressionAlgorithm(rawValue: NSData.CompressionAlgorithm.zlib.rawValue + 1),
526+
let emojiData = try? (compressedEmojiData as NSData).decompressed(using: brotliAlgorithm) as Data, // Brotli decompress https://tijo.link/WEQjXF
527+
let emoji = try? JSONDecoder().decode([Emoji].self, from: emojiData) else {
528+
return []
529+
}
530+
return emoji
525531
}
526532

527533
/// Returns an array of all available emojis categorized by section.

Sources/Resources/Emoji Unicode 15.0.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
26.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)