p4c icon indicating copy to clipboard operation
p4c copied to clipboard

Replace boost constructs with their C++17 STL equivalents

Open fruffy opened this issue 2 years ago • 16 comments

Inspired by https://github.com/ethereum/solidity/issues/7259. Now that we have C++17 support there is some boost code we can remove. The less boost we have, the better. For now, this can apply:

  • [x] changing boost::regex to std::regex
  • [x] changing boost::optional to std::optional
  • [x] changing boost::variant to std::variant
  • [x] changing boost::filesystem to std::filesystem
  • [ ] using https://github.com/boostorg/random for P4Testgen
  • [ ] using https://github.com/boostorg/multiprecision
  • [ ] using https://github.com/boostorg/format
  • [ ] replacing boost::fmt with fmt

fruffy avatar Feb 17 '23 21:02 fruffy

changing boost::filesystem to std::filesystem

I assume we already require GCC 9 ...

apinski-cavium avatar Feb 17 '23 23:02 apinski-cavium

I assume we already require GCC 9 ...

Not quite sure what you mean by that? GCC 9 should support std::filesystem, no?

fruffy avatar Feb 18 '23 22:02 fruffy

I assume we already require GCC 9 ...

Not quite sure what you mean by that? GCC 9 should support std::filesystem, no?

Before GCC 9, std::filesystem was not included in libstdc++ but rather its own library.

apinski-cavium avatar Feb 18 '23 22:02 apinski-cavium

Before GCC 9, std::filesystem was not included in libstdc++ but rather its own library.

I see, but here we should be good, right? The only problem I see is clang and MacOS, I do not know about the level of support there.

fruffy avatar Feb 21 '23 14:02 fruffy

changing boost::regex to std::regex

I'm wondering if this is a good idea. The std::regex implementation in libstdc++ is quite widely considered to be bad and slow with no chances of fixing it without an ABI break which is opposed by many in the C++ community. I wonder how does boost::regex compare to std::regex and if it is even worth replacing it. If we could get rid of all of boost then yes, but we can't do that.

For the other cases I agree, the std:: versions are probably even better than boost once. I just hope there are no parts in boost that were not standardised and we use.

vlstill avatar Mar 02 '23 12:03 vlstill

It looks like the open-source code does not even use boost::regex. So we may not have to worry about this particular replacement.

fruffy avatar Mar 02 '23 13:03 fruffy

Some additional bits here sans boost::format and boost::multiprecision:

  • [x] control-plane/p4RuntimeSymbolTable.cpp uses boost::split and boost::adaptors::reverse. We can readily replace the latter with iterator_range. And the first... well, if abseil would be a dependency, we can use StrSplit from there.
  • [ ] graph backend uses boost::graph. Looks like a hard dependency to me
  • [ ] backends/p4tools/common/lib/model.h uses boost::container::flat_map. Need to re-examine if this is needed.
  • [ ] backends/p4tools/common/lib/util.{cpp, h} uses boost::random::mt19937 => replace with <random>
  • [ ] frontends/parsers/parserDriver.cpp uses boost::iostreams, but has an alternative implementation
  • [x] frontends/p4/def_use.cpp uses boost::hash_range => replace with Util::Hash
  • [ ] ir/solver.h uses boost::container::flat_map
  • [x] test/gtest/ uses boost::replace_first
  • [x] frontends/p4/typeChecking/typeConstraints.h uses boost::split

asl avatar Feb 23 '24 17:02 asl

For boost::container::flat_map dependency I'd rather pull small header-only thing into lib. I believe I even had some implementation somewhere.

asl avatar Feb 23 '24 17:02 asl

  • We can replace the flat maps with custom maps or Abseil maps whatever direction we go.
  • The random library may be required because the standard library does not have good support for arbitrary bit-width integers if I recall correctly. But it is also a module which we can make specific to P4Testgen: https://github.com/boostorg/random
  • The hard dependency for graphs is okay since that back end is optional.
  • iostreams we should get rid of, if we can. Not sure what that entails.
  • It looks like some of the other dependencies could be substituted with helpers or equivalent Abseil helpers.

fruffy avatar Feb 23 '24 18:02 fruffy

Feel free to edit the top-level comment todo list. I will also try to add your points later.

fruffy avatar Feb 23 '24 18:02 fruffy

It looks like the open-source code does not even use boost::regex. So we may not have to worry about this particular replacement.

And for downstream you might try re2

asl avatar Feb 24 '24 01:02 asl

@fruffy I might have some spare time today, do you want me to replace boost string stuff with their abseil equivalents?

asl avatar Feb 27 '24 16:02 asl

That would be very welcome! Which boost string things concretely?

fruffy avatar Feb 27 '24 16:02 fruffy

That would be very welcome! Which boost string things concretely?

I believe it's mostly split. See https://github.com/p4lang/p4c/issues/3898#issuecomment-1961731053

asl avatar Feb 27 '24 16:02 asl

Sounds good to me.

fruffy avatar Feb 27 '24 17:02 fruffy

@fruffy abseil contains absl::BitGen / absl::InsecureBitGen. Maybe worth exploring as a replacement for boost::random

See https://abseil.io/docs/cpp/guides/random and https://abseil.io/about/design/random

asl avatar Mar 08 '24 07:03 asl