Skip to content
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

Reduce the number of memory allocations in def-use #4904

Merged
merged 16 commits into from
Sep 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ensure only StorageFactory could access StorageLocation internals, the
locations are immutable otherwise

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl committed Sep 10, 2024
commit 9b9b72469f754d42f185846e206d8f52a5d4179b
9 changes: 6 additions & 3 deletions frontends/p4/def_use.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ class WithFieldsLocation : public StorageLocation {
absl::InlinedVector<std::pair<cstring, const StorageLocation *>, 4>>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm very surprised that a flat_map is faster than an hvec_map here -- for that to be true, almost all the structs/headers in the program have no more than 2-3 fields?

Copy link
Contributor Author

@asl asl Sep 11, 2024

Choose a reason for hiding this comment

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

It is not about "fast". It is about memory consumption. Here we do not allocate memory at all for any structs with 4 or less fields. And we resort to memory allocation for anything more. But yes, normally small flat maps have comparable speed to hash maps and, as far as I can see, most structs are small. There are certainly large ones but they are much rare. And we normally do not have struct larger than, say, 16 fields.

fieldLocations;

friend class StorageFactory;
WithFieldsLocation(const IR::Type *type, cstring name) : StorageLocation(type, name) {}

public:
void createField(cstring name, StorageLocation *field) {
fieldLocations.emplace(name, field);
CHECK_NULL(field);
}
void replaceField(cstring field, StorageLocation *replacement) {
fieldLocations[field] = replacement;
}

friend class StorageFactory;

public:
auto fields() const { return Values(fieldLocations); }
void dbprint(std::ostream &out) const override {
for (auto f : fieldLocations) out << *f.second << " ";
Expand Down Expand Up @@ -150,13 +152,14 @@ class StructLocation : public WithFieldsLocation {
class IndexedLocation : public StorageLocation {
protected:
absl::InlinedVector<const StorageLocation *, 8> elements;
friend class StorageFactory;

void createElement(unsigned index, StorageLocation *element) {
elements[index] = element;
CHECK_NULL(element);
}

friend class StorageFactory;

public:
IndexedLocation(const IR::Type *type, cstring name) : StorageLocation(type, name) {
CHECK_NULL(type);
Expand Down