# Global Metrics

path: .metrics.loc.ploc
old: 26.0
new: 158.0
path: .metrics.loc.sloc
old: 46.0
new: 213.0
path: .metrics.loc.cloc
old: 15.0
new: 20.0
path: .metrics.loc.blank
old: 5.0
new: 35.0
path: .metrics.loc.lloc
old: 0.0
new: 88.0
path: .metrics.nexits.sum
old: 0.0
new: 17.0
path: .metrics.nexits.average
old: 0.0
new: 1.2142857142857142
path: .metrics.cognitive.average
old: 0.0
new: 1.0714285714285714
path: .metrics.cognitive.sum
old: 0.0
new: 15.0
path: .metrics.cyclomatic.sum
old: 6.0
new: 45.0
path: .metrics.cyclomatic.average
old: 1.0
new: 2.8125
path: .metrics.halstead.vocabulary
old: 30.0
new: 99.0
path: .metrics.halstead.time
old: 204.24932104220457
new: 12959.596669461229
path: .metrics.halstead.bugs
old: 0.07940249754500986
new: 1.2631494550909148
path: .metrics.halstead.N2
old: 37.0
new: 298.0
path: .metrics.halstead.volume
old: 397.45813824429
new: 4892.465185618752
path: .metrics.halstead.n1
old: 10.0
new: 24.0
path: .metrics.halstead.n2
old: 20.0
new: 75.0
path: .metrics.halstead.level
old: 0.10810810810810811
new: 0.02097315436241611
path: .metrics.halstead.length
old: 81.0
new: 738.0
path: .metrics.halstead.N1
old: 44.0
new: 440.0
path: .metrics.halstead.difficulty
old: 9.25
new: 47.68
path: .metrics.halstead.effort
old: 3676.4877787596824
new: 233272.7400503021
path: .metrics.halstead.estimated_program_length
old: 119.65784284662088
new: 577.2005018044988
path: .metrics.halstead.purity_ratio
old: 1.4772573190940848
new: 0.7821145010901068
path: .metrics.mi.mi_original
old: 76.4735433739073
new: 29.62071868194424
path: .metrics.mi.mi_visual_studio
old: 44.72137039409784
new: 17.322057708739322
path: .metrics.mi.mi_sei
old: 73.92278839191638
new: -5.531149276451121
path: .metrics.nom.total
old: 1.0
new: 14.0
path: .metrics.nom.functions
old: 1.0
new: 14.0
path: .metrics.nargs.sum
old: 3.0
new: 26.0
path: .metrics.nargs.average
old: 3.0
new: 1.8571428571428572

# Spaces Data

## Minimal test - lines (15, 16)

path: .spaces[0].spaces[0].metrics.cyclomatic.sum
old: 4.0
new: 1.0
path: .spaces[0].spaces[0].metrics.loc.cloc
old: 3.0
new: 0.0
path: .spaces[0].spaces[0].metrics.loc.sloc
old: 24.0
new: 2.0
path: .spaces[0].spaces[0].metrics.loc.blank
old: 3.0
new: 0.0
path: .spaces[0].spaces[0].metrics.loc.ploc
old: 18.0
new: 2.0
path: .spaces[0].spaces[0].metrics.nargs.average
old: 3.0
new: 2.0
path: .spaces[0].spaces[0].metrics.nargs.sum
old: 3.0
new: 2.0
path: .spaces[0].spaces[0].metrics.mi.mi_original
old: 87.96801478716873
new: 137.707133926154
path: .spaces[0].spaces[0].metrics.mi.mi_sei
old: 77.6546611115364
new: 123.0703670776864
path: .spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 51.44328350126826
new: 80.53048767611345
path: .spaces[0].spaces[0].metrics.halstead.time
old: 194.85715450042449
new: 14.273124627115642
path: .spaces[0].spaces[0].metrics.halstead.effort
old: 3507.4287810076407
new: 256.91624328808155
path: .spaces[0].spaces[0].metrics.halstead.n2
old: 17.0
new: 7.0
path: .spaces[0].spaces[0].metrics.halstead.bugs
old: 0.07694929635023873
new: 0.013471205273543386
path: .spaces[0].spaces[0].metrics.halstead.difficulty
old: 9.705882352941176
new: 3.857142857142857
path: .spaces[0].spaces[0].metrics.halstead.level
old: 0.10303030303030304
new: 0.25925925925925924
path: .spaces[0].spaces[0].metrics.halstead.length
old: 76.0
new: 18.0
path: .spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 102.7061492501294
new: 35.161259458730164
path: .spaces[0].spaces[0].metrics.halstead.n1
old: 10.0
new: 6.0
path: .spaces[0].spaces[0].metrics.halstead.volume
old: 361.3714501644236
new: 66.60791492653966
path: .spaces[0].spaces[0].metrics.halstead.N1
old: 43.0
new: 9.0
path: .spaces[0].spaces[0].metrics.halstead.N2
old: 33.0
new: 9.0
path: .spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 1.3513967006595973
new: 1.9534033032627869
path: .spaces[0].spaces[0].metrics.halstead.vocabulary
old: 27.0
new: 13.0

### Code

    PreloadHashKey::PreloadHashKey(const nsIURI* aKey, ResourceType aAs)
        : nsURIHashKey(aKey), mAs(aAs) {}

## Minimal test - lines (13, 213)

path: .spaces[0].metrics.nom.total
old: 1.0
new: 14.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 14.0
path: .spaces[0].metrics.halstead.N1
old: 44.0
new: 440.0
path: .spaces[0].metrics.halstead.n2
old: 18.0
new: 69.0
path: .spaces[0].metrics.halstead.level
old: 0.10588235294117648
new: 0.01969178082191781
path: .spaces[0].metrics.halstead.volume
old: 374.9736839204931
new: 4786.664249731079
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.3881786022414777
new: 0.726130174228144
path: .spaces[0].metrics.halstead.effort
old: 3541.418125915768
new: 243079.29755156089
path: .spaces[0].metrics.halstead.N2
old: 34.0
new: 292.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 108.27793097483524
new: 531.5272875350014
path: .spaces[0].metrics.halstead.bugs
old: 0.07744562384513211
new: 1.2983069852282578
path: .spaces[0].metrics.halstead.vocabulary
old: 28.0
new: 93.0
path: .spaces[0].metrics.halstead.difficulty
old: 9.444444444444445
new: 50.78260869565217
path: .spaces[0].metrics.halstead.length
old: 78.0
new: 732.0
path: .spaces[0].metrics.halstead.time
old: 196.7454514397649
new: 13504.405419531158
path: .spaces[0].metrics.halstead.n1
old: 10.0
new: 24.0
path: .spaces[0].metrics.nargs.average
old: 3.0
new: 1.8571428571428572
path: .spaces[0].metrics.nargs.sum
old: 3.0
new: 26.0
path: .spaces[0].metrics.mi.mi_visual_studio
old: 50.43812027959834
new: 18.07239616060036
path: .spaces[0].metrics.mi.mi_original
old: 86.24918567811315
new: 30.90379743462661
path: .spaces[0].metrics.mi.mi_sei
old: 77.78624323687308
new: -6.101674659075531
path: .spaces[0].metrics.loc.cloc
old: 4.0
new: 15.0
path: .spaces[0].metrics.loc.lloc
old: 0.0
new: 88.0
path: .spaces[0].metrics.loc.blank
old: 2.0
new: 34.0
path: .spaces[0].metrics.loc.ploc
old: 20.0
new: 152.0
path: .spaces[0].metrics.loc.sloc
old: 26.0
new: 201.0
path: .spaces[0].metrics.cognitive.sum
old: 0.0
new: 15.0
path: .spaces[0].metrics.cognitive.average
old: 0.0
new: 1.0714285714285714
path: .spaces[0].metrics.cyclomatic.sum
old: 5.0
new: 44.0
path: .spaces[0].metrics.cyclomatic.average
old: 1.0
new: 2.933333333333333
path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 17.0
path: .spaces[0].metrics.nexits.average
old: 0.0
new: 1.2142857142857142

