Make WTF::SharedBuffer support appending data without copying

When loading a subresource, we use `WTF::SharedBuffer` to store resource
response data in `data_` of `blink::Resource`. Currently, there is no
way to append data to `WTF::SharedBuffer` without copying the data. This
is not a big issue for normal subresource loading where the main thread
reads data from the mojo data pipe using two-step read methods
(`BeginReadData()` and `EndReadData()`).

However, when `BackgroundResourceFetch` feature is enabled, the
background thread reads script resource response data from the mojo data
pipe, passes the data to the main thread as a `Vector<char>`, copies the
data, and appends it to `data_` of blink::Resource. This is not good
from a performance point of view. We need a way to append data without
copying.

Currently, `WTF::SharedBuffer` has two fields to hold data.
`Vector<char> buffer_` for the first part of data and
`Vector<Segment> segment_` for the following parts of data. `Segment` is
a memory buffer of fixed size (4KB).

This CL changes this `Segment` to a class with `size_t start_position_`
and `Vector<char> data_`, and makes WTF::SharedBuffer have
`Vector<Segment> segment_`. So we can append `Vector<char>` to
`WTF::SharedBuffer` without copying.

Performance testing shows that:
  [1] This change does not have a significant impact on performance when
      calling `Append()` with a span that causes data copying.
  [2] With this change, calling `Append()` with passing `Vector<char>`
      no longer causes data copying as it did before.
  [3] This change has a small negative performance impact on calling
      `GetIteratorAt()`. Before this CL, it was possible to find the
      segment containing the data at a particular location in constant
      time. But after this CL, finding the segment using
      `std::upper_bound` takes `O(log(segments_.size()))`.

=== Results of performance tests using https://crrev.com/c/5568780 ===

[1] SharedBuffer::Append(base::span) executed 1000 times

    data   |  MacBook Pro 2021   |   Android Pixel 2   |
    size   |  Before  |  After   |  Before  |  After   |
  ---------|----------|----------|----------|----------|
        10 | 0.000031 | 0.000061 | 0.000118 | 0.000236 |
       100 | 0.000078 | 0.000085 | 0.000259 | 0.000455 |
      1000 | 0.000183 | 0.000188 | 0.000744 | 0.000861 |
     10000 | 0.001196 | 0.001208 | 0.006413 | 0.006576 |
    100000 | 0.011896 | 0.011178 | 0.062527 | 0.060133 |
   1000000 | 0.117630 | 0.103153 | 0.590470 | 0.616549 |

[2] SharedBuffer::Append(std::move(Vector)) executed 1000 times

    data   |  MacBook Pro 2021   |   Android Pixel 2   |
    size   |  Before  |  After   |  Before  |  After   |
  ---------|----------|----------|----------|----------|
        10 | 0.000043 | 0.000054 | 0.000116 | 0.000138 |
       100 | 0.000057 | 0.000026 | 0.000238 | 0.000109 |
      1000 | 0.000394 | 0.000024 | 0.002911 | 0.000108 |
     10000 | 0.001012 | 0.000032 | 0.008447 | 0.000117 |
    100000 | 0.014492 | 0.000029 | 0.059325 | 0.000143 |
   1000000 | 0.136521 | 0.000032 | 0.942730 | 0.000178 |

[3] SharedBuffer::GetIteratorAt() executed 1,000,000 times

    data   |  MacBook Pro 2021   |   Android Pixel 2   |
    count  |  Before  |  After   |  Before  |  After   |
  ---------|----------|----------|----------|----------|
         1 | 0.016616 | 0.014748 | 0.082286 | 0.074225 |
        10 | 0.016741 | 0.021211 | 0.082153 | 0.085651 |
       100 | 0.016951 | 0.028073 | 0.085792 | 0.102812 |
      1000 | 0.016806 | 0.036941 | 0.082096 | 0.120652 |
     10000 | 0.016732 | 0.050081 | 0.081994 | 0.143143 |
    100000 | 0.017321 | 0.064823 | 0.081415 | 0.230030 |


Bug: 342859647
Change-Id: I835342e88f26ca48006af183290b9a90fb919216
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5573212
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Commit-Queue: Tsuyoshi Horo <horo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1307864}
5 files changed
tree: 43ad54b89e3af81c75c3af763a282d639c7e49e1
  1. android_webview/
  2. apps/
  3. ash/
  4. base/
  5. build/
  6. build_overrides/
  7. buildtools/
  8. cc/
  9. chrome/
  10. chromecast/
  11. chromeos/
  12. codelabs/
  13. components/
  14. content/
  15. courgette/
  16. crypto/
  17. dbus/
  18. device/
  19. docs/
  20. extensions/
  21. fuchsia_web/
  22. gin/
  23. google_apis/
  24. google_update/
  25. gpu/
  26. headless/
  27. infra/
  28. ios/
  29. ipc/
  30. media/
  31. mojo/
  32. native_client_sdk/
  33. net/
  34. pdf/
  35. ppapi/
  36. printing/
  37. remoting/
  38. rlz/
  39. sandbox/
  40. services/
  41. skia/
  42. sql/
  43. storage/
  44. styleguide/
  45. testing/
  46. third_party/
  47. tools/
  48. ui/
  49. url/
  50. webkit/
  51. .clang-format
  52. .clang-tidy
  53. .clangd
  54. .eslintrc.js
  55. .git-blame-ignore-revs
  56. .gitallowed
  57. .gitattributes
  58. .gitignore
  59. .gitmodules
  60. .gn
  61. .mailmap
  62. .rustfmt.toml
  63. .vpython3
  64. .yapfignore
  65. ATL_OWNERS
  66. AUTHORS
  67. BUILD.gn
  68. CODE_OF_CONDUCT.md
  69. codereview.settings
  70. CPPLINT.cfg
  71. DEPS
  72. DIR_METADATA
  73. LICENSE
  74. LICENSE.chromium_os
  75. OWNERS
  76. PRESUBMIT.py
  77. PRESUBMIT_test.py
  78. PRESUBMIT_test_mocks.py
  79. README.md
  80. WATCHLISTS
README.md

Logo Chromium

Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all users to experience the web.

The project's web site is https://www.chromium.org.

To check out the source code locally, don't use git clone! Instead, follow the instructions on how to get the code.

Documentation in the source is rooted in docs/README.md.

Learn how to Get Around the Chromium Source Code Directory Structure.

For historical reasons, there are some small top level directories. Now the guidance is that new top level directories are for product (e.g. Chrome, Android WebView, Ash). Even if these products have multiple executables, the code should be in subdirectories of the product.

If you found a bug, please file it at https://crbug.com/new.