Closed Bug 1687095 Opened 4 years ago Closed 4 years ago

Merge delazification stencil into initial stencil before encoding

Categories

(Core :: JavaScript Engine, task, P3)

task

Tracking

()

RESOLVED FIXED
88 Branch
Tracking Status
firefox88 --- fixed

People

(Reporter: arai, Assigned: arai)

References

Details

Crash Data

Attachments

(7 files, 6 obsolete files)

48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review
48 bytes, text/x-phabricator-request
Details | Review

In incremental stencil XDR, we encode initial stencil first, and then encode each delazification after that.

it means that, if a script is delazified, we have both lazy script data and delazified script data inside XDR.
ultimately, the lazy script data isn't necessary in the instantiated GC objects.

if we can somehow dedup the info, or drop unnecessary info, we can reduce the XDR size.

Blocks: 1687143
No longer blocks: 1609486

the lazy script data is necessary for the current instantiation steps, because we first instantiate initial stencil,
and then apply delazification stencils' instantiation, just like normal delazification.

if we somehow drop the first lazy data, the steps needs to be modified.

we had an idea about merging stencils on decode, before instantiation.
https://phabricator.services.mozilla.com/D90574
https://phabricator.services.mozilla.com/D90585
we might be able to revisit it.

With current structure, we cannot remove a certain lazy script data from XDR data with simple move/copy operation, because data is stored into multiple spans,
and removing an item breaks the remaining part's indices (or, if we don't shift the items, it doesn't affect the space).

So, to remove the space taken by lazy script, we need to decode XDR data, and then remove the data taken by the lazy script (while keeping the lazy script's index itself as placeholder), and renumber remaining things, and then re-encode it at linearlize step. This needs to be operated recursively for all delazifications.
And when decoding, we need to merge all stencils into one, while ignoring the removed data.
The cost would almost be same as just decoding, merging, and re-encoding all stencils at linearlize step.

Either way, it's not good because it's performed on main thread, and taking long there results in long interruption.

So, if we want to reduce XDR size with such way, we should think about offloading the linearlize or the post-process work to helper thread.

If we can bring initial CompilationStencil (or gc-free subset) around with ScriptSorce or somewhere while incremental encoding,
we can merge delazification into the initial stencil there, and encode the entire stencil at once when finalizing.
so that there's no duplication around script data, and also bug 1687094 will also be solved at the same time.

Assignee: nobody → arai.unmht
Status: NEW → ASSIGNED
Summary: Delazified script's lazy data takes much space in incremental stencil XDR → Investigate merging delazification stencil into initial stencil before encoding

Initial CompilationStencil's fields that is necessary for merging and encoding:

  • all BaseCompilationStencil fields
    • delazifications are merged into them
  • CompilationStencil.alloc
    • all delazification should use this allocator, given the result will be merged and held by initial stencil
  • CompilationStencil.input.options.instrumentationKinds
    • just to check if the stencil is encodable. this can be done earlier
  • CompilationStencil.moduleMetadata
  • CompilationStencil.scriptExtra

In addition to that, it would be nice to reuse initial compilation's CompilationState.parserAtoms in delazification compilation,
so that atom indices created in delazifications doesn't require remapping when merging.

the difference between the previous merge patch and the current situation is:

  • CompilationStencil was using Vector, but now it uses Span, and it's not extensible
  • CompilationStencil was using ParserAtomsTable but now it uses ParserAtomSpan, and cannot intern/add ParserAtom
  • most data was using SystemAllocPolicy, but now all data is stored in LifoAlloc

so, if we keep using Span + LifoAlloc, merging (appending/concatenating) on each delazification isn't efficient.
it's better not to append/concatenate the span content on each delazification, but keep those Spans in "vector of Span",
and when finishing, concatenate all spans into single Vector, with mapping indices.

another solutions would be:

  • add non-Span variant of CompilationStencil (Vector + ParserAtomsTable), and use the previous way for encoding
  • make CompilationState portable, and store it in ScriptSource, and reuse it across initial and delazification, and call CompilationState::finish when finishing encoding

The reason why we chose Span was:

  • a) to avoid alloc/ctor/dtor cost of Vector
  • b) to directly use XDR buffer

