Skip to content

♻️ [PANA-7375] Eliminate unnecessary conditional node serialization#4629

Open
sethfowler-datadog wants to merge 1 commit into
seth.fowler/PANA-7353-clean-up-naming-in-recording-codefrom
seth.fowler/PANA-7375-eliminate-conditional-node-serialization
Open

♻️ [PANA-7375] Eliminate unnecessary conditional node serialization#4629
sethfowler-datadog wants to merge 1 commit into
seth.fowler/PANA-7353-clean-up-naming-in-recording-codefrom
seth.fowler/PANA-7375-eliminate-conditional-node-serialization

Conversation

@sethfowler-datadog
Copy link
Copy Markdown
Contributor

@sethfowler-datadog sethfowler-datadog commented May 14, 2026

Motivation

There's an awkward edge case that exists today: in very narrow circumstances, we decline to serialize nodes when "bulk serializing" a DOM subtree based on certain conditions, but we will serialize those exact same nodes if they're mutated later.

This breaks some invariants that you'd expect to hold. For example, you'd expect that if you encounter an unserialized node, then one of these three things must be true:

  1. The node's parent also has not been serialized.
  2. The node has a different privacy level than its parent, such that it's excluded from serialization.
  3. The node has been newly added to the document since we last processed mutations.

Conditional serialization adds a fourth, non-obvious case to this list.

Similarly (and this is a case I had reason to care about recently), you might expect that this sequence of actions:

  1. Populate the document.
  2. Take a full snapshot.
  3. Set the text content of a text node to 'foo'.
  4. Take an incremental snapshot.

Would be guaranteed to produce the same final rendering as this sequence of actions:

  1. Populate the document.
  2. Set the text content of a text node to 'foo'.
  3. Take a full snapshot.

However, today, this isn't necessarily so! That's because we apply conditional serialization to text nodes, and changing the text content of a text node can change whether it's included in the full snapshot. If it's not included, subsequent incremental mutations won't be captured either, even if the condition is satisfied as a result of the change. However, the next time a full snapshot is taken, the node will reappear -- hence the reason why the two sequences of action above can produce different results.

We should view this as a bug. There are two ways we could fix it:

  1. We could detect when the conditions that led us to decline to serialize a node have changed, and consider whether we need to serialize it again.
  2. We could just remove the unnecessary conditional serialization logic for these nodes.

I've chosen path #2; given the very limited circumstances in which we apply conditional serialization, and the very limited benefit we obtain from it, I think the best path forward is simply to remove it and eliminate this source of complexity.

Changes

We conditionally decline to serialize nodes in two circumstances today:

  1. Text nodes which are direct children of <head> are not serialized if they contain only whitespace.
  2. Text nodes which are direct children of <datalist>, <select>, or <optgroup>are not serialized if they contain only whitespace and they would otherwise be masked.

In both of these situations, we now unconditionally serialize the nodes.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@sethfowler-datadog sethfowler-datadog requested review from a team as code owners May 14, 2026 18:11
Comment thread packages/rum-core/src/domain/privacy.ts
@sethfowler-datadog sethfowler-datadog force-pushed the seth.fowler/PANA-7375-eliminate-conditional-node-serialization branch from 6cb3215 to cdf7f69 Compare May 15, 2026 10:17
@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented May 15, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.51 KiB 169.46 KiB -55 B -0.03%
Rum Profiler 5.97 KiB 5.97 KiB 0 B 0.00%
Rum Recorder 21.23 KiB 21.20 KiB -31 B -0.14%
Logs 54.70 KiB 54.70 KiB 0 B 0.00%
Rum Slim 127.85 KiB 127.85 KiB 0 B 0.00%
Worker 22.99 KiB 22.99 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0037 0.002 -45.95%
RUM - add action 0.0176 0.0111 -36.93%
RUM - add error 0.0134 0.0099 -26.12%
RUM - add timing 0.0006 0.0004 -33.33%
RUM - start view 0.0144 0.0096 -33.33%
RUM - start/stop session replay recording 0.0013 0.0007 -46.15%
Logs - log message 0.0198 0.0146 -26.26%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 38.50 KiB 38.60 KiB +109 B
RUM - add action 77.70 KiB 65.94 KiB -11.75 KiB
RUM - add timing 41.94 KiB 37.14 KiB -4.80 KiB
RUM - add error 71.38 KiB 70.41 KiB -991 B
RUM - start/stop session replay recording 41.85 KiB 40.10 KiB -1.75 KiB
RUM - start view 483.65 KiB 485.55 KiB +1.90 KiB
Logs - log message 56.74 KiB 56.46 KiB -290 B

🔗 RealWorld

@datadog-datadog-prod-us1-2
Copy link
Copy Markdown

datadog-datadog-prod-us1-2 Bot commented May 15, 2026

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 85.71%
Overall Coverage: 76.98% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: cdf7f69 | Docs | Datadog PR Page | Give us feedback!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants