Closed Bug 1746380 Opened 3 years ago Closed 3 years ago

Split string allocation code for atom and non-atom case

Categories

(Core :: JavaScript Engine, task, P3)

task

Tracking

()

RESOLVED FIXED
98 Branch
Tracking Status
firefox98 --- fixed

People

(Reporter: arai, Assigned: arai)

References

Details

Attachments

(5 files)

There are cx->zone()->isAtomsZone() branch deep inside string allocation code.
The condition becomes true only when the caller has AutoAllocInAtomsZone or JSContext::enterAtomsZone.
and now we have very limited set of callsite that allocates atoms.
we should be able to split the allocation code for each, maybe by templatizing the functions if necessary, to optimize both atom case and non-atom case.

https://searchfox.org/mozilla-central/rev/b80b92e2e10299ab405bdc4eb5109e9e23e25160/js/src/vm/StringType-inl.h#253-266

template <js::AllowGC allowGC, typename CharT>
MOZ_ALWAYS_INLINE JSLinearString* JSLinearString::new_(
    JSContext* cx, js::UniquePtr<CharT[], JS::FreePolicy> chars, size_t length,
    js::gc::InitialHeap heap) {
  if (!validateLength(cx, length)) {
    return nullptr;
  }

  JSLinearString* str;
  if (cx->zone()->isAtomsZone()) {
    str = js::Allocate<js::NormalAtom, allowGC>(cx);
  } else {
    str = js::AllocateString<JSLinearString, allowGC>(cx, heap);
  }

https://searchfox.org/mozilla-central/rev/b80b92e2e10299ab405bdc4eb5109e9e23e25160/js/src/vm/StringType-inl.h#308-315

template <js::AllowGC allowGC>
MOZ_ALWAYS_INLINE JSThinInlineString* JSThinInlineString::new_(
    JSContext* cx, js::gc::InitialHeap heap) {
  if (cx->zone()->isAtomsZone()) {
    return (JSThinInlineString*)(js::Allocate<js::NormalAtom, allowGC>(cx));
  }

  return js::AllocateString<JSThinInlineString, allowGC>(cx, heap);

https://searchfox.org/mozilla-central/rev/b80b92e2e10299ab405bdc4eb5109e9e23e25160/js/src/vm/StringType-inl.h#318-325

template <js::AllowGC allowGC>
MOZ_ALWAYS_INLINE JSFatInlineString* JSFatInlineString::new_(
    JSContext* cx, js::gc::InitialHeap heap) {
  if (cx->zone()->isAtomsZone()) {
    return (JSFatInlineString*)(js::Allocate<js::FatInlineAtom, allowGC>(cx));
  }

  return js::AllocateString<JSFatInlineString, allowGC>(cx, heap);

To avoid duplicate steps through the atomization process:

  • If the function assumes the input doesn't match static strings
    (static strings case is already handled), append NoStatic
  • If the function assumes the length is valid, append ValidLength

Depends on D135099

Split cx->zone()->isAtomsZone() branch into 2 functions, for atom case and
non-atom case.

Also removed reduncent steps (static string lookup, length validation,
deflation) from atom case, based on the atomization progress.

Depends on D135100

the stack of bug 1745664, bug 1746382, and this bug improves the atomization performance by 2% for both self-case (InstantiateMarkedAtomsAsPermanent) and normal case (InstantiateMarkedAtoms).

Attachment #9257618 - Attachment description: Bug 1746380 - Part 5: Remove unnecessary check for null-zone. r?tcampbell! → Bug 1746380 - Part 5: Optimize well-known symbol construction and remove unnecessary check for null-zone. r?tcampbell!
Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/eeb21b8d0aa9 Part 1: Clarify atomization progress in the function name. r=nbp https://hg.mozilla.org/integration/autoland/rev/006da995143d Part 2: Move atom-specific path out of string allocation code. r=nbp https://hg.mozilla.org/integration/autoland/rev/cafa79c30500 Part 3: Remove length validation from atom instantiation. r=nbp https://hg.mozilla.org/integration/autoland/rev/9e4cdff09263 Part 4: Remove length validation and static string lookup from non-atom string instantiation. r=nbp https://hg.mozilla.org/integration/autoland/rev/7269c2e3b6f8 Part 5: Optimize well-known symbol construction and remove unnecessary check for null-zone. r=nbp
Blocks: 1745687

turns out that we have yet another place that atomizes strings without zone, in Gecko side.

https://searchfox.org/mozilla-central/rev/8d108a59d067ce37671090b0b1972ee8adfb7196/js/xpconnect/src/XPCJSContext.cpp#1410-1413

// static
XPCJSContext* XPCJSContext::NewXPCJSContext() {
  XPCJSContext* self = new XPCJSContext();
  nsresult rv = self->Initialize();

https://searchfox.org/mozilla-central/rev/8d108a59d067ce37671090b0b1972ee8adfb7196/js/xpconnect/src/XPCJSContext.cpp#1387-1388

nsresult XPCJSContext::Initialize() {
...
  nsresult rv =
      CycleCollectedJSContext::Initialize(nullptr, JS::DefaultHeapMaxBytes);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  MOZ_ASSERT(Context());
  JSContext* cx = Context();
...
  MOZ_RELEASE_ASSERT(Runtime()->InitializeStrings(cx),
                     "InitializeStrings failed");

https://searchfox.org/mozilla-central/rev/8d108a59d067ce37671090b0b1972ee8adfb7196/js/xpconnect/src/XPCJSRuntime.cpp#3077-3082

bool XPCJSRuntime::InitializeStrings(JSContext* cx) {
  // if it is our first context then we need to generate our string ids
  if (JSID_IS_VOID(mStrIDs[0])) {
    RootedString str(cx);
    for (unsigned i = 0; i < XPCJSContext::IDX_TOTAL_COUNT; i++) {
      str = JS_AtomizeAndPinString(cx, mStrings[i]);

This could also be true in other embedding...

Attachment #9257618 - Attachment description: Bug 1746380 - Part 5: Optimize well-known symbol construction and remove unnecessary check for null-zone. r?tcampbell! → Bug 1746380 - Part 5: Optimize well-known symbol construction. r?tcampbell!
Flags: needinfo?(arai.unmht)
Pushed by arai_a@mac.com: https://hg.mozilla.org/integration/autoland/rev/e23ecacc7e1a Part 1: Clarify atomization progress in the function name. r=nbp https://hg.mozilla.org/integration/autoland/rev/008f15149504 Part 2: Move atom-specific path out of string allocation code. r=nbp https://hg.mozilla.org/integration/autoland/rev/7bb53daa9da3 Part 3: Remove length validation from atom instantiation. r=nbp https://hg.mozilla.org/integration/autoland/rev/d772c5df1e8f Part 4: Remove length validation and static string lookup from non-atom string instantiation. r=nbp https://hg.mozilla.org/integration/autoland/rev/7b575dd3ddc2 Part 5: Optimize well-known symbol construction. r=nbp
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: