detect: rank only systems whose source script matches the input - #28
Merged
Merged
Conversation
The detect walk loaded all 288 maps per request. On the Workers free plan (10ms CPU) that intermittently dies with 1102 — production returns anywhere between 47 and 96 partial rankings per call, or 503s. Loading every map to rank unrelated scripts was also semantically empty: Georgian input ranked 280 irrelevant systems at distance 9. detectInputScript() picks the majority Unicode script of the input (non-Latin beats Latin when tied); a build-time generated maps/scripts.js maps each system to its source script, and detect walks only the matching family (Hangul input also matches the mixed Kore sources, Han input also matches Hans/Kore/Hrkt). Georgian input now loads 8 systems instead of 288 and the top hits are unchanged. Test fixtures: the detect fixture records the prefiltered ranking.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
detecttransliterated the input through all 288 systems per request. On the Workers free plan (10 ms CPU budget) that intermittently exceeds the limit — production returns partial rankings that vary per call (47–96 entries of 288) or fails outright with error 1102 (Worker exceeded CPU time limit):Fix
detectInputScript()— majority Unicode-block scan of the input; a single non-Latin letter outranks any amount of Latin.maps/scripts.js— build-time generatedcode → source_scriptindex (emitted byscripts/build-maps.mjsfrom the artifact metadata).detect()walks only the matching family: Georgian input → 8 systems (was 288); Hangul input also matches the mixedKoresources; Han input also matchesHans/Kore/Hrkt.Top hits are unchanged for real queries (
ქართული→alalc-kat-Geor-Latn-1997@ 0,한국→ distance 0,阜康→acadsin-zho).Tests
test/detect.test.ts: script detection, prefilter (asserts the asset-fetch count drops to ≤ 10), Korean/Han family matching, script-less inputCapacity note for the owner (not addressed here)
The API sits on the Workers free plan (10 ms CPU); even single transliterations of library-dependent maps 1102 on cold isolates. The durable fix is a plan tier with real CPU — Workers Standard (usage-priced) plus
limits.cpu_msin the deploywrangler.jsonc. That is a spending decision, so it is only documented here.