featurevisor 3.1.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,6 @@ module Featurevisor
6
6
  # Feature specific
7
7
  FEATURE_NOT_FOUND = "feature_not_found" # feature is not found in datafile
8
8
  DISABLED = "disabled" # feature is disabled
9
- REQUIRED = "required" # required features are not enabled
10
- OUT_OF_RANGE = "out_of_range" # out of range when mutually exclusive experiments are involved via Groups
11
9
 
12
10
  # Variations specific
13
11
  NO_VARIATIONS = "no_variations" # feature has no variations
@@ -19,7 +17,8 @@ module Featurevisor
19
17
  VARIABLE_DISABLED = "variable_disabled" # feature is disabled, and variable's disabledValue is used
20
18
  VARIABLE_OVERRIDE_VARIATION = "variable_override_variation" # variable overridden from inside a variation
21
19
  VARIABLE_OVERRIDE_RULE = "variable_override_rule" # variable overridden from inside a rule
22
- REQUIRED_FEATURES_UNMET = "required_features_unmet"
20
+ CONDITIONS_UNMET = "conditions_unmet"
21
+ VARIABLE_COMPOSED = "variable_composed"
23
22
 
24
23
  # Common
25
24
  NO_MATCH = "no_match" # no rules matched
@@ -36,49 +35,8 @@ module Featurevisor
36
35
 
37
36
  # Evaluation module for feature flag evaluation
38
37
  module Evaluate
39
- def self.required_features_are_matched(requirements, datafile, options)
40
- return true if requirements.nil?
41
-
42
- items = requirements.is_a?(Array) ? requirements : [requirements]
43
- clean_options = options.reject do |key, _|
44
- %i[feature_key variable_key default_variation_value default_variable_value].include?(key)
45
- end
46
- items.all? do |required|
47
- if required.is_a?(String)
48
- key = required
49
- enabled = true
50
- variation = nil
51
- elsif required.key?(:feature)
52
- key = required[:feature]
53
- enabled = required.key?(:enabled) ? required[:enabled] : true
54
- variation = required[:variation]
55
- else
56
- key = required[:key]
57
- enabled = true
58
- variation = required[:variation]
59
- end
60
-
61
- flag = evaluate_with_modules(clean_options.merge(type: "flag", feature_key: key, datafile: datafile))
62
- next false unless (flag[:enabled] == true) == enabled
63
- next true if variation.nil?
64
-
65
- evaluated_variation = evaluate_with_modules(clean_options.merge(type: "variation", feature_key: key, datafile: datafile))
66
- value = evaluated_variation.key?(:variation_value) ? evaluated_variation[:variation_value] : evaluated_variation.dig(:variation, :value)
67
- value == variation
68
- end
69
- end
70
-
71
38
  def self.variable_override_matches?(override, datafile, context, options)
72
- return false unless required_features_are_matched(override[:requiredFeatures], datafile, options)
73
-
74
- conditions_match = !override[:conditions] || datafile.all_conditions_are_matched(
75
- datafile.parse_conditions_if_stringified(override[:conditions]), context
76
- )
77
- segments_match = !override[:segments] || datafile.all_segments_are_matched(
78
- datafile.parse_segments_if_stringified(override[:segments]), context
79
- )
80
- conditions_match && segments_match &&
81
- (override.key?(:conditions) || override.key?(:segments) || override.key?(:requiredFeatures))
39
+ override.key?(:conditions) && UnifiedConditions.matches?(override[:conditions], options)
82
40
  end
83
41
 
84
42
  # Evaluate with modules
@@ -86,13 +44,36 @@ module Featurevisor
86
44
  # @return [Hash] Evaluation result
87
45
  def self.evaluate_with_modules(options)
88
46
  begin
47
+ session = options[:__featurevisor_session] || EvaluationSession.new
48
+ options = options.merge(__featurevisor_session: session)
49
+ if options[:__featurevisor_prerequisite]
50
+ options[:sticky] = options[:__featurevisor_sticky_features_source].call if options[:__featurevisor_sticky_features_source]
51
+ options[:sticky_variables] = options[:__featurevisor_sticky_variables_source].call if options[:__featurevisor_sticky_variables_source]
52
+ options[:datafile] = options[:__featurevisor_datafile_source].call if options[:__featurevisor_datafile_source]
53
+ end
54
+ options[:dependency_of] = session.dependency_of if options[:__featurevisor_prerequisite]
55
+ options[:dependency_purpose] ||= "condition" if options[:__featurevisor_prerequisite]
56
+ return session.run(options, prerequisite: options[:__featurevisor_prerequisite]) do
89
57
  modules_manager = options[:modules_manager]
90
-
91
- # Run before modules
58
+ # The usual read has no modules. Keep its evaluation options intact so
59
+ # it does not allocate two filtered hashes and a merged hash.
92
60
  result_options = options
93
- if modules_manager
94
- result_options = modules_manager.run_before_modules(result_options)
61
+ if modules_manager&.before_evaluation?
62
+ private_options = options.select { |key, _| private_evaluation_option?(key) }
63
+ public_options = options.reject { |key, _| private_evaluation_option?(key) }
64
+ result_options = modules_manager.run_before_evaluation_modules(public_options)
65
+ result_options = result_options.reject { |key, _| private_evaluation_option?(key) }.merge(private_options)
95
66
  end
67
+ { sticky: :__featurevisor_sticky_features_source,
68
+ sticky_variables: :__featurevisor_sticky_variables_source }.each do |field, source|
69
+ if result_options[field].equal?(options[field])
70
+ result_options[field] = options[source].call if options[source]
71
+ else
72
+ replacement = result_options[field]
73
+ result_options[source] = -> { replacement }
74
+ end
75
+ end
76
+ session.update_current(result_options)
96
77
 
97
78
  # Evaluate
98
79
  evaluation = evaluate(result_options)
@@ -113,12 +94,18 @@ module Featurevisor
113
94
  end
114
95
 
115
96
  # Run after modules
116
- if modules_manager
117
- evaluation = modules_manager.run_after_modules(evaluation, result_options)
97
+ # A callback during evaluation may add a module. In that case its
98
+ # after hook still receives only public options.
99
+ if modules_manager&.after_evaluation?
100
+ evaluation = modules_manager.run_after_evaluation_modules(
101
+ evaluation, result_options.reject { |key, _| private_evaluation_option?(key) }
102
+ )
118
103
  end
119
104
 
120
105
  evaluation
106
+ end
121
107
  rescue => e
108
+ raise if e.is_a?(RequirementError) && options[:__featurevisor_prerequisite]
122
109
  type = options[:type]
123
110
  feature_key = options[:feature_key]
124
111
  variable_key = options[:variable_key]
@@ -132,12 +119,18 @@ module Featurevisor
132
119
  error: e
133
120
  }
134
121
 
122
+ session.remember(options, evaluation) if session
123
+
135
124
  diagnostics.error("Error during evaluation", evaluation)
136
125
 
137
126
  evaluation
138
127
  end
139
128
  end
140
129
 
130
+ def self.private_evaluation_option?(key)
131
+ key.to_s.start_with?("__featurevisor_")
132
+ end
133
+
141
134
  # Main evaluation function
142
135
  # @param options [Hash] Evaluation options
143
136
  # @return [Hash] Evaluation result
@@ -228,7 +221,7 @@ module Featurevisor
228
221
  type: type,
229
222
  feature_key: feature_key,
230
223
  reason: Featurevisor::EvaluationReason::STICKY,
231
- sticky: sticky_feature,
224
+ sticky_feature: sticky_feature,
232
225
  enabled: sticky_feature[:enabled]
233
226
  }
234
227
 
@@ -341,7 +334,7 @@ module Featurevisor
341
334
  end
342
335
 
343
336
  # Forced
344
- force_result = datafile.get_matched_force(feature, context)
337
+ force_result = datafile.get_matched_force(feature, context, options)
345
338
  force = force_result[:force]
346
339
  force_index = force_result[:forceIndex]
347
340
 
@@ -402,25 +395,17 @@ module Featurevisor
402
395
  end
403
396
  end
404
397
 
405
- # Required
406
- required_features = feature[:requiredFeatures] || feature[:required]
407
- if type == "flag" && required_features && !Array(required_features).empty?
408
- required_features_are_enabled = required_features_are_matched(required_features, datafile, options)
409
-
410
- unless required_features_are_enabled
411
- evaluation = {
412
- type: type,
413
- feature_key: feature_key,
414
- reason: Featurevisor::EvaluationReason::REQUIRED,
415
- required: feature[:required],
416
- required_features: feature[:requiredFeatures],
417
- enabled: required_features_are_enabled
418
- }
419
-
420
- diagnostics.debug("required features not enabled", evaluation)
421
-
422
- return evaluation
423
- end
398
+ if type == "flag" && feature.key?(:conditions) &&
399
+ !UnifiedConditions.matches?(feature[:conditions], options)
400
+ evaluation = {
401
+ type: type,
402
+ feature_key: feature_key,
403
+ reason: Featurevisor::EvaluationReason::CONDITIONS_UNMET,
404
+ conditions: feature[:conditions],
405
+ enabled: false
406
+ }
407
+ diagnostics.debug("conditions did not match", evaluation)
408
+ return evaluation
424
409
  end
