Skip to content

Batches மற்றும் hot paths

sh
rust-anything bridge <repo> <unit> --batch fn:param    # or: rust-anything refresh <crate> --batch fn:param

--batch param values-ஐ ஒரே தடவை எடுத்து Rust-ல வச்சுக்கிற ஒரு class-ஐ உருவாக்கும்: new FnBatch(items) (TS, Python, Java), Mod::FnBatch.new(items) (Ruby), NewFnBatch(items) (Go). ஒவ்வொரு query-க்கும், run(<other args>) எல்லா results-ஐயும் ஒரே array-ஆ return பண்ணும், மற்றும் rank(<other args>, min) சிறந்ததை முதல்ல வச்சு item indexes-ஐ return பண்ணும். இது எல்லா cores-லயும் run ஆகும், மற்றும் errors original-ஓட அதே மாதிரி இருக்கும். TypeScript/JavaScript, Python, Go, Ruby, Java மற்றும் Kotlin ஆகியவற்றுக்கு மட்டுமே batches கிடைக்கும் (C, PHP, C#, PowerShell மற்றும் Swift-க்கு இல்ல).

ஒரு item-க்கு ஒரு தடவை call செய்யப்படுற port ஒவ்வொரு item-க்கும் bridge செலவைக் கொடுக்கும், அதனால JIT கிட்டத் தோத்துப் போகும். ஜெயிக்கிற வழி loop-ஐ நகர்த்துறது தான்: profile அதைக் கண்டுபிடிக்கும், மற்றும் --batch items-ஐ ஒரே தடவை எடுத்துக்கிட்டு ஒவ்வொரு call-க்கும் அவை எல்லாத்தின் மேலயும், எல்லா cores-லயும், typed-array results கூட function-ஐ run பண்ற class-ஐ உருவாக்கும்.

sh
rust-anything profile ../x18n -- node --experimental-strip-types $PWD/demo/x18n-search/search.ts .
# -> fuzzyScore: a quarter of the run, called per key by `matched`: batch it
rust-anything bridge ../x18n packages/core/src/fuzzy.ts --batch fuzzyScore:text
js
const search = new FuzzyScoreBatch(keys)          // once
const order = search.rank(query, 0)               // per keystroke: indexes, best first

port.rs-ல இருக்குற ஒரு FuzzyScoreIndex struct, default loop-க்கு பதிலா items-ஐ ஒரே தடவை தயார் பண்ணி தானாகவே rank பண்ணக்கூடிய (rank) loop-ஐ மாற்றும்: அது keys-ஐ lowercase பண்ணி ஒவ்வொன்னுக்கும் ஒரு character mask கூட வச்சுக்கும், ஒவ்வொரு common character-க்கும் ஒவ்வொரு key-லயும் அதோட முதல் position-க்கான ஒரு table (ஒரு எழுத்து query என்பது ஒரே ஒரு column), மற்றும் கடைசி query-ஓட matches (s-க்கு அப்புறம் se type பண்ணும்போது s match பண்ணினதை மட்டும் மறுபடியும் score பண்ணும்). 50,000 keys-ல x18n-ஓட sidebar search-ல, எழுத்து எழுத்தா type பண்ணும்போது, துல்லியமா அதே முடிவுகளோட:

ஒரு query-க்கு vs TS
TS: map(fuzzyScore), filter, sort 10 ms 1x
அதே narrowing கூட TS 4.1-5.4 ms 2x
அதே port, ஒரு key-க்கு ஒரு தடவை call செய்யப்பட்டது - 0.2-0.4x
FuzzyScoreBatch, default index (4 cores) 5.1 ms 1.7x
FuzzyScoreBatch உடன் FuzzyScoreIndex (napi, 4 cores) 0.45-0.56 ms 18-22x
அதே wasm உடன் (ஒரு thread) 0.95 ms 11.5x

அதே narrowing பண்ற TS-ஐ விடவும், Rust இன்னும் 7-12x வேகமானது (wasm 5x). Return ஆன indexes-ஐ ஒரு சாதாரண loop வச்சு keys-ஆ மாத்துங்க: Array.from(order, i => keys[i]) 20x அதிகமா, 0.4 ms செலவாச்சு.

TypeScript/JavaScript, Python, Go, Ruby, Java மற்றும் Kotlin ஆகியவற்றுக்கு மட்டுமே batches support செய்யப்படுது (C, PHP, C#, PowerShell மற்றும் Swift-க்கு இல்ல). கீழே, Rust batch 4 cores-லயும், original code எழுதப்பட்ட மாதிரி original ஒரு thread-லயும் run ஆகுது. ஒரே மாதிரியான முடிவுகள் மற்றும் errors (benchmarks அந்தந்த demo folder-ல இருக்கு):

மொழி Workload Original (1 thread) Per-call port vs original Batch (4 cores) vs original
TS x18n search, 50,000 keys 10 ms 0.2-0.4x 20x
Python slugify, 20,000 strings 98 ms - 17x
Ruby slugify, 20,000 strings 110 ms - 12.7x
Ruby shellescape (stdlib), 50,000 mixed values 88 ms - 11.8x

Go மற்றும் Java தவிர்க்கப்பட்டன: அவை original-ஐயே 4 threads-ல அதே அளவு சுலபமா run பண்ண முடியும், அப்படி அளக்கும்போது Java original Rust batch-ஐ முந்துது (#110). Batch crates mimalloc வச்சு allocate பண்ணும்: system allocator வச்சா Go batch ஒரு thread-ஐ விட மெதுவா இருந்துச்சு. அதுக்கு பதிலா ஒரு pure-Rust allocator-ஐத் தேர்வு செய்ய bridge --allocator rusty_alloc பயன்படுத்தலாம். Demo: demo/x18n-search.

வேகமான index எழுதுறது

  • Caller-ஐ ஒவ்வொரு query-க்கும் இல்லாம, ஒரே தடவை batch-ஐ build பண்ண வைங்க. Return ஆன indexes-ஐ ஒரு சாதாரண loop வச்சு items-க்கு map பண்ணுங்க.
  • Items-ஐ ஒரே தடவை தயார் பண்ண (lowercase, masks, position tables), port.rs-ல new(items) மற்றும் run(<other args>) கூட pub struct FnIndex define பண்ணுங்க. அது ஒவ்வொரு item-ஆ function-ஓட அதே முடிவைத் தரணும்.
  • Ranking-ஆல வேலையை skip பண்ண முடியும்போது, index-க்கு அதோட சொந்த rank(<other args>, min) கொடுங்க. அது function-ஓட முடிவுகளை rank பண்றதுக்கு match ஆகணும்: score அதிகம் முதல் குறைவு வரை, ties item வரிசையில்.
  • ஒரு batch port-ஓட per-item வேகத்தை cores எண்ணிக்கையால பெருக்கும். Compiled originals (Go, Java) எதிரா port தானாவே வேகமா இருக்கணும்: Unicode tables இல்லாம ASCII-ஐக் கையாளுங்க, ஒவ்வொரு முடிவையும் ஒரே pre-sized String-ல build பண்ணுங்க, இடைப்பட்ட copies-ஐத் தவிர்க்கவும்.
  • Go மற்றும் Java-ல வேலை முடிஞ்சதும் batch-ஐ close பண்ணுங்க (finalizers ஒரு fallback மட்டும் தான்).
GitHub-ல இந்தப் பக்கத்தை edit பண்ணுங்க →