Optimize r_file_proc_size() reading by blocks instead of chars ##util (#17119)

* Optimize r_file_proc_size() reading by blocks instead of chars ##util

For /proc/self/maps this is almost 2x faster, will be much faster on bigger files

* Make it static, it's not even defined in r_file.h
* Rename `r_file_proc_size()` into `procfile_size()`

Co-authored-by: pancake <pancake@nopcode.org>
Co-authored-by: Anton Kochkov <xvilka@gmail.com>
This commit is contained in:
pancake 2020-06-23 07:25:14 +02:00 committed by GitHub
parent a64cd12951
commit e56c784791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -181,11 +181,12 @@ R_API bool r_file_exists(const char *str) {
return S_IFREG == (S_IFREG & buf.st_mode);
}
R_API long r_file_proc_size(FILE *fd) {
static inline long procfile_size(FILE *fd) {
char buf[1024];
long size = 0;
while (fgetc (fd) != EOF) {
size++;
}
do {
size += fread (buf, 1, sizeof (buf), fd);
} while (!feof (fd));
return size;
}
@ -369,7 +370,7 @@ R_API char *r_file_slurp(const char *str, R_NULLABLE size_t *usz) {
if (r_file_is_regular (str)) {
/* proc file */
fseek (fd, 0, SEEK_SET);
sz = r_file_proc_size (fd);
sz = procfile_size (fd);
} else {
sz = 65536;
}