Skip to content

Commit f4aafb6

Browse files
committed
Improve performance and remove resources from package by default
1 parent 2c2285c commit f4aafb6

6 files changed

Lines changed: 38 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
2+
#### Improvements
3+
* Add background engine processing for better performance.
4+
* Remove neural network resource files for greatly reduced package size.
5+
* These can be added manually by bundling the desired files and setting the appropriate engine to use them (see `Resources` directory).
6+
* For `Stockfish 15.1` (`nn-1337b1adec5b.nnue`):
7+
``` swift
8+
engine.send(command: .setoption(id: "EvalFile", value: evalFileURL))
9+
engine.send(command: .setoption(id: "Use NNUE", value: "true"))
10+
```
11+
* For `LeelaChessZero 0.29` (`192x15_network`):
12+
``` swift
13+
engine.send(command: .setoption(id: "WeightsFile", value: weightsFileURL))
14+
```
15+
116
# ChessKitEngine 0.2.2
217
Released Thursday, May 18, 2023.
318

Package.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ let package = Package(
1818
targets: [
1919
.target(
2020
name: "ChessKitEngine",
21-
dependencies: ["ChessKitEngineCore"],
22-
resources: [
23-
.copy("Resources/192x15_network"),
24-
.copy("Resources/nn-1337b1adec5b.nnue")
25-
]
21+
dependencies: ["ChessKitEngineCore"]
2622
),
2723
.target(
2824
name: "ChessKitEngineCore",

Sources/ChessKitEngine/Resources/192x15_network renamed to Resources/192x15_network

File renamed without changes.

Sources/ChessKitEngine/Resources/nn-1337b1adec5b.nnue renamed to Resources/nn-1337b1adec5b.nnue

File renamed without changes.

Sources/ChessKitEngine/Engine.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ public class Engine {
2727
///
2828
/// Engine must be running for `send(command:)` to work.
2929
public private(set) var isRunning = false
30-
30+
31+
private let queue = DispatchQueue(
32+
label: "ck-engine-queue",
33+
qos: .userInteractive
34+
)
35+
3136
/// Initializes an engine with the provided `type`.
3237
///
3338
/// - parameter type: The type of engine to use.
@@ -40,11 +45,13 @@ public class Engine {
4045
messenger.responseHandler = { [weak self] response in
4146
guard let self else { return }
4247

43-
if let parsedResponse = EngineResponse(rawValue: response) {
44-
self.log(parsedResponse.rawValue)
45-
self.receiveResponse(parsedResponse)
46-
} else if !response.isEmpty {
47-
self.log(response)
48+
DispatchQueue.main.async {
49+
if let parsedResponse = EngineResponse(rawValue: response) {
50+
self.log(parsedResponse.rawValue)
51+
self.receiveResponse(parsedResponse)
52+
} else if !response.isEmpty {
53+
self.log(response)
54+
}
4855
}
4956
}
5057
}
@@ -110,9 +117,11 @@ public class Engine {
110117
log("Engine is not running, call start() first.")
111118
return
112119
}
113-
114-
log(command.rawValue)
115-
messenger.sendCommand(command.rawValue)
120+
121+
queue.async {
122+
self.log(command.rawValue)
123+
self.messenger.sendCommand(command.rawValue)
124+
}
116125
}
117126

118127
/// Closure that is called when engine responses are received.

Sources/ChessKitEngine/EngineType.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,12 @@ public enum EngineType: Int {
3939
var setupCommands: [EngineCommand] {
4040
switch self {
4141
case .stockfish:
42-
let evalFile = Bundle.module.url(forResource: "nn-1337b1adec5b", withExtension: "nnue")?
43-
.absoluteString
44-
.replacingOccurrences(of: "file://", with: "")
45-
46-
return [
47-
evalFile != nil ?
48-
.setoption(id: "EvalFile", value: evalFile!) : nil,
49-
.setoption(id: "Use NNUE", value: "true"),
42+
[
43+
.setoption(id: "Use NNUE", value: "false"),
5044
.setoption(id: "UCI_AnalyseMode", value: "true")
51-
].compactMap { $0 }
45+
]
5246
case .lc0:
53-
let weights = Bundle.module.url(forResource: "192x15_network", withExtension: nil)?
54-
.absoluteString
55-
.replacingOccurrences(of: "file://", with: "")
56-
57-
return [
58-
.setoption(id: "Backend", value: "eigen"),
59-
weights != nil ?
60-
.setoption(id: "WeightsFile", value: weights!) : nil
61-
].compactMap { $0 }
47+
[]
6248
}
6349
}
6450

0 commit comments

Comments
 (0)