msime-engine 0.2.0

Input method engine for Chinese and other languages: quanpin, shuangpin, nine-key, wubi, zhuyin, Cantonese, Japanese, Korean and more, ranked over a word lattice with typo correction and learning from what the user picks. The engine of 水杉输入法 (Metasequoia IME).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use super::*;
use crate::ime::personal_rerank::allocations::count;
use crate::quanpin::fixture::Fixture;

fn cached_rows(count: usize, source: CandidateSource) -> Vec<WordItem> {
    let mut rows = Vec::with_capacity(count + 1);
    for index in 0..count {
        let mut row = WordItem::new(
            "ni",
            format!("合成本地{index}"),
            index as i64,
            CandidateSource::Database,
            "ni",
        );
        row.corrected_from = "合成纠错".into();
        row.sentence_association = true;
        row.sentence_words = vec!["合成词甲".to_owned(), "合成词乙".to_owned()];
        rows.push(row);
    }
    rows.push(WordItem::new("ni", "合成旧在线", 1, source, ""));
    rows
}

fn batches() -> Vec<(Vec<String>, CandidateSource)> {
    vec![
        (vec!["合成新在线".to_owned()], CandidateSource::AiSuggestion),
        (
            vec!["合成新在线".to_owned()],
            CandidateSource::CloudSuggestion,
        ),
        (vec![], CandidateSource::AiSuggestion),
        (vec!["坏\n".to_owned()], CandidateSource::AiSuggestion),
        (
            (0..11).map(|index| format!("超额{index}")).collect(),
            CandidateSource::AiSuggestion,
        ),
        (vec!["不合法来源".to_owned()], CandidateSource::Database),
    ]
}

// 固定 4f499433c5 的完整回写入口,仅改名并移除注释。
impl QuanpinDictionary {
    pub(super) fn frozen_insert_online_words(
        &mut self,
        raw: &str,
        segmentation: &str,
        autocorrect_types: u32,
        words: &[String],
        source: CandidateSource,
    ) -> bool {
        if raw.is_empty() || words.is_empty() {
            return false;
        }
        let cache_key =
            self.project_resolution(raw, segmentation, autocorrect_types, |resolution| {
                match resolution {
                    Cow::Borrowed(resolution) => resolution.cache_key.clone(),
                    Cow::Owned(resolution) => resolution.cache_key,
                }
            });
        let key = self.series_slot(&cache_key);
        let mut list = self.series_cache.get(&key).unwrap_or_default();
        if !replace_online_candidate_batch(&mut list, raw, words, source) {
            return false;
        }
        self.series_cache.insert(key, list);
        true
    }
}

fn slot(dictionary: &mut QuanpinDictionary, raw: &str) -> String {
    let cache_key =
        dictionary.project_resolution(raw, raw, 0, |resolution| resolution.cache_key.clone());
    dictionary.series_slot(&cache_key)
}

#[test]
fn cached_online_update_avoids_page_clones() {
    let fixture = Fixture::new();
    for width in [0, 1, 63, 64, 128] {
        for source in [
            CandidateSource::AiSuggestion,
            CandidateSource::CloudSuggestion,
        ] {
            let original = cached_rows(width, source);
            let (_, clone_cost) = count(|| original.clone());
            for (batch, batch_source) in batches() {
                let mut before = QuanpinDictionary::new(&fixture.paths);
                let mut after = QuanpinDictionary::new(&fixture.paths);
                let key = slot(&mut before, "ni");
                assert_eq!(slot(&mut after, "ni"), key);
                before.series_cache.insert(key.clone(), original.clone());
                after.series_cache.insert(key.clone(), original.clone());
                let (expected, old_count) = count(|| {
                    before.frozen_insert_online_words("ni", "ni", 0, &batch, batch_source)
                });
                let (actual, new_count) =
                    count(|| after.insert_online_words("ni", "ni", 0, &batch, batch_source));
                assert_eq!(actual, expected);
                assert_eq!(
                    after.series_cache.get_ref(&key),
                    before.series_cache.get_ref(&key)
                );
                // 另一来源的新行可能扩容;它只影响旧新容器的增长,不是页字段复制。
                if batch.is_empty() {
                    assert_eq!(new_count, old_count);
                } else if batch_source == source || !actual {
                    assert_eq!(
                        new_count + clone_cost,
                        old_count,
                        "width={width} source={source:?} batch={batch_source:?}"
                    );
                }
            }
        }
    }
}

#[test]
fn cached_online_update_keeps_fifo_and_owned_snapshots() {
    let fixture = Fixture::new();
    let mut dictionary = QuanpinDictionary::new(&fixture.paths);
    dictionary.series_cache = FifoCache::new(2);
    let first = slot(&mut dictionary, "ni");
    let second = slot(&mut dictionary, "hao");
    let third = slot(&mut dictionary, "ma");
    let original = cached_rows(3, CandidateSource::AiSuggestion);
    dictionary
        .series_cache
        .insert(first.clone(), original.clone());
    dictionary
        .series_cache
        .insert(second.clone(), original.clone());
    let snapshot = dictionary.series_cache.get(&first).unwrap();
    let fields: Vec<_> = dictionary.series_cache.get_ref(&first).unwrap()[..3]
        .iter()
        .map(|row| {
            (
                row.word.as_ptr(),
                row.canonical_pinyin.as_ptr(),
                row.sentence_words[0].as_ptr(),
            )
        })
        .collect();
    assert!(dictionary.insert_online_words(
        "ni",
        "ni",
        0,
        &["合成新在线".to_owned()],
        CandidateSource::AiSuggestion
    ));
    assert_eq!(snapshot, original);
    let stored = dictionary.series_cache.get_ref(&first).unwrap();
    for (row, pointers) in stored[..3].iter().zip(fields) {
        assert_eq!(
            (
                row.word.as_ptr(),
                row.canonical_pinyin.as_ptr(),
                row.sentence_words[0].as_ptr()
            ),
            pointers
        );
    }
    dictionary.series_cache.insert(third.clone(), Vec::new());
    assert!(dictionary.series_cache.get_ref(&first).is_none());
    assert!(dictionary.series_cache.get_ref(&second).is_some());
    assert!(dictionary.series_cache.get_ref(&third).is_some());
    dictionary.reset_cache();
    assert_eq!(snapshot, original);
}

#[test]
fn missing_online_slots_keep_success_and_rejection_policy() {
    let fixture = Fixture::new();
    for (batch, source) in batches() {
        let mut before = QuanpinDictionary::new(&fixture.paths);
        let mut after = QuanpinDictionary::new(&fixture.paths);
        let key = slot(&mut before, "ni");
        assert_eq!(slot(&mut after, "ni"), key);
        let (expected, old_count) =
            count(|| before.frozen_insert_online_words("ni", "ni", 0, &batch, source));
        let (actual, new_count) =
            count(|| after.insert_online_words("ni", "ni", 0, &batch, source));
        assert_eq!((actual, new_count), (expected, old_count));
        assert_eq!(
            after.series_cache.get_ref(&key),
            before.series_cache.get_ref(&key)
        );
    }
}

fn retained_bytes(rows: &Vec<WordItem>) -> usize {
    rows.capacity() * std::mem::size_of::<WordItem>()
        + rows
            .iter()
            .map(|row| {
                row.pinyin.capacity()
                    + row.canonical_pinyin.capacity()
                    + row.word.capacity()
                    + row.corrected_from.capacity()
                    + row.sentence_words.capacity() * std::mem::size_of::<String>()
                    + row
                        .sentence_words
                        .iter()
                        .map(String::capacity)
                        .sum::<usize>()
            })
            .sum::<usize>()
}

fn replacement_sequence() -> Vec<(Vec<String>, CandidateSource)> {
    vec![
        (
            (0..10).map(|index| format!("合成批次{index}")).collect(),
            CandidateSource::AiSuggestion,
        ),
        (vec!["合成缩小".to_owned()], CandidateSource::AiSuggestion),
        (
            vec!["合成本地0".to_owned()],
            CandidateSource::CloudSuggestion,
        ),
        (
            vec!["合成云行".to_owned()],
            CandidateSource::CloudSuggestion,
        ),
        (
            vec!["合成缩小".to_owned(), "合成缩小".to_owned()],
            CandidateSource::AiSuggestion,
        ),
        (vec!["坏\n".to_owned()], CandidateSource::CloudSuggestion),
    ]
}

fn time_batch(iterations: usize, mut update: impl FnMut() -> bool) -> u128 {
    let started = std::time::Instant::now();
    for _ in 0..iterations {
        std::hint::black_box(update());
    }
    started.elapsed().as_nanos()
}

#[test]
fn repeated_online_updates_preserve_fields_and_expose_retained_capacity() {
    let fixture = Fixture::new();
    let rows = cached_rows(16, CandidateSource::AiSuggestion);
    let mut before = QuanpinDictionary::new(&fixture.paths);
    let mut after = QuanpinDictionary::new(&fixture.paths);
    let key = slot(&mut before, "ni");
    assert_eq!(slot(&mut after, "ni"), key);
    before.series_cache.insert(key.clone(), rows.clone());
    after.series_cache.insert(key.clone(), rows.clone());
    let held_before = retained_bytes(after.series_cache.get_ref(&key).unwrap());
    for (step, (batch, source)) in replacement_sequence().into_iter().enumerate() {
        assert_eq!(
            after.insert_online_words("ni", "ni", 0, &batch, source),
            before.frozen_insert_online_words("ni", "ni", 0, &batch, source)
        );
        assert_eq!(
            after.series_cache.get_ref(&key).unwrap(),
            before.series_cache.get_ref(&key).unwrap()
        );
        let old_bytes = retained_bytes(before.series_cache.get_ref(&key).unwrap());
        let new_bytes = retained_bytes(after.series_cache.get_ref(&key).unwrap());
        println!("ONLINE_CAPACITY cache=quanpin step={step} before={held_before} old={old_bytes} new={new_bytes}");
        // 更新仅保留既有页的富余容量和至多十行的新批次,没有新增槽或无限增长。
        let maximum = held_before + 2 * 10 * std::mem::size_of::<WordItem>() + 1024;
        assert!(new_bytes <= maximum);
        assert!(
            new_bytes <= old_bytes,
            "cache={} step={step}: {new_bytes} > {old_bytes}",
            "quanpin"
        );
    }
}