425
410
 
426
411
  # Bucketing
@@ -438,7 +423,7 @@ module Featurevisor
438
423
  context: context,
439
424
  bucket_by: feature[:bucketBy],
440
425
  bucket_key: bucket_key
441
- }) if modules_manager
426
+ }) if modules_manager&.bucket_key?
442
427
 
443
428
  # bucketValue
444
429
  bucket_value = Featurevisor::Bucketer.get_bucketed_number(bucket_key)
@@ -449,19 +434,19 @@ module Featurevisor
449
434
  bucket_key: bucket_key,
450
435
  context: context,
451
436
  bucket_value: bucket_value
452
- }) if modules_manager
437
+ }) if modules_manager&.bucket_value?
453
438
 
454
439
  matched_traffic = nil
455
440
  matched_allocation = nil
456
441
 
457
442
  if type != "flag"
458
- matched_traffic = datafile.get_matched_traffic(feature[:traffic], context)
443
+ matched_traffic = datafile.get_matched_traffic(feature[:traffic], context, options)
459
444
 
460
445
  if matched_traffic
461
446
  matched_allocation = datafile.get_matched_allocation(matched_traffic, bucket_value)
462
447
  end
463
448
  else
464
- matched_traffic = datafile.get_matched_traffic(feature[:traffic], context)
449
+ matched_traffic = datafile.get_matched_traffic(feature[:traffic], context, options)
465
450
  end
466
451
 
467
452
  if matched_traffic
@@ -485,45 +470,6 @@ module Featurevisor
485
470
 
486
471
  # flag
487
472
  if type == "flag"
488
- # flag: check if mutually exclusive
489
- if feature[:ranges] && feature[:ranges].length > 0
490
- matched_range = feature[:ranges].find do |range|
491
- bucket_value >= range[0] && bucket_value < range[1]
492
- end
493
-
494
- # matched
495
- if matched_range
496
- evaluation = {
497
- type: type,
498
- feature_key: feature_key,
499
- reason: Featurevisor::EvaluationReason::ALLOCATED,
500
- bucket_key: bucket_key,
501
- bucket_value: bucket_value,
502
- rule_key: matched_traffic[:key],
503
- traffic: matched_traffic,
504
- enabled: matched_traffic[:enabled].nil? ? true : matched_traffic[:enabled]
505
- }
506
-
507
- diagnostics.debug("matched", evaluation)
508
-
509
- return evaluation
510
- end
511
-
512
- # no match
513
- evaluation = {
514
- type: type,
515
- feature_key: feature_key,
516
- reason: Featurevisor::EvaluationReason::OUT_OF_RANGE,
517
- bucket_key: bucket_key,
518
- bucket_value: bucket_value,
519
- enabled: false
520
- }
521
-
522
- diagnostics.debug("not matched", evaluation)
523
-
524
- return evaluation
525
- end
526
-
527
473
  # flag: override from rule
528
474
  if matched_traffic.key?(:enabled)
529
475
  evaluation = {
@@ -634,7 +580,8 @@ module Featurevisor
634
580
  variable_schema: variable_schema,
635
581
  variable_value: override[:value],
636
582
  variable_override_index: override_index,
637
- variable_override_key: override[:key]
583
+ variable_override_key: override[:key],
584
+ variable_override_path: override[:keyPath]
638
585
  }
639
586
 
640
587
  diagnostics.debug("variable override from rule", evaluation)
@@ -698,7 +645,8 @@ module Featurevisor
698
645
  variable_schema: variable_schema,
699
646
  variable_value: override[:value],
700
647
  variable_override_index: override_index,
701
- variable_override_key: override[:key]
648
+ variable_override_key: override[:key],
649
+ variable_override_path: override[:keyPath]
702
650
  }
703
651
 
704
652
  diagnostics.debug("variable override from variation", evaluation)
@@ -791,6 +739,7 @@ module Featurevisor
791
739
 
792
740
  evaluation
793
741
  rescue => e
