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

Commit ab21823

Browse files
committed
don't write out deps entries if nothing changed
Shortcuts a common case.
1 parent 58c7139 commit ab21823

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/deps_log.cc

+30-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,43 @@ bool DepsLog::OpenForWrite(const string& path, string* err) {
5050

5151
bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
5252
const vector<Node*>& nodes) {
53+
// Track whether there's any new data to be recorded.
54+
bool made_change = false;
55+
5356
// Assign ids to all nodes that are missing one.
54-
if (node->id() < 0)
57+
if (node->id() < 0) {
5558
RecordId(node);
59+
made_change = true;
60+
}
5661
for (vector<Node*>::const_iterator i = nodes.begin();
5762
i != nodes.end(); ++i) {
58-
if ((*i)->id() < 0)
63+
if ((*i)->id() < 0) {
5964
RecordId(*i);
65+
made_change = true;
66+
}
67+
}
68+
69+
// See if the new data is different than the existing data, if any.
70+
if (!made_change) {
71+
Deps* deps = GetDeps(node);
72+
if (!deps ||
73+
deps->mtime != mtime ||
74+
deps->node_count != (int)nodes.size()) {
75+
made_change = true;
76+
} else {
77+
for (int i = 0; i < (int)nodes.size(); ++i) {
78+
if (deps->nodes[i] != nodes[i]) {
79+
made_change = true;
80+
break;
81+
}
82+
}
83+
}
6084
}
6185

86+
// Don't write anything if there's no new info.
87+
if (!made_change)
88+
return true;
89+
6290
uint16_t size = 4 * (1 + 1 + (uint16_t)nodes.size());
6391
size |= 0x8000; // Deps record: set high bit.
6492
fwrite(&size, 2, 1, file_);

src/deps_log_test.cc

+46
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,50 @@ TEST_F(DepsLogTest, WriteRead) {
7676
ASSERT_EQ("bar.h", deps->nodes[1]->path());
7777
}
7878

79+
// Verify that adding the same deps twice doesn't grow the file.
80+
TEST_F(DepsLogTest, DoubleEntry) {
81+
// Write some deps to the file and grab its size.
82+
int file_size;
83+
{
84+
State state;
85+
DepsLog log;
86+
string err;
87+
EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err));
88+
ASSERT_EQ("", err);
89+
90+
vector<Node*> deps;
91+
deps.push_back(state.GetNode("foo.h"));
92+
deps.push_back(state.GetNode("bar.h"));
93+
log.RecordDeps(state.GetNode("out.o"), 1, deps);
94+
log.Close();
95+
96+
struct stat st;
97+
ASSERT_EQ(0, stat(kTestFilename, &st));
98+
file_size = (int)st.st_size;
99+
ASSERT_GT(file_size, 0);
100+
}
101+
102+
// Now reload the file, and readd the same deps.
103+
{
104+
State state;
105+
DepsLog log;
106+
string err;
107+
EXPECT_TRUE(log.Load(kTestFilename, &state, &err));
108+
109+
EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err));
110+
ASSERT_EQ("", err);
111+
112+
vector<Node*> deps;
113+
deps.push_back(state.GetNode("foo.h"));
114+
deps.push_back(state.GetNode("bar.h"));
115+
log.RecordDeps(state.GetNode("out.o"), 1, deps);
116+
log.Close();
117+
118+
struct stat st;
119+
ASSERT_EQ(0, stat(kTestFilename, &st));
120+
int file_size_2 = (int)st.st_size;
121+
ASSERT_EQ(file_size, file_size_2);
122+
}
123+
}
124+
79125
} // anonymous namespace

0 commit comments

Comments
 (0)