Skip to content

Commit c12002f

Browse files
authored
Merge pull request #469 from Shopify/open-noatime
Open with `O_NOATIME`
2 parents 266b935 + 404745f commit c12002f

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Layout/RescueEnsureAlignment:
6464
Layout/FirstHashElementIndentation:
6565
EnforcedStyle: consistent
6666

67+
Layout/FirstArrayElementIndentation:
68+
EnforcedStyle: consistent
69+
6770
Layout/SpaceInsideHashLiteralBraces:
6871
EnforcedStyle: no_space
6972

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
* Open source files and cache entries with `O_NOATIME` when available to reduce disk accesses. See #469.
34
* `bootsnap precompile --gemfile` now look for `.rb` files in the whole gem and not just the `lib/` directory. See #466.
45

56
# 1.17.1

ext/bootsnap/bootsnap.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <fcntl.h>
2121
#include <sys/stat.h>
2222

23+
#ifndef O_NOATIME
24+
#define O_NOATIME 0
25+
#endif
26+
2327
/* 1000 is an arbitrary limit; FNV64 plus some slashes brings the cap down to
2428
* 981 for the cache dir */
2529
#define MAX_CACHEPATH_SIZE 1000
@@ -30,7 +34,7 @@
3034
#define MAX_CREATE_TEMPFILE_ATTEMPT 3
3135

3236
#ifndef RB_UNLIKELY
33-
#define RB_UNLIKELY(x) (x)
37+
#define RB_UNLIKELY(x) (x)
3438
#endif
3539

3640
/*
@@ -366,7 +370,7 @@ open_current_file(char * path, struct bs_cache_key * key, const char ** errno_pr
366370
struct stat statbuf;
367371
int fd;
368372

369-
fd = open(path, O_RDONLY);
373+
fd = open(path, O_RDONLY | O_NOATIME);
370374
if (fd < 0) {
371375
*errno_provenance = "bs_fetch:open_current_file:open";
372376
return fd;
@@ -432,7 +436,7 @@ open_cache_file(const char * path, struct bs_cache_key * key, const char ** errn
432436
{
433437
int fd, res;
434438

435-
fd = open(path, O_RDONLY);
439+
fd = open(path, O_RDONLY | O_NOATIME);
436440
if (fd < 0) {
437441
*errno_provenance = "bs_fetch:open_cache_file:open";
438442
return CACHE_MISS;

ext/bootsnap/extconf.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
require "mkmf"
44

55
if %w[ruby truffleruby].include?(RUBY_ENGINE)
6-
$CFLAGS << " -O3 "
7-
$CFLAGS << " -std=c99"
6+
unless RUBY_PLATFORM.match?(/mswin|mingw|cygwin/)
7+
append_cppflags ["_GNU_SOURCE"] # Needed of O_NOATIME
8+
end
9+
10+
append_cflags ["-O3", "-std=c99"]
811

912
# ruby.h has some -Wpedantic fails in some cases
1013
# (e.g. https://github.com/Shopify/bootsnap/issues/15)
1114
unless ["0", "", nil].include?(ENV["BOOTSNAP_PEDANTIC"])
12-
$CFLAGS << " -Wall"
13-
$CFLAGS << " -Werror"
14-
$CFLAGS << " -Wextra"
15-
$CFLAGS << " -Wpedantic"
15+
append_cflags([
16+
"-Wall",
17+
"-Werror",
18+
"-Wextra",
19+
"-Wpedantic",
1620

17-
$CFLAGS << " -Wno-unused-parameter" # VALUE self has to be there but we don't care what it is.
18-
$CFLAGS << " -Wno-keyword-macro" # hiding return
19-
$CFLAGS << " -Wno-gcc-compat" # ruby.h 2.6.0 on macos 10.14, dunno
20-
$CFLAGS << " -Wno-compound-token-split-by-macro"
21+
"-Wno-unused-parameter", # VALUE self has to be there but we don't care what it is.
22+
"-Wno-keyword-macro", # hiding return
23+
"-Wno-gcc-compat", # ruby.h 2.6.0 on macos 10.14, dunno
24+
"-Wno-compound-token-split-by-macro",
25+
])
2126
end
2227

2328
create_makefile("bootsnap/bootsnap")

test/compile_cache_key_format_test.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,20 @@ def test_key_mtime
6262
end
6363

6464
def test_fetch
65-
if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/
66-
target = "NUL"
67-
expected_file = "#{@tmp_dir}/36/9eba19c29ffe00"
68-
else
69-
target = "/dev/null"
70-
expected_file = "#{@tmp_dir}/8c/d2d180bbd995df"
71-
end
65+
target = Help.set_file("a.rb", "foo = 1")
7266

73-
actual = Bootsnap::CompileCache::Native.fetch(@tmp_dir, target, TestHandler, nil)
67+
cache_dir = File.join(@tmp_dir, "compile_cache")
68+
actual = Bootsnap::CompileCache::Native.fetch(cache_dir, target, TestHandler, nil)
7469
assert_equal("NEATO #{target.upcase}", actual)
7570

76-
data = File.read(expected_file)
71+
entries = Dir["#{cache_dir}/**/*"].select { |f| File.file?(f) }
72+
assert_equal 1, entries.size
73+
cache_file = entries.first
74+
75+
data = File.read(cache_file)
7776
assert_equal("neato #{target}", data.force_encoding(Encoding::BINARY)[64..])
7877

79-
actual = Bootsnap::CompileCache::Native.fetch(@tmp_dir, target, TestHandler, nil)
78+
actual = Bootsnap::CompileCache::Native.fetch(cache_dir, target, TestHandler, nil)
8079
assert_equal("NEATO #{target.upcase}", actual)
8180
end
8281

0 commit comments

Comments
 (0)