for (a), if we don't store delazifications in XDR, the cost of Vector alloc/ctor/dtor on decode would be negligible,
but (b) still holds and we need Span, or at least variant.

about reusing ParserAtomsTable:

if we reuse ParserAtomsTable across initial and delazification, we can get consistent TaggedParserAtomIndex, but
instantiation has problem, that delazification has to instantiate more atoms than current way, given
we don't have info that which atom is used by current compilation (delazification),
and also we don't keep atomCache across initial/delazification.

the simplest way would be, just collect all CompilationStencil/BaseCompilationStencil references in the encoder,
and merge them while encoding.

we can use StencilDelazificationSet (or RefPtr/UniquePtr variant) for collecting them

Depends on: 1692648

With WIP patch that merges stencils before encoding, the size of raptor yahoo-mail, mail_app_es6_09* XDR is reduced by 14%.
there, AtomTable is reduced by 51%, and ScriptData is also reduced by 51%.

Summary: Investigate merging delazification stencil into initial stencil before encoding → Merging delazification stencil into initial stencil before encoding
Summary: Merging delazification stencil into initial stencil before encoding → Merge delazification stencil into initial stencil before encoding

To return Vector-variant of CompilationStencil as the result of compilation,
it needs to be allocated separately from other CompilationState.

Also add XDRStencilEncoder for non-incremental case, and
cleanup XDRStencilDecoder.

StencilDelazificationSet and gcOutputForDelazification become unused,
and will be removed by later patches.

Now all stencils don't have associated delazification.

Attachment #9203169 - Attachment description: Bug 1687095 - Part 2: Support encoding ExtensibleCompilationStencil, and use ExtensibleCompilationStencil in self-hosting. → Bug 1687095 - Part 2: Support encoding ExtensibleCompilationStencil.
Attachment #9203170 - Attachment description: Bug 1687095 - Part 3: Use ExtensibleCompilationStencil in self-hosting. → Bug 1687095 - Part 3: Use ExtensibleCompilationStencil as a compilation output in self-hosting.
Attachment #9203170 - Attachment is obsolete: true
Attachment #9203171 - Attachment description: Bug 1687095 - Part 4: Make CmpilationStencil and ExtensibleCompilationStencil convertible each other. → Bug 1687095 - Part 3: Make CmpilationStencil and ExtensibleCompilationStencil convertible each other.
Attachment #9203172 - Attachment description: Bug 1687095 - Part 5: Add CompilationStencilMerger. → Bug 1687095 - Part 4: Add CompilationStencilMerger.
Attachment #9203173 - Attachment description: Bug 1687095 - Part 6: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. → Bug 1687095 - Part 5: Use CompilationStencilMerger in XDRIncrementalStencilEncoder.
Attachment #9203174 - Attachment description: Bug 1687095 - Part 7: Remove StencilDelazificationSet. → Bug 1687095 - Part 6: Remove StencilDelazificationSet.
Attachment #9203175 - Attachment description: Bug 1687095 - Part 8: Remove gcOutputForDelazification. → Bug 1687095 - Part 7: Remove gcOutputForDelazification.
Attachment #9203176 - Attachment description: Bug 1687095 - Part 9: Merge AbstractBaseCompilationStencil and AbstractCompilationStencil. → Bug 1687095 - Part 8: Merge AbstractBaseCompilationStencil and AbstractCompilationStencil.

After removing BaseCompilationStencil, we could make the instantiation related methods non-static again.

Blocks: 1692130
Blocks: 1693184
Blocks: 1693611
Attachment #9203169 - Attachment is obsolete: true
Attachment #9203168 - Attachment description: Bug 1687095 - Part 1: Move Vector-variant of CompilationStencil out of CompilationState. → Bug 1687095 - Part 1: Move Vector-variant of CompilationStencil out of CompilationState. r=tcampbell
Attachment #9203171 - Attachment description: Bug 1687095 - Part 3: Make CmpilationStencil and ExtensibleCompilationStencil convertible each other. → Bug 1687095 - Part 2: Make CmpilationStencil and ExtensibleCompilationStencil convertible each other. r=tcampbell
Attachment #9203172 - Attachment description: Bug 1687095 - Part 4: Add CompilationStencilMerger. → Bug 1687095 - Part 3: Add CompilationStencilMerger. r=tcampbell

