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 mWriteInfos
.WithEntryHandle(aKey
, [&](auto&& entry
) {
32 if (entry
&& entry
.Data()->GetType() == WriteInfo::InsertItem
) {
36 MakeUnique
<DeleteItemInfo
>(NextSerialNumber(), aKey
));
40 mTotalDelta
+= aDelta
;
43 void LSWriteOptimizerBase::Truncate(int64_t aDelta
) {
44 AssertIsOnOwningThread();
49 mTruncateInfo
= MakeUnique
<TruncateInfo
>(NextSerialNumber());
52 mTotalDelta
+= aDelta
;
55 void LSWriteOptimizerBase::GetSortedWriteInfos(
56 nsTArray
<NotNull
<WriteInfo
*>>& aWriteInfos
) {
57 AssertIsOnOwningThread();
60 aWriteInfos
.InsertElementSorted(WrapNotNullUnchecked(mTruncateInfo
.get()),
61 WriteInfoComparator());
64 for (const auto& entry
: mWriteInfos
) {
65 WriteInfo
* writeInfo
= entry
.GetWeak();
67 aWriteInfos
.InsertElementSorted(WrapNotNull(writeInfo
),
68 WriteInfoComparator());
72 } // namespace mozilla::dom