Closed Bug 1799025 Opened 2 years ago Closed 2 years ago

Optimize hasOwnProperty and getProp inside for-in loops

Categories

(Core :: JavaScript Engine: JIT, task, P1)

task

Tracking

()

RESOLVED FIXED
111 Branch
Tracking Status
firefox111 --- fixed

People

(Reporter: iain, Assigned: iain)

References

(Blocks 2 open bugs)

Details

(Whiteboard: [sp3])

Attachments

(13 files)

(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details

For code like the following:

for (var key in obj) {
  if (!obj.hasOwnProperty(key)) { continue; }
  doSomethingWith(obj[key]);
}

We generate a megamorphic hasProp and a megamorphic getProp. In both cases, we can often do better. If, as is very likely, obj does not have any enumerable properties on its proto chain, then hasOwnProperty is always true. For the getProp, we can store information about how to access the most recent property in the iterator, and then use that instead of a megamorphic lookup.

V8 implemented this. It looks like it's one of the major differences between the number of megamorphic property accesses we generate in React-based benchmarks and the number generated in Chrome.

We should implement the same optimization.

I tried implementing the fusing of for..in + hasOwnProperty a few years ago in bug 1356315.

Instead of passing around ever-increasing numbers of arguments, it makes sense to wrap this up in a class.

Note that PropertyIndex can support own dense elethe indices optimization works on objects with dense elements, so long as they are not on the proto chain. We can't cache the iterator, but we can store indices for it.

Depends on D165206

This patch adds support for producing indices. Subsequent patches will store them in the NativeIterator and use them.

Depends on D165207

Drive-by cleanup

Depends on D165208

Depends on D165209

Prior to this patch, we would not attach an IC the first time we saw an object in a for-in loop, because we hadn't created a cached iterator yet. This makes writing testcases a bit awkward, and doesn't seem good for performance.

Depends on D165210

We use this in the next two patches to add optional fast-paths for hasProp and getProp when indices are available.

Depends on D165211

Depends on D165212

Depends on D165213

Depends on D165214

Depends on D165215

Blocks: 1810243
No longer blocks: 1801189
Attachment #9309250 - Attachment description: Bug 1799025: Part 1: Add PropertyEnumerator r=jandem → Bug 1799025: Part 1: Add PropertyEnumerator r=jandem!
Attachment #9309251 - Attachment description: Bug 1799025: Part 2: Add PropertyIndex r=jandem → Bug 1799025: Part 2: Add PropertyIndex r=jandem!
Attachment #9309252 - Attachment description: Bug 1799025: Part 3: Support indices in PropertyEnumerator r=jandem → Bug 1799025: Part 3: Support indices in PropertyEnumerator r=jandem!
Attachment #9309253 - Attachment description: Bug 1799025: Part 4: Remove unused argument r=jandem → Bug 1799025: Part 4: Remove unused argument r=jandem!
Attachment #9309254 - Attachment description: Bug 1799025: Part 5: Store indices in NativeIterator r=jandem → Bug 1799025: Part 5: Store indices in NativeIterator r=jandem!
Attachment #9309255 - Attachment description: Bug 1799025: Part 6: Get iterator before attaching IC r=jandem → Bug 1799025: Part 6: Get iterator before attaching IC r=jandem!
Attachment #9309256 - Attachment description: Bug 1799025: Part 7: Add MBasicBlock::wrapInstructionInFastPath r=jandem → Bug 1799025: Part 7: Add MBasicBlock::wrapInstructionInFastPath r=jandem!
Attachment #9309257 - Attachment description: Bug 1799025: Part 8: Use iterator indices for HasOwnProp r=jandem → Bug 1799025: Part 8: Use iterator indices for HasOwnProp r=jandem!
Attachment #9309258 - Attachment description: Bug 1799025: Part 9: Use iterator indices for GetProp r=jandem → Bug 1799025: Part 9: Use iterator indices for GetProp r=jandem!
Attachment #9309259 - Attachment description: Bug 1799025: Part 10: Add JitOption r=jandem → Bug 1799025: Part 10: Add JitOption r=jandem!
Attachment #9309260 - Attachment description: Bug 1799025: Part 11: Add tests r=jandem → Bug 1799025: Part 11: Add tests r=jandem!

Without this change I saw intermittent failures with '--ion-check-range-analysis', because we inserted an extra use of an IteratorHasIndices and triggered an assertion during lowering.

Depends on D167107

Attachment #9312756 - Attachment description: Bug 1799025: Part 13: Fix intermittent failure r=jandem → Bug 1799025: Part 13: Fix intermittent failure r=jandem!
Pushed by iireland@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/80736413ee26 Part 1: Add PropertyEnumerator r=jandem https://hg.mozilla.org/integration/autoland/rev/ce70716df32d Part 2: Add PropertyIndex r=jandem https://hg.mozilla.org/integration/autoland/rev/0d77465d31a9 Part 3: Support indices in PropertyEnumerator r=jandem https://hg.mozilla.org/integration/autoland/rev/284604572cda Part 4: Remove unused argument r=jandem https://hg.mozilla.org/integration/autoland/rev/5a34d12bfb67 Part 5: Store indices in NativeIterator r=jandem https://hg.mozilla.org/integration/autoland/rev/3160865a976a Part 6: Get iterator before attaching IC r=jandem https://hg.mozilla.org/integration/autoland/rev/d967af0cdc3e Part 7: Add MBasicBlock::wrapInstructionInFastPath r=jandem https://hg.mozilla.org/integration/autoland/rev/75be34e55f82 Part 8: Use iterator indices for HasOwnProp r=jandem https://hg.mozilla.org/integration/autoland/rev/ffc5325948a6 Part 9: Use iterator indices for GetProp r=jandem https://hg.mozilla.org/integration/autoland/rev/9b54d7cd721e Part 10: Add JitOption r=jandem https://hg.mozilla.org/integration/autoland/rev/4c6354f6ebf2 Part 11: Add tests r=jandem https://hg.mozilla.org/integration/autoland/rev/b4325f765972 Part 12: Use iterator indices for GetPropCache r=jandem https://hg.mozilla.org/integration/autoland/rev/f93a220d341c Part 13: Fix intermittent failure r=nbp
Whiteboard: [sp3]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: