Skip to content

Commit 25d14df

Browse files
authored
Merge pull request #468 from ahx/local-file-loader
Release cached files after OpenapiFirst.load
2 parents aec7be6 + 2e63abd commit 25d14df

15 files changed

Lines changed: 95 additions & 94 deletions

.github/workflows/ruby.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ jobs:
88
os:
99
- ubuntu-latest
1010
ruby:
11-
- "3.2"
1211
- "3.3"
1312
- "3.4"
1413
- "4.0"

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inherit_mode:
55

66
plugins: rubocop-performance
77
AllCops:
8-
TargetRubyVersion: 3.2.0
8+
TargetRubyVersion: 3.3.0
99
NewCops: enable
1010
SuggestExtensions: false
1111
Exclude:

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
## Unreleased
44

5+
## 3.3.1
6+
7+
- Optimized caching towards reducing retained memory after calling `OpenapiFirst.load` without using a global cache. (Removed `OpenapiFirst.clear_cache!`.)
8+
- Require ruby >= 3.3.0
9+
510
## 3.3.0
611

7-
- OpenapiFirst will now cache the contents of files that have been loaded. If you need to reload your OpenAPI definition for tests or server hot reloading, you can call `OpenapiFirst.clear_cache!`.
12+
- ~OpenapiFirst will now cache the contents of files that have been loaded. If you need to reload your OpenAPI definition for tests or server hot reloading, you can call `OpenapiFirst.clear_cache!`.~
813
- Optimized `OpenapiFirst::Router#match` for faster path matching and reduced memory allocation.
914

1015
## 3.2.1

Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
openapi_first (3.3.0)
4+
openapi_first (3.3.1)
55
drb (~> 2.0)
66
hana (~> 1.3)
77
json_schemer (>= 2.1, < 3.0)
@@ -151,7 +151,7 @@ GEM
151151
racc (~> 1.4)
152152
openapi_parameters (0.11.0)
153153
rack (>= 2.2)
154-
parallel (1.28.0)
154+
parallel (2.0.1)
155155
parser (3.3.11.1)
156156
ast (~> 2.4.1)
157157
racc
@@ -227,11 +227,11 @@ GEM
227227
diff-lcs (>= 1.2.0, < 2.0)
228228
rspec-support (~> 3.13.0)
229229
rspec-support (3.13.7)
230-
rubocop (1.86.0)
230+
rubocop (1.86.1)
231231
json (~> 2.3)
232232
language_server-protocol (~> 3.17.0.2)
233233
lint_roller (~> 1.1.0)
234-
parallel (~> 1.10)
234+
parallel (>= 1.10)
235235
parser (>= 3.3.0.2)
236236
rainbow (>= 2.2.2, < 4.0)
237237
regexp_parser (>= 2.9.3, < 3.0)
@@ -302,4 +302,4 @@ DEPENDENCIES
302302
sinatra
303303

304304
BUNDLED WITH
305-
4.0.6
305+
4.0.10

benchmarks/Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
openapi_first (3.3.0)
4+
openapi_first (3.3.1)
55
drb (~> 2.0)
66
hana (~> 1.3)
77
json_schemer (>= 2.1, < 3.0)
@@ -42,7 +42,7 @@ GEM
4242
optparse
4343
uri
4444
webrick
45-
puma (7.2.0)
45+
puma (8.0.0)
4646
nio4r (~> 2.0)
4747
rack (3.2.6)
4848
rack-protection (4.2.1)

benchmarks/apps/large.ru

Lines changed: 0 additions & 10 deletions
This file was deleted.

benchmarks/large.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
require 'openapi_first'
55

66
Benchmark.memory do |x|
7-
x.report { OpenapiFirst.load('../spec/data/large.yaml') }
7+
x.report do
8+
oad = OpenapiFirst.load('../spec/data/large.yaml')
9+
request = Rack::Request.new(Rack::MockRequest.env_for('/workspaces'))
10+
oad.validate_request(request)
11+
end
812
end

lib/openapi_first.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ module OpenapiFirst
2222

2323
FAILURE = :openapi_first_validation_failure
2424

25-
# Clears cached files
26-
def self.clear_cache!
27-
FileLoader.clear_cache!
28-
end
29-
3025
# @return [Configuration]
3126
def self.configuration
3227
@configuration ||= Configuration.new

lib/openapi_first/builder.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ def initialize(contents, filepath:, config:)
2727
meta_schema = detect_meta_schema(contents, filepath)
2828
@schemer_configuration = build_schemer_config(filepath:, meta_schema:)
2929
@config = config
30-
@contents = RefResolver.for(contents, filepath:)
30+
@file_loader = FileLoader.new
31+
ref_resolver = RefResolver.new(file_loader:)
32+
@contents = ref_resolver.for(contents, filepath:)
3133
end
3234

3335
attr_reader :config
34-
private attr_reader :schemer_configuration
36+
private attr_reader :schemer_configuration, :file_loader
3537

3638
def build_schemer_config(filepath:, meta_schema:)
3739
result = JSONSchemer.configuration.clone
3840
dir = (filepath && File.absolute_path(File.dirname(filepath))) || Dir.pwd
3941
result.base_uri = URI::File.build({ path: "#{dir}/" })
4042
result.ref_resolver = JSONSchemer::CachedResolver.new do |uri|
41-
FileLoader.load(uri.path)
43+
file_loader.load(uri.path)
4244
end
4345
result.meta_schema = meta_schema
4446
result.insert_property_defaults = true

lib/openapi_first/file_loader.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
module OpenapiFirst
77
# @!visibility private
8-
module FileLoader
9-
@cache = {}
10-
@mutex = Mutex.new
8+
class FileLoader
9+
def self.load(file_path)
10+
new.load(file_path)
11+
end
1112

12-
module_function
13+
def initialize
14+
@cache = {}
15+
@mutex = Mutex.new
16+
end
1317

1418
def load(file_path)
1519
@cache[file_path] || @mutex.synchronize do
@@ -29,9 +33,5 @@ def load(file_path)
2933
end
3034
end
3135
end
32-
33-
def clear_cache!
34-
@mutex.synchronize { @cache.clear }
35-
end
3636
end
3737
end

0 commit comments

Comments
 (0)