742
+ raise if e.is_a?(RequirementError)
794
743
  evaluation = {
795
744
  type: type,
796
745
  feature_key: feature_key,
@@ -5,12 +5,17 @@ require "json"
5
5
  module Featurevisor
6
6
  # Private datafile and matching adapter used by the evaluator.
7
7
  class InstanceEvaluationDataProvider
8
- attr_reader :schema_version, :revision, :featurevisor_version, :segments, :features, :variables, :diagnostics, :regex_cache
8
+ attr_reader :schema_version, :revision, :featurevisor_version, :segments, :features, :variables, :diagnostics, :regex_cache, :normalized,
9
+ :package_conditions
10
+ attr_accessor :dependency_index
9
11
 
10
12
  # Initialize a new evaluation data provider.
11
13
  # @param options [Hash] Options hash containing datafile and diagnostics
12
14
  # @option options [Hash] :datafile Datafile content
13
15
  # @option options [DiagnosticReporter] :diagnostics Diagnostic reporter
16
+ # The datafile's own keys. Every other top level key is a section or carried as it is.
17
+ DATAFILE_KEYS = %i[schemaVersion revision featurevisorVersion segments features variables].freeze
18
+
14
19
  def initialize(options)
15
20
  datafile = options[:datafile]
16
21
  @diagnostics = options[:diagnostics]
@@ -21,38 +26,19 @@ module Featurevisor
21
26
  @segments = (datafile[:segments] || {}).transform_keys(&:to_sym)
22
27
  @features = (datafile[:features] || {}).transform_keys(&:to_sym)
23
28
  @variables = (datafile[:variables] || {}).transform_keys(&:to_sym)
24
-
25
- # Transform nested structures to use symbol keys
26
- @features.each do |_key, feature|
27
- if feature[:variablesSchema]
28
- feature[:variablesSchema] = feature[:variablesSchema].transform_keys(&:to_sym)
29
- end
30
- if feature[:variations]
31
- feature[:variations].each do |variation|
32
- if variation[:variables]
33
- variation[:variables] = variation[:variables].transform_keys(&:to_sym)
34
- end
35
- if variation[:variableOverrides]
36
- variation[:variableOverrides] = variation[:variableOverrides].transform_keys(&:to_sym)
37
- end
38
- end
39
- end
40
- if feature[:force]
41
- feature[:force].each do |force_rule|
42
- if force_rule[:variables]
43
- force_rule[:variables] = force_rule[:variables].transform_keys(&:to_sym)
44
- end
45
- end
46
- end
47
- if feature[:traffic]
48
- feature[:traffic].each do |traffic_rule|
49
- if traffic_rule[:variables]
50
- traffic_rule[:variables] = traffic_rule[:variables].transform_keys(&:to_sym)
51
- end
52
- end
53
- end
54
- end
29
+ # Every other top level key, such as a schemas section, kept as the datafile has it.
30
+ @sections = datafile.reject { |key, _| DATAFILE_KEYS.include?(key) }
31
+ @normalized = { feature: {}, segment: {}, variable: {} }
32
+ @normalization_slots = { feature: {}, segment: {}, variable: {} }
33
+ @normalization_mutex = Mutex.new
55
34
  @regex_cache = {}
35
+ # A package's conditions as Core writes them, JSON text, parsed once per text
36
+ # for this datafile.
37
+ @package_conditions = {}
38
+ @conditions_cache = {}
39
+ @segments_cache = {}
40
+ @attribute_conditions_cache = {}
41
+ @get_regex = method(:get_regex)
56
42
  end
57
43
 
58
44
  # Get the revision of the datafile
@@ -75,19 +61,22 @@ module Featurevisor
75
61
  segments: @segments,
76
62
  features: @features,
77
63
  variables: @variables
78
- }.compact
64
+ }.compact.merge(@sections)
65
+ end
66
+
67
+ attr_reader :sections
68
+
69
+ # A section by name, such as "schemas": a top level key besides the SDK's own whose value is an object.
70
+ def get_section(name)
71
+ section = @sections[name.to_sym]
72
+ section.is_a?(Hash) ? section : nil
79
73
  end
80
74
 
81
75
  # Get a segment by key
82
76
  # @param segment_key [String] Segment key to retrieve
83
77
  # @return [Hash, nil] Segment data or nil if not found
84
78
  def get_segment(segment_key)
85
- segment = @segments[segment_key.to_sym] || @segments[segment_key]
86
-
87
- return nil unless segment
88
-
89
- segment[:conditions] = parse_conditions_if_stringified(segment[:conditions])
90
- segment
79
+ read_entity(:segment, segment_key, @segments)
91
80
  end
92
81
 
93
82
  def get_segment_keys
@@ -104,7 +93,7 @@ module Featurevisor
104
93
  # @param feature_key [String] Feature key to retrieve
105
94
  # @return [Hash, nil] Feature data or nil if not found
106
95
  def get_feature(feature_key)
107
- @features[feature_key.to_sym] || @features[feature_key]
96
+ read_entity(:feature, feature_key, @features)
108
97
  end
109
98
 
110
99
  # Get variable keys for a feature
@@ -121,7 +110,60 @@ module Featurevisor
121
110
  end
122
111
 
123
112
  def get_global_variable(variable_key)
124
- @variables[variable_key.to_sym] || @variables[variable_key]
113
+ read_entity(:variable, variable_key, @variables)
114
+ end
115
+
116
+ def attribute_only_conditions?(conditions)
117
+ key = conditions.object_id
118
+ cached = @attribute_conditions_cache[key]
119
+ return cached[1] if cached
120
+
121
+ # Retain the syntax with its identity. Cache classification, never matches.
122
+ @attribute_conditions_cache[key] = [conditions, UnifiedConditions.send(:attribute_only?, conditions)]
123
+ @attribute_conditions_cache[key][1]
124
+ end
125
+
126
+ def retain_normalized(previous, incoming)
127
+ @normalization_mutex, @normalization_slots, @normalized = previous.normalization_snapshot
128
+ { feature: :features, segment: :segments, variable: :variables }.each do |kind, name|
129
+ (incoming[name] || {}).each_key do |key|
130
+ @normalization_slots[kind][key.to_sym] = []
131
+ @normalized[kind].delete(key.to_sym)
132
+ end
133
+ end
134
+ end
135
+
136
+ # Share individual cold slots, never a chain of previous providers.
137
+ def normalization_snapshot
138
+ @normalization_mutex.synchronize do
139
+ { feature: @features, segment: @segments, variable: @variables }.each do |kind, definitions|
140
+ next if @normalization_slots[kind].size == definitions.size
141
+ definitions.each_key { |key| @normalization_slots[kind][key] ||= [] }
142
+ end
143
+ [@normalization_mutex, @normalization_slots.transform_values(&:dup), @normalized.transform_values(&:dup)]
144
+ end
145
+ end
146
+
147
+ def read_entity(kind, key, definitions)
148
+ name = key.to_sym
149
+ cached = @normalized[kind][name]
150
+ return cached if cached
151
+ source = definitions[name] || definitions[key]
152
+ return nil unless source
153
+ errors = []
154
+ value = @normalization_mutex.synchronize do
155
+ slot = @normalization_slots[kind][name] ||= []
156
+ if slot.empty?
157
+ normalized, errors = Normalize.entity(kind, name, source)
158
+ slot << normalized
159
+ end
160
+ @normalized[kind][name] = slot.first
161
+ end
162
+ # Publication precedes diagnostics, and application callbacks never hold the mutex.
163
+ errors.each do |error|
164
+ @diagnostics.error(error[:message], { code: error[:code], error: error[:original_error], details: error[:details] })
165
+ end
166
+ value
125
167
  end
126
168
 
127
169
  # Check if a feature has variations
@@ -153,7 +195,7 @@ module Featurevisor
153
195
  invalid_flags = flags.delete("gimsuy")
154
196
  raise ArgumentError, "invalid regular expression flags: #{invalid_flags}" unless invalid_flags.empty?
155
197
 
156
- regex = Regexp.new(regex_string, ruby_flags)
198
+ regex = Regexp.new(Conditions.normalize_portable_regex(regex_string, flags), ruby_flags)
157
199
  @regex_cache[cache_key] = regex
158
200
  @regex_cache[cache_key]
159
201
  end
@@ -168,11 +210,9 @@ module Featurevisor
168
210
  return false
169
211
  end
170
212
 
171
- get_regex_proc = ->(regex_string, regex_flags) { get_regex(regex_string, regex_flags) }
172
-
173
213
  if conditions.is_a?(Hash) && (conditions[:attribute] || conditions["attribute"])
174
214
  begin
175
- result = Conditions.condition_is_matched(conditions, context, get_regex_proc)
215
+ result = Conditions.condition_is_matched(conditions, context, @get_regex)
176
216
  return result
177
217
  rescue => e
