mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 14:26:02 +01:00
The current representation has two nested presence indicatators (the optional bool, the null pointer). Currently the pointer is never null, but the style guide suggests that T* should be used for parameters that may or may not be present, so we do not need the optional here. > When passing an object's address as an argument, use a reference > unless one of the following cases applies: > > - If the parameter is optional, use a pointer and document that it > may be null. Once the parameter is just a pointer, the ScopedTiming field does not need an optional either, and can just store the pointer. It would be more preferable to have an optional representation of a sometimes-null pointer like optional<T&> to describe a sometimes-null pointer, as this would allow clearer runtime diagnostics when used incorrectly (a check failure in unwrapping) and would be better self-documenting through syntax instead of a comment. But we do not currently have such a primitive.
80 lines
2.5 KiB
C++
80 lines
2.5 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_BASE_TIMINGS_H_
|
|
#define CARBON_TOOLCHAIN_BASE_TIMINGS_H_
|
|
|
|
#include <chrono>
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "toolchain/base/yaml.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Helps track timings for a compile.
|
|
class Timings {
|
|
public:
|
|
// Records a timing for a scope, if the timings object is present.
|
|
class ScopedTiming {
|
|
public:
|
|
// The `timings` may be null, in which case the `ScopedTiming` is a no-op.
|
|
explicit ScopedTiming(Timings* timings, llvm::StringRef label)
|
|
: timings_(timings),
|
|
label_(label),
|
|
start_(timings ? std::chrono::steady_clock::now()
|
|
: std::chrono::steady_clock::time_point::min()) {}
|
|
|
|
~ScopedTiming() {
|
|
if (timings_) {
|
|
timings_->Add(label_, std::chrono::steady_clock::now() - start_);
|
|
}
|
|
}
|
|
|
|
private:
|
|
Timings* timings_;
|
|
llvm::StringRef label_;
|
|
std::chrono::steady_clock::time_point start_;
|
|
};
|
|
|
|
// Adds tracking for nanoseconds, paired with the given label.
|
|
auto Add(llvm::StringRef label, std::chrono::nanoseconds nanoseconds)
|
|
-> void {
|
|
timings_.push_back(
|
|
{.label = std::string(label), .nanoseconds = nanoseconds});
|
|
}
|
|
|
|
auto OutputYaml(llvm::StringRef filename) const -> Yaml::OutputMapping {
|
|
// Explicitly copy the filename.
|
|
return Yaml::OutputMapping([&, filename](Yaml::OutputMapping::Map map) {
|
|
map.Add("filename", filename);
|
|
map.Add("nanoseconds",
|
|
Yaml::OutputMapping([&](Yaml::OutputMapping::Map label_map) {
|
|
std::chrono::nanoseconds total_nanoseconds(0);
|
|
for (const auto& entry : timings_) {
|
|
total_nanoseconds += entry.nanoseconds;
|
|
label_map.Add(entry.label, static_cast<int64_t>(
|
|
entry.nanoseconds.count()));
|
|
}
|
|
label_map.Add("Total",
|
|
static_cast<int64_t>(total_nanoseconds.count()));
|
|
}));
|
|
});
|
|
}
|
|
|
|
private:
|
|
// Timing for a specific label.
|
|
struct Entry {
|
|
std::string label;
|
|
std::chrono::nanoseconds nanoseconds;
|
|
};
|
|
|
|
// The accumulated data on timings.
|
|
llvm::SmallVector<Entry> timings_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_BASE_TIMINGS_H_
|