pw_persistent_ram: add Persistent size report

Change-Id: I9c4c029f911f251ee9a93f10d571f3ae59c88628
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/38581
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Reviewed-by: David Rogers <davidrogers@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_persistent_ram/BUILD.gn b/pw_persistent_ram/BUILD.gn
index 6c7bdcf..b58ccd5 100644
--- a/pw_persistent_ram/BUILD.gn
+++ b/pw_persistent_ram/BUILD.gn
@@ -14,6 +14,7 @@
 
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_bloat/bloat.gni")
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_unit_test/test.gni")
@@ -41,8 +42,28 @@
   sources = [ "persistent_test.cc" ]
 }
 
-# TODO(ewout): add size reports in a follow up CL.
-
 pw_doc_group("docs") {
   sources = [ "docs.rst" ]
+  report_deps = [ ":persistent_size" ]
+}
+
+pw_size_report("persistent_size") {
+  title = "pw::persistent_ram::Persistent"
+
+  # To see all the symbols, uncomment the following:
+  # Note: The size report RST table won't be generated when full_report = true.
+  # full_report = true
+
+  binaries = [
+    {
+      target = "size_report:persistent"
+      base = "size_report:persistent_base"
+      label = "Persistent including pw_checksum's CRC16"
+    },
+    {
+      target = "size_report:persistent"
+      base = "size_report:persistent_base_with_crc16"
+      label = "Persistent without pw_checksum's CRC16"
+    },
+  ]
 }
diff --git a/pw_persistent_ram/docs.rst b/pw_persistent_ram/docs.rst
index e0d94c6..e307b99 100644
--- a/pw_persistent_ram/docs.rst
+++ b/pw_persistent_ram/docs.rst
@@ -158,6 +158,14 @@
       // ... rest of main
     }
 
+Size Report
+-----------
+The following size report showcases the overhead for using Persistent. Note that
+this is templating the Persistent only on a ``uint32_t``, ergo the cost without
+pw_checksum's CRC16 is the approximate cost per type.
+
+.. include:: persistent_size
+
 Compatibility
 -------------
 * C++17
diff --git a/pw_persistent_ram/size_report/BUILD b/pw_persistent_ram/size_report/BUILD
new file mode 100644
index 0000000..11f4091
--- /dev/null
+++ b/pw_persistent_ram/size_report/BUILD
@@ -0,0 +1,35 @@
+# Copyright 2021 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+load(
+    "//pw_build:pigweed.bzl",
+    "pw_cc_binary",
+)
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache License 2.0
+
+pw_cc_binary(
+    name = "persistent",
+    srcs = [
+        "persistent.cc",
+        "persistent_base.cc",
+        "persistent_base_with_crc16.cc",
+    ],
+    deps = [
+        "//pw_bloat:bloat_this_binary",
+        "//pw_persistent_ram:persistent",
+    ],
+)
diff --git a/pw_persistent_ram/size_report/BUILD.gn b/pw_persistent_ram/size_report/BUILD.gn
new file mode 100644
index 0000000..afc1661
--- /dev/null
+++ b/pw_persistent_ram/size_report/BUILD.gn
@@ -0,0 +1,38 @@
+# Copyright 2021 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/target_types.gni")
+
+_persistent_deps = [
+  "$dir_pw_bloat:bloat_this_binary",
+  dir_pw_persistent_ram,
+]
+
+pw_executable("persistent") {
+  sources = [ "persistent.cc" ]
+  deps = _persistent_deps
+}
+
+pw_executable("persistent_base") {
+  sources = [ "persistent_base.cc" ]
+  deps = _persistent_deps
+}
+
+pw_executable("persistent_base_with_crc16") {
+  sources = [ "persistent_base_with_crc16.cc" ]
+  deps = _persistent_deps
+  deps += [ dir_pw_checksum ]
+}
diff --git a/pw_persistent_ram/size_report/persistent.cc b/pw_persistent_ram/size_report/persistent.cc
new file mode 100644
index 0000000..8ffeafa
--- /dev/null
+++ b/pw_persistent_ram/size_report/persistent.cc
@@ -0,0 +1,40 @@
+// Copyright 2021 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "pw_persistent_ram/persistent.h"
+
+#include "pw_bloat/bloat_this_binary.h"
+
+int main() {
+  pw::bloat::BloatThisBinary();
+
+  // Default constructor.
+  pw::persistent_ram::Persistent<uint32_t> persistent;
+
+  // Emplace to construct value in place.
+  persistent.emplace(42u);
+
+  // Assignment operator.
+  persistent = 13u;
+
+  // Reset.
+  persistent.reset();
+
+  // Has value and value accesstors.
+  if (persistent.has_value() && persistent.value() == 0u) {
+    return 1;
+  }
+
+  return 0;
+}
diff --git a/pw_persistent_ram/size_report/persistent_base.cc b/pw_persistent_ram/size_report/persistent_base.cc
new file mode 100644
index 0000000..9868cc0
--- /dev/null
+++ b/pw_persistent_ram/size_report/persistent_base.cc
@@ -0,0 +1,41 @@
+// Copyright 2021 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <cstdint>
+#include <utility>
+
+#include "pw_bloat/bloat_this_binary.h"
+
+int main() {
+  pw::bloat::BloatThisBinary();
+
+  // Default constructor.
+  volatile uint32_t value;
+
+  // Emplace to construct value in place.
+  value = std::move(42);
+
+  // Assignment operator.
+  value = 13u;
+
+  // Reset.
+  value = 0u;
+
+  // Has value and value accesstors.
+  if (value == 0u) {
+    return 1;
+  }
+
+  return 0;
+}
diff --git a/pw_persistent_ram/size_report/persistent_base_with_crc16.cc b/pw_persistent_ram/size_report/persistent_base_with_crc16.cc
new file mode 100644
index 0000000..a4e18e3
--- /dev/null
+++ b/pw_persistent_ram/size_report/persistent_base_with_crc16.cc
@@ -0,0 +1,46 @@
+// Copyright 2021 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <cstdint>
+#include <utility>
+
+#include "pw_bloat/bloat_this_binary.h"
+#include "pw_checksum/crc16_ccitt.h"
+
+int main() {
+  pw::bloat::BloatThisBinary();
+
+  // Default constructor.
+  volatile uint32_t value;
+
+  // Emplace to construct value in place.
+  value = std::move(42);
+
+  // Assignment operator.
+  value = 13u;
+
+  // Reset.
+  value = 0u;
+
+  // Has value and value accesstors.
+  if (value == 0u) {
+    return 1;
+  }
+
+  // Use CRC16.
+  value = pw::checksum::Crc16Ccitt::Calculate(
+      std::as_bytes(std::span(const_cast<uint32_t*>(&value), 1)));
+
+  return 0;
+}