Skip to content

perf: reduce per-call overhead in gin converters#51607

Merged
MarshallOfSound merged 2 commits into
42-x-yfrom
trop/42-x-y-bp-perf-reduce-per-call-overhead-in-gin-converters-1778705836730
May 14, 2026
Merged

perf: reduce per-call overhead in gin converters#51607
MarshallOfSound merged 2 commits into
42-x-yfrom
trop/42-x-y-bp-perf-reduce-per-call-overhead-in-gin-converters-1778705836730

Conversation

@trop

@trop trop Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

Backport of #51599

See that PR for details.

Notes: Improved performance of webRequest header conversions and several other gin converter hot paths.

trop Bot and others added 2 commits May 13, 2026 20:57
Mechanical and structural fixes to avoid unnecessary allocations,
copies, and V8 boundary crossings in gin converter and gin_helper hot
paths.

gin_converters/std_converter.h:
- std::set/std::map FromV8: move the converted element/key into the
  container instead of copying.
- std::set/std::span ToV8: use CreateDataProperty() (the direct define
  path) instead of Object::Set() on freshly-created arrays. The
  NOTREACHED message already referenced CreateDataProperty.
- std::map FromV8: hoist keys->Length() out of the loop.

gin_converters/net_converter.{h,cc}:
- HttpResponseHeaders*::ToV8: build the V8 {name: [value, ...]} object
  directly from a std::map<string, vector<string>> instead of
  round-tripping through base::DictValue (a flat_map with O(d) shift on
  new-key insert) plus content::V8ValueConverter (which allocates and
  walks an entirely separate base::Value graph).
- ResourceRequestBody::ToV8: hoist GetCurrentContext() out of the loop;
  use CreateDataProperty() on the freshly-created array.
- vector<pair<K,V>>::FromV8: hoist keys->Length(); reserve().
- VerifyRequestParams::ToV8: take by const& instead of by value.

gin_converters/blink_converter.cc:
- WebKeyboardEvent::ToV8: cache GetModifiers() once instead of calling
  it nine times.
- ModifiersToArray: reserve() the result vector.

gin_converters/gfx_converter.cc:
- ColorSpace::ToV8: use std::string_view for the four enum-name
  variables that are always assigned compile-time literals.

gin_converters/guid_converter.h:
- Uuid::FromV8: ParseCaseInsensitive() instead of
  ParseLowercase(ToLowerASCII()) — avoids a temporary string.
- Uuid::ToV8: take by const& and pass AsLowercaseString() through
  directly — avoids two heap-allocated string copies.

gin_converters/login_item_settings_converter.{h,cc}:
- LaunchItem/LoginItemSettings ToV8: take by const& (each holds strings
  and vectors).

gin_helper/dictionary.h:
- SetHidden / SetReadOnlyNonConfigurable: take T by const& to match the
  sibling Set/SetReadOnly signatures.
- SetGetter accessor lambda: pass acc_value.Value directly to
  TryConvertToV8 instead of copying it to a local first.

Microbenchmark medians (Linux x86-64 testing build):

  HttpResponseHeaders::ToV8 (16 lines)  30790 ns -> 19020 ns  (-38%)
  Uuid::ToV8                              320 ns ->    92 ns  (-71%)
  Uuid::FromV8 (mixed-case)               561 ns ->   425 ns  (-24%)
  Dictionary::SetHidden+SetReadOnlyNonCfg 3147 ns ->  2627 ns (-17%)
  span<int>::ToV8 (64 ints)             12027 ns -> 10311 ns  (-14%)
  set<string>::ToV8 (11 strings)         3857 ns ->  3485 ns  (-10%)
  WebKeyboardEvent::ToV8                15883 ns -> 15428 ns  (-3%)
  vector<pair>::FromV8 (10 keys)         6288 ns ->  6241 ns  (-1%)
  set<string>::FromV8                    3549 ns ->  3563 ns  (no change)
  map<string,string>::FromV8             7833 ns ->  7820 ns  (no change)
  gfx::ColorSpace::ToV8                  4842 ns ->  4845 ns  (no change)

Co-authored-by: Sam Attard <sattard@anthropic.com>
Replace gin::Dictionary::CreateEmpty() + N x Set("literal", ...) with
gin::DataObjectBuilder in the net and content gin converters that build
plain data objects. DataObjectBuilder internalizes the property keys
(StringToSymbol) and uses CreateDataProperty() instead of Object::Set()
— a single direct property define instead of a setter dispatch plus
prototype-chain walk for every key, on a freshly-created object where
they are observably equivalent.

Converters changed:
  net::AuthChallengeInfo, scoped_refptr<net::X509Certificate>,
  net::CertPrincipal, net::HttpRequestHeaders, net::HttpVersion,
  net::RedirectInfo, net::IPEndPoint, network::ResourceRequest,
  electron::VerifyRequestParams, content::Referrer

Also rewrite Converter<net::HttpRequestHeaders>::FromV8 to iterate the
V8 object directly instead of round-tripping through base::DictValue +
content::V8ValueConverter, which heap-allocates a complete intermediate
base::Value graph just to read the top-level string entries. The
acceptance and value filtering match V8ValueConverterImpl::FromV8Object
(reject non-plain-objects, skip non-string values, swallow throwing
getters).

Behavior change: headers from webRequest's requestHeaders object are
now applied in the object's insertion order rather than alphabetically.
The previous alphabetical order was an artifact of base::DictValue
being a sorted map, and means modified requests fingerprint differently
on the wire from unmodified ones. Insertion order is what apps wrote
and is consistent with how Chromium emits its own default headers.

Co-authored-by: Sam Attard <sattard@anthropic.com>
@trop trop Bot requested a review from MarshallOfSound May 13, 2026 20:57
@trop trop Bot added 42-x-y backport This is a backport PR semver/none labels May 13, 2026
@MarshallOfSound MarshallOfSound enabled auto-merge (squash) May 13, 2026 20:58
@MarshallOfSound MarshallOfSound merged commit e229b1d into 42-x-y May 14, 2026
121 of 128 checks passed
@MarshallOfSound MarshallOfSound deleted the trop/42-x-y-bp-perf-reduce-per-call-overhead-in-gin-converters-1778705836730 branch May 14, 2026 15:24
@release-clerk

release-clerk Bot commented May 14, 2026

Copy link
Copy Markdown

Release Notes Persisted

Improved performance of webRequest header conversions and several other gin converter hot paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant