Skip to content

Inefficient use of a for loop #10529

@dlozaco

Description

@dlozaco

problem

In the following directory: \cloudstack-main\core\src\main\java\org\apache\cloudstack\direct\download\HttpsDirectTemplateDowloader.java at line 101 we see this maybe inefficient use of a for loop:

for (String headerKey: headers.keySet()) {
    req.setHeader(headerKey, headers.get(headerKey));
}

Using this for loop for the entrySet in this way causes a drop in performance because using the method .get() after iterating a keySet() it's not worth it at all.

versions

4.20

The steps to reproduce the bug

What to do about it?

We found a better implementation using this:

for (Map.Entry<String, String> entry : headers.entrySet()) {
    req.setHeader(entry.getKey(), entry.getValue());
}

This avoids unnecessary searches, iterating and setting the values faster than the previous case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions