Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit c5ea04b

Browse files
committed
Recompact the deps log when it gets too big.
Now that Recompact() keeps all data structures intact, it can just be called at the beginning of a build and the build will still work.
1 parent 1eaacc8 commit c5ea04b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/deps_log.cc

+19-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ DepsLog::~DepsLog() {
3737
}
3838

3939
bool DepsLog::OpenForWrite(const string& path, string* err) {
40+
if (needs_recompaction_) {
41+
Close();
42+
if (!Recompact(path, err))
43+
return false;
44+
}
45+
4046
file_ = fopen(path.c_str(), "ab");
4147
if (!file_) {
4248
*err = strerror(errno);
@@ -161,6 +167,8 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
161167

162168
long offset;
163169
bool read_failed = false;
170+
int unique_dep_record_count = 0;
171+
int total_dep_record_count = 0;
164172
for (;;) {
165173
offset = ftell(f);
166174

@@ -193,8 +201,9 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
193201
deps->nodes[i] = nodes_[deps_data[i]];
194202
}
195203

196-
if (UpdateDeps(out_id, deps))
197-
++dead_record_count_;
204+
total_dep_record_count++;
205+
if (!UpdateDeps(out_id, deps))
206+
++unique_dep_record_count;
198207
} else {
199208
StringPiece path(buf, size);
200209
Node* node = state->GetNode(path);
@@ -225,6 +234,14 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
225234

226235
fclose(f);
227236

237+
// Rebuild the log if there are too many dead records.
238+
int kMinCompactionEntryCount = 1000;
239+
int kCompactionRatio = 3;
240+
if (total_dep_record_count > kMinCompactionEntryCount &&
241+
total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
242+
needs_recompaction_ = true;
243+
}
244+
228245
return true;
229246
}
230247

src/deps_log.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct State;
6161
/// wins, allowing updates to just be appended to the file. A separate
6262
/// repacking step can run occasionally to remove dead records.
6363
struct DepsLog {
64-
DepsLog() : dead_record_count_(0), file_(NULL) {}
64+
DepsLog() : needs_recompaction_(false), file_(NULL) {}
6565
~DepsLog();
6666

6767
// Writing (build-time) interface.
@@ -96,11 +96,9 @@ struct DepsLog {
9696
// Write a node name record, assigning it an id.
9797
bool RecordId(Node* node);
9898

99-
/// Number of deps record read while loading the file that ended up
100-
/// being unused (due to a latter entry superceding it).
101-
int dead_record_count_;
102-
99+
bool needs_recompaction_;
103100
FILE* file_;
101+
104102
/// Maps id -> Node.
105103
vector<Node*> nodes_;
106104
/// Maps id -> deps of that id.

0 commit comments

Comments
 (0)