Skip to content

[Improvement] Replace linear proxy scan in MultiProxyManager with a lookup map #1951

@dpol1

Description

@dpol1

What would you like to be improved?

Follow-up to #1935. MultiProxyManager.getConfiguredProxy linearly scans the configured proxies on every metadata-override request, to reuse the configured SCProxy instance for usage accounting. O(n) per override request.

Low priority. A miss is harmless (the metadata proxy is still used, just as a throwaway instance), the override path is opt-in per URL, and getLeastUsed already does a comparable per-request scan. This is cleanup, not a measured bottleneck. To be clear, getLeastUsed is inherently O(n) and is not a target here; I mention it only because O(n) is already accepted on this path.

How should we improve?

The lookup only exists to reuse the configured SCProxy so its usage counter keeps incrementing. The cleanest fix puts proxy identity on the type instead of rebuilding it in a separate key.

  1. Give SCProxy an equals/hashCode based on its identity fields only: protocol and address lowercased, plus port, username and password. Leave out country, area, location, status and usage. None of the identity fields have setters, so the hash stays stable for the life of the object. (A blind "generate equals/hashCode" from the IDE pulls in every field, which is wrong here.)
  2. Build a Map<SCProxy, SCProxy> in both configure() methods and turn getConfiguredProxy into Optional.ofNullable(map.get(proxy)), which makes it O(1).
  3. ProxyUtils.isSameProxy is only called by getConfiguredProxy, so after step 2 it has no callers. Fold its logic into SCProxy.equals and delete isSameProxy along with its private normalize helper. Keep ProxyUtils itself, since it still holds validatePortRange. A normalized string key would also work, but it duplicates the same five-field comparison and leaves two definitions of proxy identity to keep in sync.

No behavior change. This is a code-quality cleanup (single identity definition, less dead code), not a performance fix.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions