Skip to content

Commit

Permalink
Single lookups into concurrent dictionary (hardkoded#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup authored and leonardo-fernandes committed Jun 13, 2023
1 parent c83b5e7 commit 50f85e6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
14 changes: 2 additions & 12 deletions lib/PuppeteerSharp/ChromeTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,8 @@ await _connection.SendAsync("Target.setAutoAttach", new TargetSetAutoAttachReque

public void AddTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
{
lock (_targetInterceptors)
{
_targetInterceptors.TryGetValue(session, out var interceptors);

if (interceptors == null)
{
interceptors = new List<TargetInterceptor>();
_targetInterceptors.TryAdd(session, interceptors);
}

interceptors.Add(interceptor);
}
var interceptors = _targetInterceptors.GetOrAdd(session, static _ => new());
interceptors.Add(interceptor);
}

public void RemoveTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
Expand Down
14 changes: 2 additions & 12 deletions lib/PuppeteerSharp/FirefoxTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,8 @@ internal class FirefoxTargetManager : ITargetManager

public void AddTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
{
lock (_targetInterceptors)
{
_targetInterceptors.TryGetValue(session, out var interceptors);

if (interceptors == null)
{
interceptors = new List<TargetInterceptor>();
_targetInterceptors.TryAdd(session, interceptors);
}

interceptors.Add(interceptor);
}
var interceptors = _targetInterceptors.GetOrAdd(session, static _ => new());
interceptors.Add(interceptor);
}

public void RemoveTargetInterceptor(CDPSession session, TargetInterceptor interceptor)
Expand Down
6 changes: 2 additions & 4 deletions lib/PuppeteerSharp/FrameTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ internal Task<Frame> WaitForFrameAsync(string frameId)
}

var deferred = new TaskCompletionSource<Frame>(TaskCreationOptions.RunContinuationsAsynchronously);
_waitRequests.TryAdd(frameId, new List<TaskCompletionSource<Frame>>());
_waitRequests.TryGetValue(frameId, out var callbacks);
var callbacks = _waitRequests.GetOrAdd(frameId, static _ => new());
callbacks.Add(deferred);

return deferred.Task;
Expand All @@ -58,8 +57,7 @@ internal void AddFrame(Frame frame)
{
_parentIds.TryAdd(frame.Id, frame.ParentId);

_childIds.TryAdd(frame.ParentId, new List<string>());
_childIds.TryGetValue(frame.ParentId, out var childIds);
var childIds = _childIds.GetOrAdd(frame.ParentId, static _ => new());
childIds.Add(frame.Id);
}
else
Expand Down

0 comments on commit 50f85e6

Please sign in to comment.