1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LSWriteOptimizer.h"
10 #include "nsBaseHashtable.h"
13 namespace mozilla::dom
{
15 class LSWriteOptimizerBase::WriteInfoComparator
{
17 bool Equals(const WriteInfo
* a
, const WriteInfo
* b
) const {
19 return a
->SerialNumber() == b
->SerialNumber();
22 bool LessThan(const WriteInfo
* a
, const WriteInfo
* b
) const {
24 return a
->SerialNumber() < b
->SerialNumber();
28 void LSWriteOptimizerBase::DeleteItem(const nsAString
& aKey
, int64_t aDelta
) {
29 AssertIsOnOwningThread();
31 WriteInfo
* existingWriteInfo
;
32 if (mWriteInfos
.Get(aKey
, &existingWriteInfo
) &&
33 existingWriteInfo
->GetType() == WriteInfo::InsertItem
) {
34 mWriteInfos
.Remove(aKey
);
36 mWriteInfos
.Put(aKey
, MakeUnique
<DeleteItemInfo
>(NextSerialNumber(), aKey
));
39 mTotalDelta
+= aDelta
;
42 void LSWriteOptimizerBase::Truncate(int64_t aDelta
) {
43 AssertIsOnOwningThread();
48 mTruncateInfo
= MakeUnique
<TruncateInfo
>(NextSerialNumber());
51 mTotalDelta
+= aDelta
;
54 void LSWriteOptimizerBase::GetSortedWriteInfos(
55 nsTArray
<NotNull
<WriteInfo
*>>& aWriteInfos
) {
56 AssertIsOnOwningThread();
59 aWriteInfos
.InsertElementSorted(WrapNotNullUnchecked(mTruncateInfo
.get()),
60 WriteInfoComparator());
63 for (auto iter
= mWriteInfos
.ConstIter(); !iter
.Done(); iter
.Next()) {
64 WriteInfo
* writeInfo
= iter
.UserData();
66 aWriteInfos
.InsertElementSorted(WrapNotNull(writeInfo
),
67 WriteInfoComparator());
71 } // namespace mozilla::dom