Commit 51fb18ba authored by Jan de Mooij's avatar Jan de Mooij
Browse files

Bug 1733075 part 3 - Optimize property enumeration in EnumerateNativeProperties. r=iain

If we only care about enumerable properties and the object doesn't have any, we don't need
to iterate its properties.

We can't use this optimization in the CheckForDuplicates case, but fortunately the
optimization in the previous patch made that a lot less common.

Depends on D127055

Differential Revision: https://phabricator.services.mozilla.com/D127056
parent 66114545
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -189,8 +189,21 @@ static bool EnumerateNativeProperties(JSContext* cx, HandleNativeObject pobj,
                                      unsigned flags,
                                      MutableHandle<PropertyKeySet> visited,
                                      MutableHandleIdVector props) {
  // We don't need to iterate over the shape's properties if we're only
  // interested in enumerable properties and the object is known to have no
  // enumerable properties.
  //
  // Don't optimize if CheckForDuplicates is true, because non-enumerable
  // properties still have to participate in duplicate-property checking.
  const bool iterShapeProperties = CheckForDuplicates ||
                                   (flags & JSITER_HIDDEN) ||
                                   pobj->hasEnumerableProperty();

  bool enumerateSymbols;
  if (flags & JSITER_SYMBOLSONLY) {
    if (!iterShapeProperties) {
      return true;
    }
    enumerateSymbols = true;
  } else {
    // Collect any dense elements from this object.
@@ -235,6 +248,12 @@ static bool EnumerateNativeProperties(JSContext* cx, HandleNativeObject pobj,
      }
    }

    // The code below enumerates shape properties (including sparse elements) so
    // if we can ignore those we're done.
    if (!iterShapeProperties) {
      return true;
    }

    // Collect any sparse elements from this object.
    bool isIndexed = pobj->isIndexed();
    if (isIndexed) {
@@ -299,6 +318,8 @@ static bool EnumerateNativeProperties(JSContext* cx, HandleNativeObject pobj,
  }

  if (enumerateSymbols) {
    MOZ_ASSERT(iterShapeProperties);

    // Do a second pass to collect symbols. ES6 draft rev 25 (2014 May 22)
    // 9.1.12 requires that all symbols appear after all strings in the
    // result.