Skip to content

refactor: use std::map::try_emplace() over std::map::insert() #46761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

ckerr
Copy link
Member

@ckerr ckerr commented Apr 24, 2025

Description of Change

Small cleanup in our use of maps: try_emplace() has more efficient move semantics and its name is clearer about what happens when the same key is already present in the map. All other things being equal, we should use it instead of insert().

All human reviews welcomed! 😸 I'm also requesting a review from Copilot just to see what will happen; I've never used that before.

Checklist

Release Notes

Notes: none.

@ckerr ckerr added semver/patch backwards-compatible bug fixes target/36-x-y PR should also be added to the "36-x-y" branch. labels Apr 24, 2025
@ckerr ckerr requested a review from Copilot April 24, 2025 15:30
@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Apr 24, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors map insertions to use std::map::try_emplace() in favor of std::map::insert() to leverage more efficient move semantics and clearer intent when handling duplicate keys.

  • Replaced std::map::insert() with std::map::try_emplace() across several modules.
  • Updated USB, USB delegate, serial delegate, proxying URL loader factory, HID delegate, file system access, and autofill driver factory code.

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
shell/browser/usb/usb_chooser_context.cc Replaced insert() with try_emplace() to simplify device insertion
shell/browser/usb/electron_usb_delegate.cc Updated controller map insertion to use try_emplace()
shell/browser/serial/electron_serial_delegate.cc Updated controller map insertion with try_emplace()
shell/browser/net/proxying_url_loader_factory.cc Converted map insertion code to try_emplace() for improved move semantics
shell/browser/hid/electron_hid_delegate.cc Updated controller map insertion using try_emplace()
shell/browser/file_system_access/file_system_access_permission_context.cc Replaced insert() with try_emplace() in directory mapping
shell/browser/electron_autofill_driver_factory.cc Used try_emplace() for driver mapping improvement
Files not reviewed (1)
  • shell/browser/notifications/mac/cocoa_notification.mm: Language not supported

@ckerr ckerr force-pushed the refactor/prefer-std-map-try-emplace-over-insert branch from 76a7d88 to 1580e42 Compare April 24, 2025 19:03
@ckerr ckerr changed the title refactor: prefer std::map::try_emplace() over std::map::insert() refactor: use std::map::try_emplace() over std::map::insert() Apr 25, 2025
@ckerr ckerr force-pushed the refactor/prefer-std-map-try-emplace-over-insert branch from 1580e42 to 95fb45d Compare April 25, 2025 13:03
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Apr 25, 2025
@ckerr ckerr merged commit b40b4dc into main Apr 25, 2025
56 checks passed
@ckerr ckerr deleted the refactor/prefer-std-map-try-emplace-over-insert branch April 25, 2025 18:11
@release-clerk
Copy link

release-clerk bot commented Apr 25, 2025

No Release Notes

@trop
Copy link
Contributor

trop bot commented Apr 25, 2025

I have automatically backported this PR to "36-x-y", please check out #46794

@trop trop bot added in-flight/36-x-y merged/36-x-y PR was merged to the "36-x-y" branch. and removed target/36-x-y PR should also be added to the "36-x-y" branch. in-flight/36-x-y labels Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged/36-x-y PR was merged to the "36-x-y" branch. semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants