# Global Metrics

path: .metrics.nom.total
old: 5.0
new: 21.0
path: .metrics.nom.functions
old: 5.0
new: 21.0
path: .metrics.mi.mi_visual_studio
old: 10.002671904716156
new: 15.00511035766807
path: .metrics.mi.mi_sei
old: -28.115537599016463
new: -11.600145165134824
path: .metrics.mi.mi_original
old: 17.10456895706463
new: 25.6587387116124
path: .metrics.loc.lloc
old: 135.0
new: 38.0
path: .metrics.loc.ploc
old: 294.0
new: 175.0
path: .metrics.loc.sloc
old: 333.0
new: 243.0
path: .metrics.loc.blank
old: 19.0
new: 47.0
path: .metrics.loc.cloc
old: 20.0
new: 21.0
path: .metrics.nargs.sum
old: 33.0
new: 11.0
path: .metrics.nargs.average
old: 6.6
new: 0.5238095238095238
path: .metrics.halstead.N2
old: 795.0
new: 300.0
path: .metrics.halstead.difficulty
old: 85.17857142857143
new: 32.55813953488372
path: .metrics.halstead.bugs
old: 3.88051782731259
new: 1.0683556459592511
path: .metrics.halstead.vocabulary
old: 204.0
new: 157.0
path: .metrics.halstead.estimated_program_length
old: 1428.026627078755
new: 1039.054253767213
path: .metrics.halstead.n2
old: 168.0
new: 129.0
path: .metrics.halstead.N1
old: 1127.0
new: 464.0
path: .metrics.halstead.time
old: 69782.07856118468
new: 10080.525003894683
path: .metrics.halstead.volume
old: 14746.401507269216
new: 5573.090252153203
path: .metrics.halstead.level
old: 0.011740041928721174
new: 0.030714285714285715
path: .metrics.halstead.length
old: 1922.0
new: 764.0
path: .metrics.halstead.n1
old: 36.0
new: 28.0
path: .metrics.halstead.purity_ratio
old: 0.742989920436397
new: 1.3600186567633674
path: .metrics.halstead.effort
old: 1256077.4141013245
new: 181449.4500701043
path: .metrics.cyclomatic.average
old: 6.142857142857143
new: 1.6666666666666667
path: .metrics.cyclomatic.sum
old: 43.0
new: 50.0
path: .metrics.cognitive.sum
old: 66.0
new: 11.0
path: .metrics.cognitive.average
old: 13.2
new: 0.5238095238095238
path: .metrics.nexits.average
old: 1.2
new: 0.42857142857142855
path: .metrics.nexits.sum
old: 6.0
new: 9.0

# Spaces Data

## Minimal test - lines (33, 33)

path: .spaces[1].metrics.cognitive.sum
old: 1.0
new: 0.0
path: .spaces[1].metrics.cognitive.average
old: 1.0
new: null
path: .spaces[1].metrics.loc.lloc
old: 3.0
new: 0.0
path: .spaces[1].metrics.loc.sloc
old: 7.0
new: 1.0
path: .spaces[1].metrics.loc.ploc
old: 7.0
new: 1.0
path: .spaces[1].metrics.halstead.effort
old: 4426.720114986861
new: 0.0
path: .spaces[1].metrics.halstead.vocabulary
old: 31.0
new: 1.0
path: .spaces[1].metrics.halstead.estimated_program_length
old: 122.78983721006222
new: null
path: .spaces[1].metrics.halstead.length
old: 70.0
new: 1.0
path: .spaces[1].metrics.halstead.n1
old: 14.0
new: 0.0
path: .spaces[1].metrics.halstead.time
old: 245.9288952770478
new: 0.0
path: .spaces[1].metrics.halstead.n2
old: 17.0
new: 1.0
path: .spaces[1].metrics.halstead.bugs
old: 0.08986697460436896
new: 0.0
path: .spaces[1].metrics.halstead.purity_ratio
old: 1.7541405315723173
new: null
path: .spaces[1].metrics.halstead.difficulty
old: 12.764705882352942
new: 0.0
path: .spaces[1].metrics.halstead.level
old: 0.07834101382488479
new: null
path: .spaces[1].metrics.halstead.N2
old: 31.0
new: 1.0
path: .spaces[1].metrics.halstead.N1
old: 39.0
new: 0.0
path: .spaces[1].metrics.halstead.volume
old: 346.7937417270812
new: 0.0
path: .spaces[1].metrics.nargs.average
old: 6.0
new: null
path: .spaces[1].metrics.nargs.sum
old: 6.0
new: 0.0
path: .spaces[1].metrics.cyclomatic.sum
old: 2.0
new: 1.0
path: .spaces[1].metrics.cyclomatic.average
old: 2.0
new: 1.0
path: .spaces[1].metrics.nom.total
old: 1.0
new: 0.0
path: .spaces[1].metrics.nom.functions
old: 1.0
new: 0.0
path: .spaces[1].metrics.mi.mi_visual_studio
old: 63.5104435968653
new: null
path: .spaces[1].metrics.mi.mi_original
old: 108.60285855063968
new: null
path: .spaces[1].metrics.mi.mi_sei
old: 81.18359318416957
new: null
path: .spaces[1].metrics.nexits.sum
old: 1.0
new: 0.0
path: .spaces[1].metrics.nexits.average
old: 1.0
new: null

