Merge pull request #16 from ensan-hcl/performance/add_inlinable

[Performance] Add @inlinable to more cases
This commit is contained in:
Miwa / Ensan
2023-08-31 19:22:22 +09:00
committed by GitHub
3 changed files with 29 additions and 26 deletions

View File

@@ -6,7 +6,6 @@
// Copyright © 2020 ensan. All rights reserved.
//
import Algorithms
import Foundation
import SwiftUtils
@@ -25,7 +24,6 @@ public final class DicdataStore {
private var ccParsed: [Bool] = .init(repeating: false, count: 1319)
private var ccLines: [[Int: PValue]] = []
private var mmValue: [PValue] = []
private let threshold: PValue = -17
private var loudses: [String: LOUDS] = [:]
private var importedLoudses: Set<String> = []
@@ -35,7 +33,12 @@ public final class DicdataStore {
private var osUserDict: [DicdataElement] = []
internal let maxlength: Int = 20
///
/// - TODO: make this value as an option
public let maxlength: Int = 20
///
/// - TODO: make this value as an option
public let threshold: PValue = -17
private let midCount = 502
private let cidCount = 1319
@@ -118,7 +121,7 @@ public final class DicdataStore {
}
///
private static func getPenalty(data: DicdataElement) -> PValue {
@inlinable static func getPenalty(data: DicdataElement) -> PValue {
-2.0 / PValue(data.word.count)
}
@@ -133,7 +136,7 @@ public final class DicdataStore {
}
///
internal func shouldBeRemoved(data: DicdataElement) -> Bool {
@inlinable func shouldBeRemoved(data: DicdataElement) -> Bool {
let d = data.value() - self.threshold
if d < 0 {
return true
@@ -358,7 +361,7 @@ public final class DicdataStore {
}
}
internal func getZeroHintPredictionDicdata() -> [DicdataElement] {
func getZeroHintPredictionDicdata() -> [DicdataElement] {
if let dicdata = self.zeroHintPredictionDicdata {
return dicdata
}
@@ -381,7 +384,7 @@ public final class DicdataStore {
/// - head:
/// - Returns:
///
internal func getPredictionLOUDSDicdata(key: some StringProtocol) -> [DicdataElement] {
func getPredictionLOUDSDicdata(key: some StringProtocol) -> [DicdataElement] {
let count = key.count
if count == .zero {
return []
@@ -591,18 +594,18 @@ public final class DicdataStore {
}
/// OSruby
private func getMatchOSUserDict(_ ruby: some StringProtocol) -> [DicdataElement] {
func getMatchOSUserDict(_ ruby: some StringProtocol) -> [DicdataElement] {
self.osUserDict.filter {$0.ruby == ruby}
}
/// OSruby
internal func getPrefixMatchOSUserDict(_ ruby: some StringProtocol) -> [DicdataElement] {
func getPrefixMatchOSUserDict(_ ruby: some StringProtocol) -> [DicdataElement] {
self.osUserDict.filter {$0.ruby.hasPrefix(ruby)}
}
//
// TODO: previous
internal func updateLearningData(_ candidate: Candidate, with previous: DicdataElement?) {
func updateLearningData(_ candidate: Candidate, with previous: DicdataElement?) {
if let previous {
self.learningManager.update(data: [previous] + candidate.data)
} else {
@@ -647,7 +650,7 @@ public final class DicdataStore {
]
//
internal static func existLOUDS(for character: Character) -> Bool {
static func existLOUDS(for character: Character) -> Bool {
Self.possibleLOUDS.contains(character)
}
@@ -673,7 +676,7 @@ public final class DicdataStore {
/// - c_latter: id
/// - Returns:
///
internal static func isClause(_ former: Int, _ latter: Int) -> Bool {
@inlinable static func isClause(_ former: Int, _ latter: Int) -> Bool {
// EOS
let latter_wordtype = Self.wordTypes[latter]
if latter_wordtype == 3 {
@@ -720,7 +723,7 @@ public final class DicdataStore {
/// - 1 when core
/// - 2 when postposition
/// - 11B1.3KB
static let wordTypes = (0...1319).map(_judgeWordType)
public static let wordTypes = (0...1319).map(_judgeWordType)
/// wordTypes使
private static func _judgeWordType(cid: Int) -> UInt8 {
@@ -736,7 +739,7 @@ public final class DicdataStore {
return 2 //
}
internal static func includeMMValueCalculation(_ data: DicdataElement) -> Bool {
@inlinable static func includeMMValueCalculation(_ data: DicdataElement) -> Bool {
//
if 895...1280 ~= data.lcid || 895...1280 ~= data.rcid {
return true
@@ -753,7 +756,7 @@ public final class DicdataStore {
static let penaltyRatio = (0...1319).map(_getTypoPenaltyRatio)
/// penaltyRatio使
internal static func _getTypoPenaltyRatio(_ lcid: Int) -> PValue {
static func _getTypoPenaltyRatio(_ lcid: Int) -> PValue {
// 147...368, 369...554
if 147...554 ~= lcid {
return 2.5
@@ -762,7 +765,7 @@ public final class DicdataStore {
}
//
internal static func needWValueMemory(_ data: DicdataElement) -> Bool {
@inlinable static func needWValueMemory(_ data: DicdataElement) -> Bool {
//
if 147...554 ~= data.lcid {
return false
@@ -786,7 +789,7 @@ public final class DicdataStore {
return true
}
internal static let possibleNexts: [String: [String]] = [
static let possibleNexts: [String: [String]] = [
"x": ["", "", "", "", "", "", "", "", "", ""],
"l": ["", "", "", "", "", "", "", "", "", ""],
"xt": [""],

View File

@@ -24,7 +24,7 @@ struct LOUDS {
/// 01
private let rankLarge: [Int]
init(bytes: [UInt64], nodeIndex2ID: [UInt8]) {
@inlinable init(bytes: [UInt64], nodeIndex2ID: [UInt8]) {
self.bits = bytes
self.char2nodeIndices = nodeIndex2ID.enumerated().reduce(into: .init(repeating: [], count: 1 << 8)) { list, data in
list[Int(data.element)].append(data.offset)
@@ -36,7 +36,7 @@ struct LOUDS {
}
/// parentNodeIndex01Index
private func childNodeIndices(from parentNodeIndex: Int) -> Range<Int> {
@inlinable func childNodeIndices(from parentNodeIndex: Int) -> Range<Int> {
//
// startIndex == parentNodeIndex0index
// endIndex == parentNodeIndex+10index
@@ -98,7 +98,7 @@ struct LOUDS {
/// charIndex
/// `childNodeIndices`0.02
private func searchCharNodeIndex(from parentNodeIndex: Int, char: UInt8) -> Int? {
@inlinable func searchCharNodeIndex(from parentNodeIndex: Int, char: UInt8) -> Int? {
// char2nodeIndices調
let childNodeIndices = self.childNodeIndices(from: parentNodeIndex)
let nodeIndices = self.char2nodeIndices[Int(char)]
@@ -122,7 +122,7 @@ struct LOUDS {
///
/// - Parameter chars: CharID
/// - Returns: loudstxt3
internal func searchNodeIndex(chars: [UInt8]) -> Int? {
@inlinable func searchNodeIndex(chars: [UInt8]) -> Int? {
var index = 1
for char in chars {
if let nodeIndex = self.searchCharNodeIndex(from: index, char: char) {
@@ -134,7 +134,7 @@ struct LOUDS {
return index
}
private func prefixNodeIndices(nodeIndex: Int, depth: Int = 0, maxDepth: Int) -> [Int] {
@inlinable func prefixNodeIndices(nodeIndex: Int, depth: Int = 0, maxDepth: Int) -> [Int] {
var childNodeIndices = Array(self.childNodeIndices(from: nodeIndex))
if depth == maxDepth {
return childNodeIndices
@@ -151,7 +151,7 @@ struct LOUDS {
/// - Parameter chars: CharID
/// - Parameter maxDepth:
/// - Returns: loudstxt3
internal func prefixNodeIndices(chars: [UInt8], maxDepth: Int) -> [Int] {
@inlinable func prefixNodeIndices(chars: [UInt8], maxDepth: Int) -> [Int] {
guard let nodeIndex = self.searchNodeIndex(chars: chars) else {
return []
}
@@ -164,7 +164,7 @@ struct LOUDS {
/// - Parameter chars: CharID
/// - Returns: loudstxt3
/// - Note:
internal func byfixNodeIndices(chars: [UInt8]) -> [Int] {
@inlinable func byfixNodeIndices(chars: [UInt8]) -> [Int] {
var indices = [1]
for char in chars {
if let nodeIndex = self.searchCharNodeIndex(from: indices.last!, char: char) {

View File

@@ -54,7 +54,7 @@ extension LOUDS {
/// LOUDS
/// - Parameter identifier:
/// - Returns: LOUDS`nil`
internal static func load(_ identifier: String, option: ConvertRequestOptions) -> LOUDS? {
static func load(_ identifier: String, option: ConvertRequestOptions) -> LOUDS? {
let (charsURL, loudsURL) = getLOUDSURL(identifier, option: option)
let nodeIndex2ID: [UInt8]
do {
@@ -104,7 +104,7 @@ extension LOUDS {
}
internal static func getDataForLoudstxt3(_ identifier: String, indices: [Int], option: ConvertRequestOptions) -> [DicdataElement] {
static func getDataForLoudstxt3(_ identifier: String, indices: [Int], option: ConvertRequestOptions) -> [DicdataElement] {
let binary: Data
do {
let url = getLoudstxt3URL(identifier, option: option)