178
218
  @diagnostics.warn(e.message, {
@@ -186,11 +226,11 @@ module Featurevisor
186
226
  end
187
227
 
188
228
  if conditions.is_a?(Hash) && conditions[:and] && conditions[:and].is_a?(Array)
189
- return conditions[:and].all? { |c| all_conditions_are_matched(c, context) }
229
+ return !conditions[:and].empty? && conditions[:and].all? { |c| all_conditions_are_matched(c, context) }
190
230
  end
191
231
 
192
232
  if conditions.is_a?(Hash) && conditions["and"] && conditions["and"].is_a?(Array)
193
- return conditions["and"].all? { |c| all_conditions_are_matched(c, context) }
233
+ return !conditions["and"].empty? && conditions["and"].all? { |c| all_conditions_are_matched(c, context) }
194
234
  end
195
235
 
196
236
  if conditions.is_a?(Hash) && conditions[:or] && conditions[:or].is_a?(Array)
@@ -214,7 +254,7 @@ module Featurevisor
214
254
 
215
255
 
216
256
  if conditions.is_a?(Array)
217
- return conditions.all? { |c| all_conditions_are_matched(c, context) }
257
+ return !conditions.empty? && conditions.all? { |c| all_conditions_are_matched(c, context) }
218
258
  end
219
259
 
220
260
  false
@@ -288,8 +328,9 @@ module Featurevisor
288
328
  # @param traffic [Array<Hash>] Traffic array to search
289
329
  # @param context [Hash] Context to evaluate against
290
330
  # @return [Hash, nil] Matched traffic or nil
291
- def get_matched_traffic(traffic, context)
331
+ def get_matched_traffic(traffic, context, options = nil)
292
332
  traffic.find do |t|
333
+ next t.key?(:conditions) && UnifiedConditions.matches?(t[:conditions], options) if options
293
334
  segments = parse_segments_if_stringified(t[:segments])
294
335
  matched = all_segments_are_matched(segments, context)
295
336
  next false unless matched
@@ -314,7 +355,7 @@ module Featurevisor
314
355
  # @param feature_key [String, Hash] Feature key or feature object
315
356
  # @param context [Hash] Context to evaluate against
316
357
  # @return [Hash] Force result with force and forceIndex
317
- def get_matched_force(feature_key, context)
358
+ def get_matched_force(feature_key, context, options = nil)
318
359
  result = {
319
360
  force: nil,
320
361
  forceIndex: nil
@@ -325,6 +366,14 @@ module Featurevisor
325
366
  return result unless feature && feature[:force]
326
367
 
327
368
  feature[:force].each_with_index do |current_force, i|
369
+ if options
370
+ if current_force.key?(:conditions) && UnifiedConditions.matches?(current_force[:conditions], options)
371
+ result[:force] = current_force
372
+ result[:forceIndex] = i
373
+ break
374
+ end
375
+ next
376
+ end
328
377
  if current_force[:conditions] && all_conditions_are_matched(
329
378
  parse_conditions_if_stringified(current_force[:conditions]), context
330
379
  )
@@ -352,9 +401,10 @@ module Featurevisor
352
401
  return conditions unless conditions.is_a?(String)
353
402
 
354
403
  return conditions if conditions == "*"
404
+ return @conditions_cache[conditions] if @conditions_cache.key?(conditions)
355
405
 
356
406
  begin
357
- JSON.parse(conditions)
407
+ @conditions_cache[conditions] = JSON.parse(conditions)
358
408
  rescue => e
359
409
  @diagnostics.error("Error parsing conditions", {
360
410
  error: e,
@@ -371,11 +421,27 @@ module Featurevisor
371
421
  # @return [Array, Hash, String] Parsed segments
372
422
  def parse_segments_if_stringified(segments)
373
423
  if segments.is_a?(String) && (segments.start_with?("{") || segments.start_with?("["))
374
- return JSON.parse(segments)
424
+ return @segments_cache[segments] if @segments_cache.key?(segments)
425
+
426
+ return @segments_cache[segments] = JSON.parse(segments)
375
427
  end
376
428
 
377
429
  segments
378
430
  end
431
+
432
+ # Cache parsed expressions, never context dependent matching results.
433
+ # A new provider owns a new generation after each datafile update.
434
+ def clear_caches
435
+ @dependency_index = nil
436
+ @normalization_mutex.synchronize do
437
+ @normalized.each_value(&:clear)
438
+ @normalization_slots.each_value(&:clear)
439
+ end
440
+ @regex_cache.clear
441
+ @conditions_cache.clear
442
+ @segments_cache.clear
443
+ @attribute_conditions_cache.clear
444
+ end
379
445
  end
380
446
 
381
447
  private_constant :InstanceEvaluationDataProvider