### Code

    class nsWindow;

## Minimal test - lines (35, 241)

path: .spaces[2].metrics.cognitive.sum
old: 51.0
new: 11.0
path: .spaces[2].metrics.cognitive.average
old: 51.0
new: 0.5238095238095238
path: .spaces[2].metrics.cyclomatic.average
old: 22.0
new: 1.7407407407407407
path: .spaces[2].metrics.cyclomatic.sum
old: 22.0
new: 47.0
path: .spaces[2].metrics.nexits.sum
old: 2.0
new: 9.0
path: .spaces[2].metrics.nexits.average
old: 2.0
new: 0.42857142857142855
path: .spaces[2].metrics.nargs.average
old: 9.0
new: 0.5238095238095238
path: .spaces[2].metrics.nargs.sum
old: 9.0
new: 11.0
path: .spaces[2].metrics.loc.sloc
old: 146.0
new: 207.0
path: .spaces[2].metrics.loc.ploc
old: 138.0
new: 151.0
path: .spaces[2].metrics.loc.cloc
old: 8.0
new: 14.0
path: .spaces[2].metrics.loc.blank
old: 0.0
new: 42.0
path: .spaces[2].metrics.loc.lloc
old: 62.0
new: 38.0
path: .spaces[2].metrics.halstead.length
old: 968.0
new: 736.0
path: .spaces[2].metrics.halstead.bugs
old: 1.7087725802901657
new: 1.084963525100163
path: .spaces[2].metrics.halstead.effort
old: 367035.67767687613
new: 185696.87240134587
path: .spaces[2].metrics.halstead.vocabulary
old: 122.0
new: 139.0
path: .spaces[2].metrics.halstead.N1
old: 564.0
new: 455.0
path: .spaces[2].metrics.halstead.time
old: 20390.870982048673
new: 10316.492911185882
path: .spaces[2].metrics.halstead.n2
old: 96.0
new: 111.0
path: .spaces[2].metrics.halstead.N2
old: 404.0
new: 281.0
path: .spaces[2].metrics.halstead.volume
old: 6708.953742760874
new: 5239.540629524502
path: .spaces[2].metrics.halstead.estimated_program_length
old: 754.3678327408994
new: 888.7860989824748
path: .spaces[2].metrics.halstead.purity_ratio
old: 0.7793056123356399
new: 1.2075898084001015
path: .spaces[2].metrics.halstead.level
old: 0.018278750952018277
new: 0.028215556685307577
path: .spaces[2].metrics.halstead.n1
old: 26.0
new: 28.0
path: .spaces[2].metrics.halstead.difficulty
old: 54.708333333333336
new: 35.44144144144144
path: .spaces[2].metrics.mi.mi_sei
old: 1.1002365271512673
new: -9.0878140620607
path: .spaces[2].metrics.mi.mi_visual_studio
old: 23.03353310298341
new: 17.115328766594324
path: .spaces[2].metrics.mi.mi_original
old: 39.38734160610163
new: 29.267212190876293
path: .spaces[2].metrics.nom.functions
old: 1.0
new: 21.0
path: .spaces[2].metrics.nom.total
old: 1.0
new: 21.0

### Code

    class nsAppShell : public nsBaseAppShell {
     public:
      struct Event : mozilla::LinkedListElement {
        static uint64_t GetTime() {
          timespec time;
          if (clock_gettime(CLOCK_MONOTONIC, &time)) {
            return 0ull;
          }
          return uint64_t(time.tv_sec) * 1000000000ull + time.tv_nsec;
        }

        uint64_t mPostTime{0};

        bool HasSameTypeAs(const Event* other) const {
          // Compare vtable addresses to determine same type.
          return *reinterpret_cast(this) ==
                 *reinterpret_cast(other);
        }

        virtual ~Event() {}
        virtual void Run() = 0;

        virtual void PostTo(mozilla::LinkedList& queue) {
          queue.insertBack(this);
        }

        virtual bool IsUIEvent() const { return false; }
      };

      template
      class LambdaEvent : public Event {
       protected:
        T lambda;

       public:
        explicit LambdaEvent(T&& l) : lambda(std::move(l)) {}
        void Run() override { lambda(); }
      };

      class ProxyEvent : public Event {
       protected:
        mozilla::UniquePtr baseEvent;

       public:
        explicit ProxyEvent(mozilla::UniquePtr&& event)
            : baseEvent(std::move(event)) {}

        void PostTo(mozilla::LinkedList& queue) override {
          baseEvent->PostTo(queue);
        }

        void Run() override { baseEvent->Run(); }
      };

      static nsAppShell* Get() {
        MOZ_ASSERT(NS_IsMainThread());
        return sAppShell;
      }

      nsAppShell();

      NS_DECL_ISUPPORTS_INHERITED
      NS_DECL_NSIOBSERVER

      nsresult Init();

      void NotifyNativeEvent();
      bool ProcessNextNativeEvent(bool mayWait) override;

      // Post a subclass of Event.
      // e.g. PostEvent(mozilla::MakeUnique());
      template
      static void PostEvent(mozilla::UniquePtr&& event) {
        mozilla::MutexAutoLock lock(*sAppShellLock);
        if (!sAppShell) {
          return;
        }
        sAppShell->mEventQueue.Post(std::move(event));
      }

      // Post a event that will call a lambda
      // e.g. PostEvent([=] { /* do something */ });
      template
      static void PostEvent(T&& lambda) {
        mozilla::MutexAutoLock lock(*sAppShellLock);
        if (!sAppShell) {
          return;
        }
        sAppShell->mEventQueue.Post(
            mozilla::MakeUnique>(std::move(lambda)));
      }

      // Post a event and wait for it to finish running on the Gecko thread.
      static bool SyncRunEvent(
          Event&& event,
          mozilla::UniquePtr (*eventFactory)(mozilla::UniquePtr&&) =
              nullptr,
          const mozilla::TimeDuration timeout = mozilla::TimeDuration::Forever());

      template
      static std::enable_if_t::value, void> SyncRunEvent(
          T&& lambda) {
        SyncRunEvent(LambdaEvent(std::forward(lambda)));
      }

      static already_AddRefed ResolveURI(const nsCString& aUriStr);

     protected:
      static nsAppShell* sAppShell;
      static mozilla::StaticAutoPtr sAppShellLock;

      static void RecordLatencies();

      virtual ~nsAppShell();

      nsresult AddObserver(const nsAString& aObserverKey, nsIObserver* aObserver);

      class NativeCallbackEvent : public Event {
        // Capturing the nsAppShell instance is safe because if the app
        // shell is destroyed, this lambda will not be called either.
        nsAppShell* const appShell;

       public:
        explicit NativeCallbackEvent(nsAppShell* as) : appShell(as) {}
        void Run() override { appShell->NativeEventCallback(); }
      };

      void ScheduleNativeEventCallback() override {
        mEventQueue.Post(mozilla::MakeUnique(this));
      }

      class Queue {
       private:
        mozilla::Monitor mMonitor;
        mozilla::LinkedList mQueue;

       public:
        enum { LATENCY_UI, LATENCY_OTHER, LATENCY_COUNT };
        static uint32_t sLatencyCount[LATENCY_COUNT];
        static uint64_t sLatencyTime[LATENCY_COUNT];

        Queue() : mMonitor("nsAppShell.Queue") {}

        void Signal() {
          mozilla::MonitorAutoLock lock(mMonitor);
          lock.NotifyAll();
        }

        void Post(mozilla::UniquePtr&& event) {
          MOZ_ASSERT(event && !event->isInList());

          mozilla::MonitorAutoLock lock(mMonitor);
          event->PostTo(mQueue);
          if (event->isInList()) {
            event->mPostTime = Event::GetTime();
            // Ownership of event object transfers to the queue.
            mozilla::Unused << event.release();
          }
          lock.NotifyAll();
        }

        mozilla::UniquePtr Pop(bool mayWait) {
    #ifdef EARLY_BETA_OR_EARLIER
          bool isQueueEmpty = false;
          if (mayWait) {
            mozilla::MonitorAutoLock lock(mMonitor);
            isQueueEmpty = mQueue.isEmpty();
          }
          if (isQueueEmpty) {
            // Record latencies when we're about to be idle.
            // Note: We can't call this while holding the lock because
            // nsAppShell::RecordLatencies may try to dispatch an event to the main
            // thread which tries to acquire the lock again.
            nsAppShell::RecordLatencies();
          }
    #endif
          mozilla::MonitorAutoLock lock(mMonitor);

          if (mayWait && mQueue.isEmpty()) {
            lock.Wait();
          }

          // Ownership of event object transfers to the return value.
          mozilla::UniquePtr event(mQueue.popFirst());
          if (!event || !event->mPostTime) {
            return event;
          }

    #ifdef EARLY_BETA_OR_EARLIER
          const size_t latencyType =
              event->IsUIEvent() ? LATENCY_UI : LATENCY_OTHER;
          const uint64_t latency = Event::GetTime() - event->mPostTime;

          sLatencyCount[latencyType]++;
          sLatencyTime[latencyType] += latency;
    #endif
          return event;
        }

      } mEventQueue;

     private:
      mozilla::CondVar mSyncRunFinished;
      bool mSyncRunQuit;

      nsInterfaceHashtable mObserversHash;
    };

## Minimal test - lines (28, 31)

path: .spaces[0].metrics.cognitive.sum
old: 6.0
new: 0.0
path: .spaces[0].metrics.cognitive.average
old: 6.0
new: null
path: .spaces[0].metrics.loc.blank
old: 5.0
new: 0.0
path: .spaces[0].metrics.loc.sloc
old: 74.0
new: 4.0
path: .spaces[0].metrics.loc.ploc
old: 69.0
new: 4.0
path: .spaces[0].metrics.loc.lloc
old: 28.0
new: 0.0
path: .spaces[0].metrics.cyclomatic.sum
old: 6.0
new: 1.0
path: .spaces[0].metrics.cyclomatic.average
old: 6.0
new: 1.0
path: .spaces[0].metrics.nargs.average
old: 5.0
new: null
path: .spaces[0].metrics.nargs.sum
old: 5.0
new: 0.0
path: .spaces[0].metrics.mi.mi_original
old: 61.37707947491555
new: 130.97092666425706
path: .spaces[0].metrics.mi.mi_sei
old: 13.45847534712246
new: 113.35207426654748
path: .spaces[0].metrics.mi.mi_visual_studio
old: 35.893028932699146
new: 76.59118518377606
path: .spaces[0].metrics.halstead.volume
old: 1647.687518354528
new: 28.07354922057604
path: .spaces[0].metrics.halstead.length
old: 285.0
new: 10.0
path: .spaces[0].metrics.halstead.time
old: 3138.452415913386
new: 3.1192832467306713
path: .spaces[0].metrics.halstead.bugs
old: 0.49076183421622993
new: 0.0048876146417121315
path: .spaces[0].metrics.halstead.vocabulary
old: 55.0
new: 7.0
path: .spaces[0].metrics.halstead.effort
old: 56492.14348644096
new: 56.14709844115208
path: .spaces[0].metrics.halstead.purity_ratio
old: 0.9332051490906004
new: 1.2754887502163468
path: .spaces[0].metrics.halstead.N2
old: 120.0
new: 3.0
path: .spaces[0].metrics.halstead.difficulty
old: 34.285714285714285
new: 2.0
path: .spaces[0].metrics.halstead.level
old: 0.029166666666666667
new: 0.5
path: .spaces[0].metrics.halstead.n1
old: 20.0
new: 4.0
path: .spaces[0].metrics.halstead.n2
old: 35.0
new: 3.0
path: .spaces[0].metrics.halstead.N1
old: 165.0
new: 7.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 265.9634674908211
new: 12.754887502163468
path: .spaces[0].metrics.nom.total
old: 1.0
new: 0.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 0.0
path: .spaces[0].metrics.nexits.sum
old: 2.0
new: 0.0
path: .spaces[0].metrics.nexits.average
old: 2.0
new: null

### Code

    namespace mozilla {
    bool ProcessNextEvent();
    void NotifyEvent();
    }  // namespace mozilla