Skip to content

Commit f87f750

Browse files
avoid allocation in ObjectId formatter
Summary: We currently use multiple string formatting approaches across the project. This diff stacks works at moving the project away from using `iostream`/`operator<<()`, `folly::format`, and `folly::to<std::string>` and instead opting using the `fmt` library. A few different things are mixed in with this stack, a diff will do some combination of the following: 1) removing definitions of `operator<<()` to discourage their use 2) removing definitions of `toAppend()` to discourage the use of `folly::to<std::string>` (though note, some diffs add this as a stepping stone to `fmt` 3) adding custom `fmt::formatter`s 4) adding unit tests for new and existing `fmt::formatter`s 5) cleaning up/optimizing existing `fmt::formatter`s Reviewed By: taidaii Differential Revision: D80026497 fbshipit-source-id: 98cf181cf77469c93404c05183a222515c1c8436
1 parent 558712d commit f87f750

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

eden/fs/model/ObjectId.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,21 @@ struct hash<facebook::eden::ObjectId> {
219219
} // namespace std
220220

221221
template <>
222-
struct fmt::formatter<facebook::eden::ObjectId> : formatter<std::string> {
222+
struct fmt::formatter<facebook::eden::ObjectId> {
223+
constexpr auto parse(fmt::format_parse_context& ctx) {
224+
return ctx.begin();
225+
}
226+
223227
template <typename Context>
224228
auto format(const facebook::eden::ObjectId& id, Context& ctx) const {
225-
// TODO: avoid allocation here
226-
return formatter<std::string>::format(id.toLogString(), ctx);
229+
auto out = ctx.out();
230+
constexpr char hexValues[] = "0123456789abcdef";
231+
232+
auto bytes = id.getBytes();
233+
for (unsigned char b : bytes) {
234+
*out++ = hexValues[b >> 4];
235+
*out++ = hexValues[b & 0x0f];
236+
}
237+
return out;
227238
}
228239
};

0 commit comments

Comments
 (0)