From 2efff38c8019ac7d58787878806b54b88c112209 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Thu, 15 Feb 2024 14:09:18 -0500 Subject: [PATCH] Minor improvement switching to real classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes delegation and respond to for many features/gates. ``` Before Calculating ------------------------------------- Typecast.to_json 1.567k (± 1.0%) i/s - 7.850k in 5.008514s After Calculating ------------------------------------- Typecast.to_json 2.493k (± 1.0%) i/s - 12.699k in 5.094881s ``` --- lib/flipper/api/v1/decorators/feature.rb | 15 ++++++++------- lib/flipper/api/v1/decorators/gate.rb | 16 +++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/flipper/api/v1/decorators/feature.rb b/lib/flipper/api/v1/decorators/feature.rb index f13625aeb..c8b8ce681 100644 --- a/lib/flipper/api/v1/decorators/feature.rb +++ b/lib/flipper/api/v1/decorators/feature.rb @@ -5,20 +5,21 @@ module Flipper module Api module V1 module Decorators - class Feature < SimpleDelegator - # Public: The feature being decorated. - alias_method :feature, :__getobj__ + class Feature + def initialize(feature) + @feature = feature + end # Public: Returns instance as hash that is ready to be json dumped. def as_json(exclude_gates: false, exclude_gate_names: false) result = { - 'key' => key, - 'state' => state.to_s, + 'key' => @feature.key, + 'state' => @feature.state.to_s, } unless exclude_gates - gate_values = feature.adapter.get(self) - result['gates'] = gates.map do |gate| + gate_values = @feature.adapter.get(@feature) + result['gates'] = @feature.gates.map do |gate| Decorators::Gate.new(gate, gate_values[gate.key]).as_json(exclude_name: exclude_gate_names) end end diff --git a/lib/flipper/api/v1/decorators/gate.rb b/lib/flipper/api/v1/decorators/gate.rb index f8001cee5..2a8bfd39f 100644 --- a/lib/flipper/api/v1/decorators/gate.rb +++ b/lib/flipper/api/v1/decorators/gate.rb @@ -2,24 +2,18 @@ module Flipper module Api module V1 module Decorators - class Gate < SimpleDelegator - # Public the gate being decorated - alias_method :gate, :__getobj__ - - # Public: the value for the gate from the adapter. - attr_reader :value - + class Gate def initialize(gate, value = nil) - super gate + @gate = gate @value = value end def as_json(exclude_name: false) as_json = { - 'key' => gate.key.to_s, + 'key' => @gate.key.to_s, 'value' => value_as_json, } - as_json['name'] = gate.name.to_s unless exclude_name + as_json['name'] = @gate.name.to_s unless exclude_name as_json end @@ -30,7 +24,7 @@ def as_json(exclude_name: false) # json doesn't like sets def value_as_json - JSON_ARRAY_TYPES.include?(data_type) ? value.to_a : value + JSON_ARRAY_TYPES.include?(@gate.data_type) ? @value.to_a : @value end end end