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

Refactor Util::Enumerator #4513

Merged
merged 22 commits into from
Mar 22, 2024
Merged

Refactor Util::Enumerator #4513

merged 22 commits into from
Mar 22, 2024

Conversation

asl
Copy link
Contributor

@asl asl commented Mar 7, 2024

  • Simplify interface
  • Introduce special-case for ICastable for AsEnumerator
  • Remove non-sense std::function type-erasure for FilterEnumerator, make it less memory hog
  • Make ConcatEnumerator sane, do not do tons of memory allocations each time ConcatEnumerator is created
  • Some convenient helpers (enumerate / concat)
  • Make EnumeratorHandle behave as ForwardIterator, so STL insert, etc. are working with Enumerator
  • Make sure Values and other things from lib/map.h could be used with Enumerator, improve here const-correctness & remove deprecated std::iterator usage in favor of iterator traits
  • Normalize doxygen comments and reorganize code inside lib/enumerator.h, make it cstring-less

@asl asl requested review from fruffy and vlstill March 7, 2024 23:47
@asl
Copy link
Contributor Author

asl commented Mar 7, 2024

@fruffy This is the change that gives that p4tools testcase speedup due to removal of dynamic_cast.

@asl asl force-pushed the enum-trash-refactor branch from 0a1b850 to 0c639af Compare March 7, 2024 23:47
@asl
Copy link
Contributor Author

asl commented Mar 7, 2024

@fruffy What is clang-format version on CI? It fails checks, however, code is clearly clang-format clean for me (with clang-format 17.0.6)

@asl
Copy link
Contributor Author

asl commented Mar 7, 2024

Compilation failed due to gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90031

@fruffy
Copy link
Collaborator

fruffy commented Mar 8, 2024

@fruffy What is clang-format version on CI? It fails checks, however, code is clearly clang-format clean for me (with clang-format 17.0.6)

We pin it to 15.0.6 https://github.com/p4lang/p4c/blob/main/requirements.txt#L7
But there is no reason not to upgrade it. Just needs to be consistent so people do not get confused.

@fruffy fruffy requested review from grg and ChrisDodd March 8, 2024 19:22
Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

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

Thanks! I do not have many comments on the technical side, it's probably best other stakeholders look at the changes here.

: Enumerator<typename Iter::value_type>(),
begin(begin),
end(end),
current(begin),
name(name) {}

public:
cstring toString() const { return this->name + ":" + this->stateName(); }
[[nodiscard]] std::string toString() const {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm most other toString functions return a cstring. I am not certain whether we should refactor this spotwise or do it all at once it one single PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I'd go for small iterative changes. Otherwise this (cstring change) would end with something of enormous size that is impossible to review / digest

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fine with me. We should define some form of heuristic to decide when to use a cstring. toString might be fairly benign for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I'm planning to do first sweep one day to get rid of most obvious cases (e.g. we do not need to cstring of format strings or jsons). Then we can discuss the proper API (cstring vs std::string vs std::string_view usage)

Copy link
Contributor

Choose a reason for hiding this comment

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

Then we can discuss the proper API (cstring vs std::string vs std::string_view usage)

And document it somewhere 🙂

Copy link
Contributor

@grg grg left a comment

Choose a reason for hiding this comment

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

Looks okay to me too.

lib/enumerator.h Outdated Show resolved Hide resolved
: Enumerator<typename Iter::value_type>(),
begin(begin),
end(end),
current(begin),
name(name) {}

public:
cstring toString() const { return this->name + ":" + this->stateName(); }
[[nodiscard]] std::string toString() const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Then we can discuss the proper API (cstring vs std::string vs std::string_view usage)

And document it somewhere 🙂

@asl asl force-pushed the enum-trash-refactor branch from dec1c18 to 3707662 Compare March 9, 2024 01:08
@asl
Copy link
Contributor Author

asl commented Mar 12, 2024

@vlstill @fruffy Will you please take a look when you have a chance? Thanks!

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Looks good overall, but I have concerns about some details.

ir/ir.cpp Outdated Show resolved Hide resolved
lib/enumerator.h Show resolved Hide resolved
lib/enumerator.h Show resolved Hide resolved
lib/enumerator.h Outdated Show resolved Hide resolved
lib/enumerator.h Outdated Show resolved Hide resolved
lib/enumerator.h Outdated Show resolved Hide resolved
lib/map.h Outdated Show resolved Hide resolved
tools/ir-generator/irclass.cpp Outdated Show resolved Hide resolved
@asl asl force-pushed the enum-trash-refactor branch 2 times, most recently from f3c91ac to fc45577 Compare March 12, 2024 20:45
@asl asl requested a review from vlstill March 12, 2024 20:46
Copy link
Contributor

@ChrisDodd ChrisDodd left a comment

Choose a reason for hiding this comment

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

Nice cleanups!

Though I think ideally we'd like to move away from these java enumerators to more static C++-style ones.

@asl
Copy link
Contributor Author

asl commented Mar 14, 2024

Nice cleanups!

Though I think ideally we'd like to move away from these java enumerators to more static C++-style ones.

This is the plan. But one step at a time. We rely on this type-erased things heavily in few places...

@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Mar 15, 2024
@asl asl force-pushed the enum-trash-refactor branch from 60058ef to 12a8cc7 Compare March 15, 2024 19:12
@asl
Copy link
Contributor Author

asl commented Mar 15, 2024

@vlstill Refactored MapEnumerator as well. Please take a look when you will have a chance. CTAD + std::invoke_result made the task simple

@asl asl force-pushed the enum-trash-refactor branch from 12a8cc7 to 563d685 Compare March 19, 2024 21:13
@asl asl force-pushed the enum-trash-refactor branch 2 times, most recently from 53d1ef0 to eb2cdab Compare March 21, 2024 19:17
@asl
Copy link
Contributor Author

asl commented Mar 22, 2024

@vlstill Will you please check the refined PR. Does it address your comments? Thanks!

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Thank you for the changes! Sorry I forgot about needing to re-review this one.

lib/map.h Outdated Show resolved Hide resolved
@asl asl enabled auto-merge March 22, 2024 10:24
@asl asl added this pull request to the merge queue Mar 22, 2024
Merged via the queue into p4lang:main with commit 66d7e6e Mar 22, 2024
17 checks passed
@asl asl deleted the enum-trash-refactor branch March 22, 2024 12:22
grg added a commit that referenced this pull request Apr 5, 2024
Restore parentheses that were accidentally dropped from begin call as
part of recent code improvements (#4513).
@grg grg mentioned this pull request Apr 5, 2024
github-merge-queue bot pushed a commit that referenced this pull request Apr 5, 2024
* restore missing parentheses

Restore parentheses that were accidentally dropped from begin call as
part of recent code improvements (#4513).

* add basic test for ValuesForKeys from map.h
@asl asl linked an issue Apr 18, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get rid of dynamic_cast in filtering enumerator
5 participants