### Code

    namespace mozilla {

    PreloadHashKey::PreloadHashKey(const nsIURI* aKey, ResourceType aAs)
        : nsURIHashKey(aKey), mAs(aAs) {}

    PreloadHashKey::PreloadHashKey(const PreloadHashKey* aKey)
        : nsURIHashKey(aKey->mKey) {
      *this = *aKey;
    }

    PreloadHashKey::PreloadHashKey(PreloadHashKey&& aToMove)
        : nsURIHashKey(std::move(aToMove)) {
      mAs = std::move(aToMove.mAs);
      mCORSMode = std::move(aToMove.mCORSMode);
      mPrincipal = std::move(aToMove.mPrincipal);

      switch (mAs) {
        case ResourceType::SCRIPT:
          mScript = std::move(aToMove.mScript);
          break;
        case ResourceType::STYLE:
          mStyle = std::move(aToMove.mStyle);
          break;
        case ResourceType::IMAGE:
          break;
        case ResourceType::FONT:
          break;
        case ResourceType::FETCH:
          break;
        case ResourceType::NONE:
          break;
      }
    }

    PreloadHashKey& PreloadHashKey::operator=(const PreloadHashKey& aOther) {
      MOZ_ASSERT(mAs == ResourceType::NONE || aOther.mAs == ResourceType::NONE,
                 "Assigning more than once, only reset is allowed");

      nsURIHashKey::operator=(aOther);

      mAs = aOther.mAs;
      mCORSMode = aOther.mCORSMode;
      mPrincipal = aOther.mPrincipal;

      switch (mAs) {
        case ResourceType::SCRIPT:
          mScript = aOther.mScript;
          break;
        case ResourceType::STYLE:
          mStyle = aOther.mStyle;
          break;
        case ResourceType::IMAGE:
          break;
        case ResourceType::FONT:
          break;
        case ResourceType::FETCH:
          break;
        case ResourceType::NONE:
          break;
      }

      return *this;
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsScript(nsIURI* aURI, CORSMode aCORSMode,
                                                  dom::ScriptKind aScriptKind) {
      PreloadHashKey key(aURI, ResourceType::SCRIPT);
      key.mCORSMode = aCORSMode;

      key.mScript.mScriptKind = aScriptKind;

      return key;
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsScript(nsIURI* aURI,
                                                  const nsAString& aCrossOrigin,
                                                  const nsAString& aType) {
      dom::ScriptKind scriptKind = dom::ScriptKind::eClassic;
      if (aType.LowerCaseEqualsASCII("module")) {
        scriptKind = dom::ScriptKind::eModule;
      }
      CORSMode crossOrigin = dom::Element::StringToCORSMode(aCrossOrigin);
      return CreateAsScript(aURI, crossOrigin, scriptKind);
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsStyle(
        nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
        css::SheetParsingMode aParsingMode) {
      PreloadHashKey key(aURI, ResourceType::STYLE);
      key.mCORSMode = aCORSMode;
      key.mPrincipal = aPrincipal;

      key.mStyle.mParsingMode = aParsingMode;

      return key;
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsStyle(
        css::SheetLoadData& aSheetLoadData) {
      return CreateAsStyle(aSheetLoadData.mURI, aSheetLoadData.mTriggeringPrincipal,
                           aSheetLoadData.mSheet->GetCORSMode(),
                           aSheetLoadData.mSheet->ParsingMode());
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsImage(nsIURI* aURI,
                                                 nsIPrincipal* aPrincipal,
                                                 CORSMode aCORSMode) {
      PreloadHashKey key(aURI, ResourceType::IMAGE);
      key.mCORSMode = aCORSMode;
      key.mPrincipal = aPrincipal;

      return key;
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI, CORSMode aCORSMode) {
      PreloadHashKey key(aURI, ResourceType::FETCH);
      key.mCORSMode = aCORSMode;

      return key;
    }

    // static
    PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI,
                                                 const nsAString& aCrossOrigin) {
      return CreateAsFetch(aURI, dom::Element::StringToCORSMode(aCrossOrigin));
    }

    PreloadHashKey PreloadHashKey::CreateAsFont(nsIURI* aURI, CORSMode aCORSMode) {
      PreloadHashKey key(aURI, ResourceType::FONT);
      key.mCORSMode = aCORSMode;

      return key;
    }

    bool PreloadHashKey::KeyEquals(KeyTypePointer aOther) const {
      if (mAs != aOther->mAs || mCORSMode != aOther->mCORSMode) {
        return false;
      }

      if (!mPrincipal != !aOther->mPrincipal) {
        // One or the other has a principal, but not both... not equal
        return false;
      }

      if (mPrincipal && !mPrincipal->Equals(aOther->mPrincipal)) {
        return false;
      }

      if (!nsURIHashKey::KeyEquals(
              static_cast(aOther)->GetKey())) {
        return false;
      }

      switch (mAs) {
        case ResourceType::SCRIPT:
          if (mScript.mScriptKind != aOther->mScript.mScriptKind) {
            return false;
          }
          break;
        case ResourceType::STYLE: {
          if (mStyle.mParsingMode != aOther->mStyle.mParsingMode) {
            return false;
          }
          break;
        }
        case ResourceType::IMAGE:
          // No more checks needed.  The image cache key consists of the document
          // (which we are scoped into), origin attributes (which we compare as part
          // of the principal check) and the URL.  imgLoader::ValidateEntry compares
          // CORS, referrer info and principal, which we do by default.
          break;
        case ResourceType::FONT:
          break;
        case ResourceType::FETCH:
          // No more checks needed.
          break;
        case ResourceType::NONE:
          break;
      }

      return true;
    }

    // static
    PLDHashNumber PreloadHashKey::HashKey(KeyTypePointer aKey) {
      PLDHashNumber hash = nsURIHashKey::HashKey(aKey->mKey);

      // Enough to use the common attributes
      hash = AddToHash(hash, static_cast(aKey->mAs));
      hash = AddToHash(hash, static_cast(aKey->mCORSMode));

      return hash;
    }

    }  // namespace mozilla