Skip to main content

2022-07-19

Work Researchโ€‹

์˜ค๋Š˜์€ ๋ฐ˜๋“œ์‹œ WorkerDOM Scheduler๋ฅผ ์žก์•„๋‚ด์ž. 2022-07-11์— ํ™•์ธํ•œ 2๊ฐ€์ง€ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ™•์ธํ•œ๋‹ค. Web Worker Thread์—์„œ ๋‚˜ํƒ€๋‚œ AnimationFrame.ts์€ requestAnimationFrame์— ๊ด€๋ จ๋œ ๊ฒƒ์œผ๋กœ ๋ณด์ด๊ณ  Data Transfer๊ณผ๋Š” ๋ฌด๊ด€ํ•ด๋ณด์ธ๋‹ค (๋ถˆํ™•์‹ค). ์ฆ‰ ์ž์„ธํ•˜๊ฒŒ ๋ด์•ผํ•˜๋Š” ๊ฒƒ์€ WorkerDOMConfiguration์— ๋‚˜์˜จ ๋‹ค์Œ ๋ถ€๋ถ„์ด๋‹ค.

export interface WorkerDOMConfiguration {
// ...
// ---- Optional, with defaults
// Schedules mutation phase.
mutationPump: MutationPumpFunction

// ---- Optional Overrides
// Schedules long task.
longTask?: LongTaskFunction
// ...
}

LongTaskFunction์˜ ์ฐจ์ด๋Š” Promise๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. MutationPumpFunction์˜ ํƒ€์ž… ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

export type MutationPumpFunction = (flush: Function, phase: Phase) => void

flush๋Š” ๋ฌด์–ธ๊ฐ€ ๋’ท์ •๋ฆฌ๋ฅผ ํ•˜๋Š” ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค (๋ถˆํ™•์‹ค). Phase๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

export const enum Phase {
Initializing = 0,
Hydrating = 1,
Mutating = 2,
}

InboundWorkerDOMConfiguration๊ณผ WorkerDOMConfiguration์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ๊ฐ€? ์šฐ์„  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณ€ํ™˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. InboundWorkerDOMConfiguration์—๋Š” ์—†๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด์ค€๋‹ค.

export function normalizeConfiguration(config: InboundWorkerDOMConfiguration): WorkerDOMConfiguration {
return Object.assign(
{},
{
mutationPump: requestAnimationFrame.bind(null),
executorsAllowed: DefaultAllowedMutations,
},
config
)
}

InboundWorkerDOMConfiguration ๊ทธ๋ฆฌ๊ณ  normalizeConfiguration์ด Main Thread install.ts์—๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ ์ตœ์ดˆ ์„ค์น˜ ์‹œ์—๋งŒ InboundWorkerDOMConfiguration์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

๋„๋Œ€์ฒด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์Šค์ผ€์ค„๋Ÿฌ๊ฐ€ ๋™์ž‘ํ•˜๋Š”์ง€ ์•„์ง ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค. ์•„์˜ˆ ์Šค์ผ€์ค„๋Ÿฌ๊ฐ€ ์˜ˆ์ƒํ–ˆ๋˜ ๋™๊ธฐ์ ์ธ ํƒ€์ด๋จธ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค (๋ถˆํ™•์‹ค). requestAnimationFrame์— ์˜ํ•ด ๋™์ž‘ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค (๋ถˆํ™•์‹ค). ์ด ๊ฐ€์ •์ด ๋งž๋‹ค๋ฉด requestAnimationFrame์ด ์–ด๋–ค ์‹์œผ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

Timโ€‹

Tim๊ณผ 2์‹œ๊ฐ„ ๊ฐ€๋Ÿ‰ ์ด์•ผ๊ธฐํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • (๋ถˆํ™•์‹ค)
  • Web Worker Thread์˜ MutationTransfer.ts์— transferSync ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋“ ๋‹ค.
  • TransferrableKeys์— Sync ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ๋™๊ธฐ์ ์ธ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ
    • Web Worker Thread transferSync์—์„œ SAB๋ฅผ ํ•˜๋‚˜ ํ• ๋‹นํ•œ๋‹ค
    • TransferrableKeys๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ๋‹ค
    • Main Thread์—์„œ Atomic Wait๋ฅผ ๊ฑด๋‹ค
    • Main Thread์˜ ํ•ด๋‹น ํ”„๋กœ์„ธ์„œ์—์„œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ๋‹ค
    • ๋ฐ›์€ SAB์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฑ„์šด๋‹ค
    • Web Worker Thread์— Atomic Notify๋ฅผ ํ•œ๋‹ค

(๋ถˆํ™•์‹ค) Message ํ˜•์‹์€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๊ณ  Processor๋Š” ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

Personal Researchโ€‹