BorrowingCompilationStencil provides a CompilationStencil instance that
borrows all ExtensibleCompilationStencil data,

Not-borrow-able fields need to be moved to the temporary CompilationStencil,
and BorrowingCompilationStencil destructor returns them.

Depends on D105153

Attachment #9203173 - Attachment description: Bug 1687095 - Part 5: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. → Bug 1687095 - Part 5: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell
Attachment #9203174 - Attachment description: Bug 1687095 - Part 6: Remove StencilDelazificationSet. → Bug 1687095 - Part 6: Remove StencilDelazificationSet. r=tcampbell
Attachment #9203175 - Attachment description: Bug 1687095 - Part 7: Remove gcOutputForDelazification. → Bug 1687095 - Part 7: Remove gcOutputForDelazification. r=tcampbell
Attachment #9203176 - Attachment description: Bug 1687095 - Part 8: Merge AbstractBaseCompilationStencil and AbstractCompilationStencil. → Bug 1687095 - Part 8: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell
Attachment #9203309 - Attachment description: Bug 1687095 - Part 9: Use ExtensibleCompilationStencil as off-thread-script-compilation output. → Bug 1687095 - Part 9: Use ExtensibleCompilationStencil as off-thread-script-compilation output. r=tcampbell
Attachment #9203309 - Attachment description: Bug 1687095 - Part 9: Use ExtensibleCompilationStencil as off-thread-script-compilation output. r=tcampbell → Bug 1687095 - Part 9: Use ExtensibleCompilationStencil as off-thread-script-compilation output. r=tcampbell!
Attachment #9203168 - Attachment is obsolete: true
Attachment #9204129 - Attachment is obsolete: true
Attachment #9203309 - Attachment is obsolete: true
Attachment #9203171 - Attachment is obsolete: true

To make it possible to convert them each other.

Depends on D105908

Attachment #9203172 - Attachment description: Bug 1687095 - Part 3: Add CompilationStencilMerger. r=tcampbell → Bug 1687095 - Part 2: Add CompilationStencilMerger. r=tcampbell!
Attachment #9203173 - Attachment description: Bug 1687095 - Part 5: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell → Bug 1687095 - Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell!
Attachment #9203174 - Attachment description: Bug 1687095 - Part 6: Remove StencilDelazificationSet. r=tcampbell → Bug 1687095 - Part 4: Remove StencilDelazificationSet. r=tcampbell!
Attachment #9203175 - Attachment description: Bug 1687095 - Part 7: Remove gcOutputForDelazification. r=tcampbell → Bug 1687095 - Part 5: Remove gcOutputForDelazification. r=tcampbell!
Attachment #9203176 - Attachment description: Bug 1687095 - Part 8: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell → Bug 1687095 - Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell!
Depends on: 1695088
Attachment #9204472 - Attachment description: Bug 1687095 - Part 1: Add {CmpilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell! → Bug 1687095 - Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell!

Since merging stencils is a read-only operation for the source delazification
stencil and we already have a borrowed stencil at caller, it is more
consistent with our conventions to pass a CompilationStencil& instead of an
ExtensibleCompilationStencil&.

Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/07d2db86c609 Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/6ab4cbb8f4a2 Part 2: Add CompilationStencilMerger. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/8e59cd75371f Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/d1fc4ee5aa50 Part 4: Remove StencilDelazificationSet. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/1ad324a72551 Part 5: Remove gcOutputForDelazification. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/13b97543f899 Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/efff9a5c7a1f Use CompilationStencil& argument to incremental-encoder. r=arai

Backed out for frequent crashes in Nightly:

https://hg.mozilla.org/mozilla-central/rev/432c586951243c132694fa9988a691fbf5198cca

Example of crashes: bp-6d1d6c1a-c45e-4277-9d9b-12c3d0210303

 0 	xul.dll	static js::frontend::CompilationStencil::instantiateStencilAfterPreparation(JSContext*, js::frontend::CompilationInput&, js::frontend::CompilationStencil const&, js::frontend::CompilationGCOutput&)	js/src/frontend/Stencil.cpp:1341
