Skip to content

⚡ Optimize Set allocation in formatYamlValue#54

Open
frostmute wants to merge 1 commit intomainfrom
perf-optimize-yaml-seen-set-18435570760316938437
Open

⚡ Optimize Set allocation in formatYamlValue#54
frostmute wants to merge 1 commit intomainfrom
perf-optimize-yaml-seen-set-18435570760316938437

Conversation

@frostmute
Copy link
Copy Markdown
Owner

💡 What: Modified formatYamlValue in src/utils/yamlUtils.ts to defer the creation of the seen Set until it's actually required when parsing arrays or plain objects. The signature was changed from seen: Set<unknown> = new Set() to seen?: Set<unknown>.
🎯 Why: Previously, a new Set was being allocated for every invocation of formatYamlValue, including all primitives (strings, numbers, booleans) representing the vast majority of calls. Given the number of properties per item, this generated unnecessary memory allocations.
📊 Measured Improvement: Measured with a microbenchmark looping through 50,000 iterations over a representative object with a mix of types. The baseline time was ~790ms, and the optimized time was ~631ms, an improvement of roughly ~20%.


PR created automatically by Jules for task 18435570760316938437 started by @frostmute

Co-authored-by: frostmute <989225+frostmute@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request removes accidentally committed code coverage reports, adds the coverage directory to .gitignore, and refactors the formatYamlValue function in yamlUtils.ts to use lazy initialization for the circular reference detection set. The review feedback identifies that the logic for initializing and managing the seen set is duplicated across the array and object formatting blocks, suggesting a refactor to improve maintainability and adhere to DRY principles.

Comment thread src/utils/yamlUtils.ts
}
if (seen.has(value)) {

const currentSeen = seen || new Set<unknown>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this optimization correctly defers Set creation, initializing currentSeen inside the Array.isArray and isPlainObject blocks leads to code duplication. Consider extracting this logic to a small helper or handling it at the start of the function to keep the implementation DRY.

Comment thread src/utils/yamlUtils.ts
Comment on lines +101 to 109
const currentSeen = seen || new Set<unknown>();
if (currentSeen.has(value)) {
return '"[Circular Reference]"';
}
seen.add(value);
currentSeen.add(value);

const items = value.map((item) => `${indent}- ${formatYamlValue(item, indentLevel + 1, seen)}`);
seen.delete(value);
const items = value.map((item) => `${indent}- ${formatYamlValue(item, indentLevel + 1, currentSeen)}`);
currentSeen.delete(value);
return "\n" + items.join("\n");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for circular reference detection is duplicated between the array and object formatting blocks. This increases maintenance overhead. Refactoring the seen set management into a wrapper or a unified block would improve clarity.

References
  1. General rule: Avoid code duplication to improve maintainability and reduce the risk of bugs when logic changes.

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.

1 participant