WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 42ecff2

Browse files
committed
Reduce number of stat(2) calls, improve directory iteration.
1 parent cecb5f2 commit 42ecff2

File tree

12 files changed

+101
-109
lines changed

12 files changed

+101
-109
lines changed

src/ArchiveDir.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,20 @@ void ArchiveDir::Commit::ensure_parent_directory(const std::filesystem::path &fi
250250
bool ArchiveDir::read_infos_xxx() {
251251
try {
252252
Dir dir(name, !(contents->flags & ARCHIVE_FL_TOP_LEVEL_ONLY));
253-
std::filesystem::path filepath;
254253

255-
while ((filepath = dir.next()) != "") {
256-
if (name == filepath || name_type(filepath) == NAME_IGNORE || !std::filesystem::is_regular_file(filepath)) {
254+
for (const auto& entry: dir) {
255+
if (name == entry.path() || name_type(entry) == NAME_IGNORE || !entry.is_regular_file()) {
257256
continue;
258257
}
259258

260259
files.emplace_back();
261260
auto &f = files[files.size() - 1];
262261

263-
f.name = filepath.string().substr(name.size() + 1);
264-
f.hashes.size = std::filesystem::file_size(filepath);
265-
// auto ftime = std::filesystem::last_write_time(filepath);
262+
f.name = entry.path().string().substr(name.size() + 1);
263+
f.hashes.size = entry.file_size();
264+
// auto ftime = entry.last_write_time();
266265
// f.mtime = decltype(ftime)::clock::to_time_t(ftime);
267-
f.mtime = get_mtime(filepath);
266+
f.mtime = get_mtime(entry.path());
268267
}
269268
}
270269
catch (...) {

src/ArchiveImages.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,32 @@ bool ArchiveImages::read_infos_xxx() {
6262

6363
try {
6464
Dir dir(name, (contents->flags & ARCHIVE_FL_TOP_LEVEL_ONLY) == 0);
65-
std::filesystem::path filepath;
6665

67-
while ((filepath = dir.next()) != "") {
68-
if (name == filepath || name_type(filepath) == NAME_IGNORE || filepath.extension() != ".chd" || !std::filesystem::is_regular_file(filepath)) {
66+
for (const auto& entry : dir) {
67+
if (name == entry.path() || name_type(entry) == NAME_IGNORE || entry.path().extension() != ".chd" || !entry.is_regular_file()) {
6968
continue;
7069
}
7170

7271
files.emplace_back();
7372
auto &f = files[files.size() - 1];
74-
auto filename = filepath.string();
73+
auto filename = entry.path().string();
7574
auto start = name.length() + 1;
7675
f.name = filename.substr(start, filename.length() - start - 4);
7776

7877
try {
78+
#if 0
79+
// auto ftime = entry->last_write_time();
80+
// f.mtime = decltype(ftime)::clock::to_time_t(ftime);
81+
#else
7982
struct stat sb;
8083

81-
if (stat(filepath.c_str(), &sb) != 0) {
84+
if (stat(entry.path().c_str(), &sb) != 0) {
8285
throw Exception("%s", strerror(errno));
8386
}
8487

85-
// auto ftime = std::filesystem::last_write_time(filepath);
86-
// f.mtime = decltype(ftime)::clock::to_time_t(ftime);
8788
f.mtime = sb.st_mtime;
88-
89-
Chd chd(filepath);
89+
#endif
90+
Chd chd(entry.path());
9091

9192
f.hashes.size = chd.size();
9293
f.hashes.set_hashes(chd.hashes);

src/CkmameCache.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void CkmameCache::ensure_extra_maps() {
189189

190190
for (auto &entry : superfluous_delete_list->archives) {
191191
auto file = entry.name;
192-
switch ((name_type(file))) {
192+
switch ((name_type_s(file))) {
193193
case NAME_IMAGES:
194194
case NAME_ZIP: {
195195
Progress::push_message("scanning '" + file + "'");
@@ -269,15 +269,14 @@ bool CkmameCache::enter_dir_in_map_and_list(const DeleteListPtr &list, const std
269269
bool CkmameCache::enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list, const std::string &directory_name, where_t where) {
270270
try {
271271
Dir dir(directory_name, false);
272-
std::filesystem::path filepath;
273272

274-
while (!(filepath = dir.next()).empty()) {
275-
if (name_type(filepath) == NAME_IGNORE) {
273+
for (const auto& entry : dir) {
274+
if (name_type(entry) == NAME_IGNORE) {
276275
continue;
277276
}
278-
if (std::filesystem::is_directory(filepath)) {
279-
Progress::push_message("scanning '" + filepath.string() + "'");
280-
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
277+
if (entry.is_directory()) {
278+
Progress::push_message("scanning '" + entry.path().string() + "'");
279+
auto a = Archive::open(entry.path(), TYPE_ROM, where, 0);
281280
if (a) {
282281
list->add(a.get());
283282
a->close();
@@ -304,10 +303,9 @@ bool CkmameCache::enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list,
304303
bool CkmameCache::enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, const std::string &dir_name, where_t where) {
305304
try {
306305
Dir dir(dir_name, true);
307-
std::filesystem::path filepath;
308306

309-
while (!(filepath = dir.next()).empty()) {
310-
enter_file_in_map_and_list(list, filepath, where);
307+
for (const auto &entry : dir) {
308+
enter_file_in_map_and_list(list, entry, where);
311309
}
312310

313311
Progress::push_message("scanning '" + dir_name + "'");
@@ -325,14 +323,14 @@ bool CkmameCache::enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, co
325323
}
326324

327325

328-
bool CkmameCache::enter_file_in_map_and_list(const DeleteListPtr &list, const std::string &name, where_t where) {
326+
bool CkmameCache::enter_file_in_map_and_list(const DeleteListPtr &list, const std::filesystem::directory_entry &entry, where_t where) {
329327
name_type_t nt;
330328

331-
switch ((nt = name_type(name))) {
329+
switch ((nt = name_type(entry))) {
332330
case NAME_IMAGES:
333331
case NAME_ZIP: {
334-
Progress::push_message("scanning '" + name + "'");
335-
auto a = Archive::open(name, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
332+
Progress::push_message("scanning '" + entry.path().string() + "'");
333+
auto a = Archive::open(entry.path(), nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
336334
if (a) {
337335
list->add(a.get());
338336
a->close();

src/CkmameCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class CkmameCache {
8888

8989
bool enter_dir_in_map_and_list(const DeleteListPtr &list, const std::string &directory_name, where_t where);
9090
static bool enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list, const std::string &directory_name, where_t where);
91-
static bool enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, const std::string &dir_name, where_t where);
92-
static bool enter_file_in_map_and_list(const DeleteListPtr &list, const std::string &name, where_t where);
91+
static bool enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, const std::string &directory_name, where_t where);
92+
static bool enter_file_in_map_and_list(const DeleteListPtr &list, const std::filesystem::directory_entry& entry, where_t where);
9393

9494
const CacheDirectory* get_directory_for_archive(const std::string &name);
9595
};

src/CkmameDB.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,14 @@ void CkmameDB::refresh() {
449449
void CkmameDB::refresh_unzipped() {
450450
try {
451451
Dir dir(directory, false);
452-
std::filesystem::path filepath;
453452

454-
while (!(filepath = dir.next()).empty()) {
455-
if (name_type(filepath) == NAME_IGNORE) {
453+
for (const auto& entry : dir) {
454+
if (name_type(entry) == NAME_IGNORE) {
456455
continue;
457456
}
458-
if (std::filesystem::is_directory(filepath)) {
459-
Progress::push_message("scanning directory '" + filepath.string() + "'");
460-
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
457+
if (entry.is_directory()) {
458+
Progress::push_message("scanning directory '" + entry.path().string() + "'");
459+
auto a = Archive::open(entry.path(), TYPE_ROM, where, 0);
461460
a->close();
462461
Progress::pop_message();
463462
}
@@ -476,17 +475,16 @@ void CkmameDB::refresh_unzipped() {
476475
void CkmameDB::refresh_zipped() {
477476
try {
478477
Dir dir(directory, true);
479-
std::filesystem::path filepath;
480478

481-
while (!(filepath = dir.next()).empty()) {
479+
for (const auto& entry : dir) {
482480
Progress::update();
483481
name_type_t nt;
484482

485-
switch ((nt = name_type(filepath))) {
483+
switch ((nt = name_type(entry))) {
486484
case NAME_IMAGES:
487485
case NAME_ZIP: {
488-
auto progress = Progress::Message("scanning archive '" + filepath.string() + "'");
489-
auto a = Archive::open(filepath, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
486+
auto progress = Progress::Message("scanning archive '" + entry.path().string() + "'");
487+
auto a = Archive::open(entry.path(), nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
490488
if (a) {
491489
a->close();
492490
}

src/DatRepository.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,18 @@ std::vector<std::string> DatRepository::list_dats() {
121121
void DatRepository::update_directory(const std::string &directory, const DatDBPtr &db) {
122122
auto dir = Dir(directory, true);
123123
std::unordered_set<std::string> files;
124-
std::filesystem::path filepath;
125124

126-
while ((filepath = dir.next()) != "") {
125+
for (const auto& entry: dir) {
127126
try {
128-
if (directory == filepath || name_type(filepath) == NAME_IGNORE || !std::filesystem::is_regular_file(filepath)) {
127+
if (directory == entry.path() || name_type(entry) == NAME_IGNORE || !entry.is_regular_file()) {
129128
continue;
130129
}
131130

132-
auto file = filepath.string().substr(directory.size() + 1);
131+
auto file = entry.path().string().substr(directory.size() + 1);
133132

134133
struct stat st{};
135134

136-
if (stat(filepath.c_str(), &st) < 0) {
135+
if (stat(entry.path().c_str(), &st) < 0) {
137136
continue;
138137
}
139138

@@ -152,14 +151,14 @@ void DatRepository::update_directory(const std::string &directory, const DatDBPt
152151
std::vector<DatDB::DatEntry> entries;
153152

154153
try {
155-
auto zip_archive = zip_open(filepath.c_str(), 0, nullptr);
154+
auto zip_archive = zip_open(entry.path().c_str(), 0, nullptr);
156155
if (zip_archive != nullptr) {
157156
for (size_t index = 0; static_cast<int64_t>(index) < zip_get_num_entries(zip_archive, 0); index++) {
158157
try {
159158
auto entry_name = zip_get_name(zip_archive, index, 0);
160159
auto output = OutputContextHeader();
161160

162-
auto source = std::make_shared<ParserSourceZip>(filepath, zip_archive, entry_name);
161+
auto source = std::make_shared<ParserSourceZip>(entry.path(), zip_archive, entry_name);
163162
auto parser_options = Parser::Options{{}, false};
164163
auto parser = Parser::create(source, {}, nullptr, &output, parser_options);
165164
if (parser) {
@@ -177,7 +176,7 @@ void DatRepository::update_directory(const std::string &directory, const DatDBPt
177176
else {
178177
auto output = OutputContextHeader();
179178

180-
auto source = std::make_shared<ParserSourceFile>(filepath);
179+
auto source = std::make_shared<ParserSourceFile>(entry.path());
181180
auto parser_options = Parser::Options{{}, false};
182181
auto parser = Parser::create(source, {}, nullptr, &output, parser_options);
183182

@@ -195,7 +194,7 @@ void DatRepository::update_directory(const std::string &directory, const DatDBPt
195194
db->insert_file(file, st.st_mtime, st.st_size, entries);
196195
}
197196
catch (Exception &ex) {
198-
output.error("can't process '%s': %s", filepath.c_str(), ex.what());
197+
output.error("can't process '%s': %s", entry.path().c_str(), ex.what());
199198
}
200199
}
201200

src/DeleteList.cc

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,37 @@ void DeleteList::add_directory(const std::string &directory, bool omit_known) {
8484

8585
try {
8686
Dir dir(directory, false);
87-
std::filesystem::path filepath;
88-
89-
while ((filepath = dir.next()) != "") {
87+
88+
for (const auto& entry : dir ) {
9089
Progress::update();
91-
if (name_type(filepath) == NAME_IGNORE) {
90+
if (name_type(entry) == NAME_IGNORE) {
9291
continue;
9392
}
9493

9594
bool known = false;
9695

97-
if (std::filesystem::is_directory(filepath)) {
98-
auto filename = filepath.filename();
96+
if (entry.is_directory()) {
97+
auto filename = entry.path().filename();
9998
known = known_games.find(filename) != known_games.end();
10099

101100
if (configuration.roms_zipped) {
102101
if (!known) {
103-
archives.emplace_back(filepath, TYPE_DISK);
102+
archives.emplace_back(entry.path(), TYPE_DISK);
104103
}
105-
list_non_chds(filepath);
104+
list_non_chds(entry.path());
106105
}
107106
else {
108107
if (!known) {
109-
archives.emplace_back(filepath, TYPE_ROM);
108+
archives.emplace_back(entry.path(), TYPE_ROM);
110109
}
111110
}
112111
}
113112
else {
114113
if (configuration.roms_zipped) {
115-
auto ext = filepath.extension();
114+
auto ext = entry.path().extension();
116115

117116
if (ext == ".zip") {
118-
auto stem = filepath.stem();
117+
auto stem = entry.path().stem();
119118
known = known_games.find(stem) != known_games.end();
120119
}
121120
else if (ext == ".chd") {
@@ -131,7 +130,7 @@ void DeleteList::add_directory(const std::string &directory, bool omit_known) {
131130
}
132131

133132
if (!known) {
134-
archives.emplace_back(filepath, TYPE_ROM);
133+
archives.emplace_back(entry.path(), TYPE_ROM);
135134
}
136135
}
137136
}
@@ -169,7 +168,7 @@ int DeleteList::execute() {
169168
}
170169
else {
171170
filetype_t filetype;
172-
switch (name_type(name)) {
171+
switch (name_type_s(name)) {
173172
case NAME_ZIP:
174173
filetype = TYPE_ROM;
175174
break;
@@ -243,11 +242,10 @@ void DeleteList::sort_entries() {
243242
void DeleteList::list_non_chds(const std::string &directory) {
244243
try {
245244
Dir dir(directory, true);
246-
std::filesystem::path filepath;
247-
248-
while ((filepath = dir.next()) != "") {
249-
if (filepath.extension() != ".chd") {
250-
archives.emplace_back(filepath, TYPE_ROM);
245+
246+
for (const auto &entry : dir) {
247+
if (entry.path().extension() != ".chd") {
248+
archives.emplace_back(entry.path(), TYPE_ROM);
251249
}
252250
}
253251
}

src/Dir.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#include "Progress.h"
4040

41-
Dir::Dir(const std::string &name, bool recursive) : index(0) {
41+
Dir::Dir(const std::string &name, bool recursive) {
4242
if (recursive) {
4343
for (const auto &p : std::filesystem::recursive_directory_iterator(name)) {
4444
Progress::update();
@@ -53,11 +53,3 @@ Dir::Dir(const std::string &name, bool recursive) : index(0) {
5353

5454
std::sort(entries.begin(), entries.end());
5555
}
56-
57-
std::filesystem::path Dir::next() {
58-
if (index >= entries.size()) {
59-
return "";
60-
}
61-
62-
return entries[index++];
63-
}

src/Dir.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@
3535
*/
3636

3737
#include <filesystem>
38-
#include <string>
3938
#include <vector>
4039

4140
class Dir {
42-
public:
43-
Dir(const std::string& path, bool recursive);
41+
public:
42+
Dir(const std::string& path, bool recursive);
4443

45-
std::filesystem::path next();
44+
auto begin() {return entries.cbegin();}
45+
auto end() {return entries.cend();}
4646

47-
private:
48-
std::vector<std::filesystem::path> entries;
49-
size_t index;
47+
private:
48+
std::vector<std::filesystem::directory_entry> entries;
5049
};
5150

5251
#endif /* HAD_DIR_H */

0 commit comments

Comments
 (0)