// 只在临时关闭计数分配器后的 release 程序中手动运行,完整保留首轮配对观测。
#[test]
#[ignore]
fn online_cache_paired_latency() {
    let fixture = Fixture::new();
    for width in [16, 128, 512] {
        for variant in 0..3 {
            let source = if variant == 1 {
                CandidateSource::CloudSuggestion
            } else {
                CandidateSource::AiSuggestion
            };
            let batch = vec![if variant == 2 {
                "坏\n"
            } else {
                "合成计时回包"
            }
            .to_owned()];
            let rows = cached_rows(width, source);
            let mut before = QuanpinDictionary::new(&fixture.paths);
            let mut after = QuanpinDictionary::new(&fixture.paths);
            let key = slot(&mut before, "ni");
            assert_eq!(slot(&mut after, "ni"), key);
            before.series_cache.insert(key.clone(), rows.clone());
            after.series_cache.insert(key.clone(), rows.clone());
            assert_eq!(
                after.insert_online_words("ni", "ni", 0, &batch, source),
                before.frozen_insert_online_words("ni", "ni", 0, &batch, source)
            );
            assert_eq!(
                after.series_cache.get_ref(&key).unwrap(),
                before.series_cache.get_ref(&key).unwrap()
            );
            let mut iterations = 32;
            while time_batch(iterations, || {
                before.frozen_insert_online_words("ni", "ni", 0, &batch, source)
            }) < 2_000_000
                && iterations < 1 << 20
            {
                iterations *= 2;
            }
            for pair in 0..40 {
                let (old_ns, new_ns) = if pair % 2 == 0 {
                    (
                        time_batch(iterations, || {
                            before.frozen_insert_online_words("ni", "ni", 0, &batch, source)
                        }),
                        time_batch(iterations, || {
                            after.insert_online_words("ni", "ni", 0, &batch, source)
                        }),
                    )
                } else {
                    let new_ns = time_batch(iterations, || {
                        after.insert_online_words("ni", "ni", 0, &batch, source)
                    });
                    (
                        time_batch(iterations, || {
                            before.frozen_insert_online_words("ni", "ni", 0, &batch, source)
                        }),
                        new_ns,
                    )
                };
                println!("ONLINE_PAIR cache=quanpin width={width} variant={variant} pair={pair} iterations={iterations} old_ns={old_ns} new_ns={new_ns}");
            }
        }
    }
}

#[test]
fn cached_online_updates_keep_resolved_input_slots() {
    let fixture = Fixture::new();
    for (key, word) in [
        ("ni", "合成单词"),
        ("ni'hao", "合成短语"),
        ("nve", "合成别名"),
        ("gua", "合成换位"),
        ("gai", "合成邻键"),
    ] {
        fixture.insert(key, word, 100);
    }
    for (raw, segmentation, types) in [
        ("nihao", "ni'hao", 0),
        ("nihao", "", 0),
        ("ni'hao", "ni'hao", 0),
        ("nue", "nue", 0),
        (
            "gau",
            "",
            autocorrect_type::TRANSPOSITION | autocorrect_type::NEIGHBOR,
        ),
    ] {
        let mut before = QuanpinDictionary::new(&fixture.paths);
        let mut after = QuanpinDictionary::new(&fixture.paths);
        let fuzzy = FuzzyPinyinOptions { rules: 0 };
        assert_eq!(
            after.query(raw, segmentation, types, fuzzy),
            before.query(raw, segmentation, types, fuzzy)
        );
        let cache_key = before.project_resolution(raw, segmentation, types, |resolution| {
            resolution.cache_key.clone()
        });
        if raw == "gau" {
            assert!(cache_key.starts_with("C:"));
        }
        let slot = before.series_slot(&cache_key);
        for source in [
            CandidateSource::AiSuggestion,
            CandidateSource::CloudSuggestion,
        ] {
            let pending = if source == CandidateSource::AiSuggestion {
                CandidateSource::AiPending
            } else {
                CandidateSource::CloudPending
            };
            for dictionary in [&mut before, &mut after] {
                dictionary
                    .series_cache
                    .get_mut(&slot)
                    .unwrap()
                    .push(WordItem::new(raw, "合成占位", 0, pending, ""));
            }
            let batch = vec!["合成异步回包".to_owned()];
            assert_eq!(
                after.insert_online_words(raw, segmentation, types, &batch, source),
                before.frozen_insert_online_words(raw, segmentation, types, &batch, source)
            );
            assert_eq!(
                after.series_cache.get_ref(&slot),
                before.series_cache.get_ref(&slot)
            );
            assert_eq!(
                after.query(raw, segmentation, types, fuzzy),
                before.query(raw, segmentation, types, fuzzy)
            );
        }
    }
}