magick-feature-flags 0.9.21 → 0.9.22

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 467ad50ad54569fd4ac8562da75e37672716b980cfdfb03da79b2f29950fd4c9
4
- data.tar.gz: 0b89ad165e8aad8fb2074821025bd7e50ef2568cc46f5b750bfc4c91c6e11658
3
+ metadata.gz: 42699b3af1d542ac14b10f2b82ea4156da27b68e3e2a07e715cd528f0e3f936b
4
+ data.tar.gz: 0c5235c6cb220201ed0443d1a4633009325d410620d8d28ecbb836cac01bdc25
5
5
  SHA512:
6
- metadata.gz: 7b3fa0a75bd127f269ecafcddfbe6dcba8d45a247d6b47d83085a097f58de6b74c570eb0655fd4a97c78d8335ff1916a57b3eb2b891143d6a2cc585acc5ff87c
7
- data.tar.gz: 79cf7083e34daab0c569e9e05d6e0b123167692f2b206b67ae53321ddabe722861e701346dd5cac80c6b1898be75140ed52fd93470b9a19ccf8113ebf6d91a16
6
+ metadata.gz: 10fb678c4aeaf5a17f721341e01ec937bf9a14042e4a90fa0bcc1aba13c5e91272aba144fa05913a020660ffd602ccf7481d216e92364338c7e4a111794e0896
7
+ data.tar.gz: '079e2d11d6ab0339929f965c1eb05fc934ea88522f26e268df324c366ad28268f87131233fe0f2315a5494328ae5fa18790e8e512fa550372852c3c1d61c3faf'
@@ -52,11 +52,12 @@ module Magick
52
52
  return check_enabled(context) unless perf_metrics_enabled
53
53
 
54
54
  # Performance metrics enabled: measure and record
55
+ # Use inline timing to avoid function call overhead
55
56
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
56
57
  result = check_enabled(context)
57
58
  duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000 # milliseconds
58
59
 
59
- # Record metrics (only if enabled)
60
+ # Record metrics (fast path - minimal overhead)
60
61
  perf_metrics.record(name, 'enabled?', duration, success: true)
61
62
 
62
63
  # Rails 8+ events (only in development or when explicitly enabled)
@@ -36,44 +36,59 @@ module Magick
36
36
  # If redis_enabled is explicitly set, use it; otherwise default to false
37
37
  # It will be enabled later via enable_redis_tracking if Redis adapter is available
38
38
  @redis_enabled = redis_enabled.nil? ? false : redis_enabled
39
+ # Cache expensive checks for performance
40
+ @_rails_events_enabled = defined?(Magick::Rails::Events) && Magick::Rails::Events.rails8?
41
+ @_adapter_available = nil # Will be cached on first check
42
+ @_redis_available = nil # Will be cached on first check
39
43
  end
40
44
 
41
45
  # Public accessor for redis_enabled
42
46
  attr_reader :redis_enabled
43
47
 
44
48
  def record(feature_name, operation, duration, success: true)
49
+ # Fast path: minimize work in mutex
45
50
  feature_name_str = feature_name.to_s
46
- metric = Metric.new(feature_name_str, operation, duration, success: success)
47
51
 
52
+ # Minimize mutex lock time - only update counters
53
+ pending_count = nil
54
+ total_pending = nil
48
55
  @mutex.synchronize do
49
- @metrics << metric
56
+ # Only create Metric object if we're keeping metrics in memory
57
+ # Most of the time we just need counters, not full metric objects
58
+ if @metrics.length < 1000
59
+ metric = Metric.new(feature_name_str, operation, duration, success: success)
60
+ @metrics << metric
61
+ end
50
62
  @usage_count[feature_name_str] += 1
51
63
  @pending_updates[feature_name_str] += 1
64
+ pending_count = @pending_updates[feature_name_str]
65
+ total_pending = @pending_updates.values.sum
52
66
  # Keep only last 1000 metrics (as a safety limit)
53
- # Note: Metrics are removed from memory after flushing to Redis to reduce memory usage
54
67
  @metrics.shift if @metrics.length > 1000
55
68
  end
56
69
 
57
- # Rails 8+ event for usage tracking
58
- if defined?(Magick::Rails::Events) && Magick::Rails::Events.rails8?
70
+ # Rails 8+ event for usage tracking (cached check)
71
+ if @_rails_events_enabled
59
72
  Magick::Rails::Events.usage_tracked(feature_name, operation: operation, duration: duration, success: success)
60
73
  end
61
74
 
62
- # Batch flush to Redis if needed
63
- flush_to_redis_if_needed
75
+ # Batch flush check - only if we're close to batch size (avoid expensive checks)
76
+ # Check pending count first before doing expensive adapter checks
77
+ flush_to_redis_if_needed if pending_count >= @batch_size || total_pending >= @batch_size
64
78
 
65
- metric
79
+ nil # Don't return metric object to avoid allocation overhead
66
80
  end
67
81
 
68
82
  def flush_to_redis_if_needed
69
- # Always try to flush if Redis adapter is available, even if @redis_enabled is false
70
- # This ensures stats are collected even if redis_enabled wasn't set correctly
71
- adapter = Magick.adapter_registry || Magick.default_adapter_registry
72
- return unless adapter # No adapter at all, skip
73
-
74
- redis_available = adapter.is_a?(Magick::Adapters::Registry) && adapter.redis_available?
83
+ # Cache adapter availability check (expensive)
84
+ if @_adapter_available.nil?
85
+ adapter = Magick.adapter_registry || Magick.default_adapter_registry
86
+ @_adapter_available = adapter
87
+ @_redis_available = adapter.is_a?(Magick::Adapters::Registry) && adapter.redis_available? if adapter
88
+ end
75
89
 
76
- return unless redis_available || @redis_enabled
90
+ return unless @_adapter_available
91
+ return unless @_redis_available || @redis_enabled
77
92
  return if @pending_updates.empty?
78
93
 
79
94
  should_flush = false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Magick
4
- VERSION = '0.9.21'
4
+ VERSION = '0.9.22'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magick-feature-flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.21
4
+ version: 0.9.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Lobanov