1 	xul.dll	js::frontend::InstantiateStencils(JSContext*, js::frontend::CompilationInput&, js::frontend::CompilationStencil const&, js::frontend::CompilationGCOutput&)	js/src/frontend/BytecodeCompiler.cpp:386
2 	xul.dll	js::ParseTask::instantiateStencils(JSContext*)	js/src/vm/HelperThreads.cpp:722
3 	xul.dll	js::ScriptDecodeTask::parse(JSContext*)	js/src/vm/HelperThreads.cpp:836
4 	xul.dll	js::ParseTask::runHelperThreadTask(js::AutoLockHelperThreadState&)	js/src/vm/HelperThreads.cpp:619
5 	xul.dll	static js::HelperThread::ThreadMain(void*)	js/src/vm/HelperThreads.cpp:2366
6 	xul.dll	static js::detail::ThreadTrampoline<void (&)(void*), js::HelperThread*>::Start(void*)	js/src/threading/Thread.h:205
7 	ucrtbase.dll	thread_start<unsigned int (__cdecl*)(void*), 1>	
8 	kernel32.dll	BaseThreadInitThunk	
9 	ntdll.dll	RtlUserThreadStart	
10 	kernelbase.dll	TerminateProcessOnMemoryExhaustion	

Nightly updates have been halted and will resume once the new nightly builds with the backout are available.

Status: RESOLVED → REOPENED
Crash Signature: [@ js::frontend::CompilationStencil::instantiateStencilAfterPreparation ]
Flags: needinfo?(arai.unmht)
Resolution: FIXED → ---
Target Milestone: 88 Branch → ---
Attachment #9204472 - Attachment description: Bug 1687095 - Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell! → Bug 1687095 - Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell
Attachment #9203172 - Attachment description: Bug 1687095 - Part 2: Add CompilationStencilMerger. r=tcampbell! → Bug 1687095 - Part 2: Add CompilationStencilMerger. r=tcampbell
Attachment #9203173 - Attachment description: Bug 1687095 - Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell! → Bug 1687095 - Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell
Attachment #9203174 - Attachment description: Bug 1687095 - Part 4: Remove StencilDelazificationSet. r=tcampbell! → Bug 1687095 - Part 4: Remove StencilDelazificationSet. r=tcampbell
Attachment #9203175 - Attachment description: Bug 1687095 - Part 5: Remove gcOutputForDelazification. r=tcampbell! → Bug 1687095 - Part 5: Remove gcOutputForDelazification. r=tcampbell
Attachment #9203176 - Attachment description: Bug 1687095 - Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell! → Bug 1687095 - Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell
Attachment #9206524 - Attachment description: Bug 1687095 - Use CompilationStencil& argument to incremental-encoder. r?arai! → Bug 1687095 - Use CompilationStencil& argument to incremental-encoder. r=arai
Attachment #9203172 - Attachment description: Bug 1687095 - Part 2: Add CompilationStencilMerger. r=tcampbell → Bug 1687095 - Part 2: Add CompilationStencilMerger. r=tcampbell!
Attachment #9203173 - Attachment description: Bug 1687095 - Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell → Bug 1687095 - Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell!
Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/aef34d5f65e6 Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/34a37bdb73ac Part 2: Add CompilationStencilMerger. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/1f89cffbd3d5 Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/4f27f5ca8656 Part 4: Remove StencilDelazificationSet. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/075c72b787a2 Part 5: Remove gcOutputForDelazification. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/d7815415080c Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/9419ba69b445 Use CompilationStencil& argument to incremental-encoder. r=arai
Flags: needinfo?(arai.unmht)

it's bug 1696205 patch.
will fix there.

Flags: needinfo?(arai.unmht)
Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/19f9592d23c1 Part 1: Add {CompilationStencil,ExtensibleCompilationStencil}::steal. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/cf67566b48ab Part 2: Add CompilationStencilMerger. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/21e179ac5547 Part 3: Use CompilationStencilMerger in XDRIncrementalStencilEncoder. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/b2c2e8ec8016 Part 4: Remove StencilDelazificationSet. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/de2a4e293b88 Part 5: Remove gcOutputForDelazification. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/2ccfa2d73d3c Part 6: Merge BaseCompilationStencil and CompilationStencil. r=tcampbell https://hg.mozilla.org/integration/autoland/rev/0e8c6b3bd1e2 Use CompilationStencil& argument to incremental-encoder. r=arai
See Also: → 1697952
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: