blink/bindings: Reduce the overhead of ParkableString.

The overhead of ParkableString is:
- sizeof(ParkableString)
- sizeof(ParkableStringImpl) + digest + compressed data (if compressed)

The first one is small (sizeof(scoped_refptr<>), which is
sizeof(void*)), but the second one is non-trivial. This is due to:
- Need to cache metadata about a string when it is parked, for instance
  Is8Bit(), or its initial length.
- SHA256 hash used to deduplicate parked and unparked strings
- WTF::Mutex, which includes a pthread_mutex_t, which is large on most
  platforms.
- Handling of recursive locking, plus ParkableStringImpl being a
  refcounted class itself.

This is problematic, since it means that callers must know whether the
string is large, and either use WTF::String for small ones, or
ParkableString for large ones. It leads to more complex and duplicated
code, or to potential memory regressions.

As ParkableString is starting to be used in more places (e.g. for
trusted scripts), having to handle this becomes cumbersome and more
error-prone. On the other hand, all the metadata outlined above is not
necessary if the string is never parked, which is the case for small
ones. So the desired state is: short ParkableStrings are cheap enough so
that they can be used no matter the string length.

There are two simple ways to handle this:
- Have ParkableString hold the StringImpl directly for short strings
- Separate the metadata in ParkableStringImpl.

The first approach seems simpler, but would introduce non trivial
complexity in V8's value cache for strings (since it caches a
ref-counted impl for strings, and would need to know about the short and
long ParkableStrings). So the second one is selected here.

The new overhead is:
- Short ParkableStrings:
  - sizeof(ParkableStringImpl) == 3 * sizeof(void*) compared to
    WTF::String
  - savings of sizeof(ParkableStringImpl::Metadata) compared to
    ParkableString
- Long ParkableStrings: 0 vs the previous overhead (by replacing a
  std::unique_ptr<SecureDigest> with a
  std::unique_ptr<ParkableStringImpl::ParkableMetadata>)

Given that client code had to keep a ParkableString *and* a String to
handle short ones, the net cost is 2*sizeof(void*) for short strings,
and no code duplication.

As a consequence, ParkableString can now be used for short and long
strings without any custom code. CharacterData is converted
accordingly. A forthcoming CL will evaluate using ParkableString for
*all* CharacterData instances.

Also some cleanup:
- "git cl lint" warnings: include-what-you-use and explicit constructor
- Move mutex runtime assertions to compile-time checks.
- Make the ordering in parkable_string.cc consistent with the .h

Bug: 1059268, 877044
Change-Id: Ic397751970c6beeaf9d524102f50d5d71ec4a0ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2091494
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748199}
8 files changed
tree: 7377c934adf102bef8760b5316dd746974007af0
  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. cloud_print/
  13. components/
  14. content/
  15. courgette/
  16. crypto/
  17. dbus/
  18. device/
  19. docs/
  20. extensions/
  21. fuchsia/
  22. gin/
  23. google_apis/
  24. google_update/
  25. gpu/
  26. headless/
  27. infra/
  28. ios/
  29. ipc/
  30. jingle/
  31. media/
  32. mojo/
  33. native_client_sdk/
  34. net/
  35. pdf/
  36. ppapi/
  37. printing/
  38. remoting/
  39. rlz/
  40. sandbox/
  41. services/
  42. skia/
  43. sql/
  44. storage/
  45. styleguide/
  46. testing/
  47. third_party/
  48. tools/
  49. ui/
  50. url/
  51. weblayer/
  52. .clang-format
  53. .clang-tidy
  54. .eslintrc.js
  55. .git-blame-ignore-revs
  56. .gitattributes
  57. .gitignore
  58. .gn
  59. .vpython
  60. .vpython3
  61. .yapfignore
  62. AUTHORS
  63. BUILD.gn
  64. CODE_OF_CONDUCT.md
  65. codereview.settings
  66. DEPS
  67. ENG_REVIEW_OWNERS
  68. LICENSE
  69. LICENSE.chromium_os
  70. OWNERS
  71. PRESUBMIT.py
  72. PRESUBMIT_test.py
  73. PRESUBMIT_test_mocks.py
  74. README.md
  75. 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.

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.