Skip to content

Commit 89869af

Browse files
committed
cleanup Filter building.
1 parent 503882e commit 89869af

2 files changed

Lines changed: 72 additions & 35 deletions

File tree

‎lib/trailblazer/activity/variable_mapping/runtime/filter.rb‎

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,25 @@ module Runtime
55
class Filter < Struct.new(:read_name, :write_name, keyword_init: true) # We *could* allow more options here.
66
# This Node represents one step in the input/output pipe,
77
# one filter.
8-
def self.build_node(args_for_provider:, id:, **options)
9-
provider_with_step_interface = args_for_provider[0]
10-
options_for_provider_node = args_for_provider[2] || {} # FIXME: change public API of build_node.
8+
def self.build_node(id:, **options)
9+
circuit = build_circuit(**options)
1110

1211
options_for_filter = options.slice(*members) # extract :read_name, :write_name.
1312

13+
create_node_for(circuit, id: id, **options_for_filter)
14+
end
15+
16+
def self.build_circuit(args_for_step_build:, **options)
17+
provider_arg, provider_options = args_for_step_build
18+
1419
provider_node = Activity::Step.build(
15-
provider_with_step_interface,
16-
**options_for_provider_node, # DISCUSS: what's this?
20+
provider_arg,
21+
**provider_options, # e.g. {exec_context: Filter.new}
1722
binary: false
1823
)
1924

20-
steps = [ # FIXME: better defaulting, please, not very obvious.
21-
[:invoke_provider, node: provider_node],
22-
[:add_value_to_aggregate, :add_value_to_aggregate, Circuit::Task::Adapter::LibInterface::InstanceMethod],
23-
]
24-
25-
pipe = build_circuit(steps, **options)
25+
steps = rows_for_build(provider_node)
2626

27-
create_node_for(pipe, id: id, **options_for_filter)
28-
end
29-
30-
def self.build_circuit(steps, **)
3127
Circuit::Builder.Circuit(*steps)
3228
end
3329

@@ -38,17 +34,26 @@ def self.create_node_for(circuit, id:, **options_for_filter)
3834
return Circuit::Node::MergeToCircuitOptions[id, circuit, Circuit::Processor, exec_context: filter_exec_context]
3935
end
4036

37+
# FIXME: can we reuse nodes?
38+
def self.rows_for_build(provider_node)
39+
[
40+
[:invoke_provider, node: provider_node],
41+
[:add_value_to_aggregate, :add_value_to_aggregate, Circuit::Task::Adapter::LibInterface::InstanceMethod],
42+
]
43+
end
44+
4145
module Out
4246

4347
end
4448

4549
class Conditioned < Filter
46-
def self.build_circuit(*)
47-
provider_with_step_interface = :read_variable_from_application_ctx
48-
49-
provider_node = Activity::Step.build(provider_with_step_interface, binary: false)
50+
def self.build_circuit(**options)
51+
super(**options, args_for_step_build: [:read_variable_from_application_ctx, {}]) # the provider is {#read_variable_from_application_ctx}.
52+
end
5053

51-
circuit_steps = [
54+
# FIXME: we can reuse most nodes?
55+
def self.rows_for_build(provider_node)
56+
[
5257
[:variable_present_in_application_ctx?, :variable_present_in_application_ctx?, Circuit::Task::Adapter::LibInterface::InstanceMethod,
5358
connections: Circuit::Resolver::Conditional.new([Left], nil, :invoke_provider)], # Left means terminate.
5459
[:invoke_provider, node: provider_node, # extract a value
@@ -59,15 +64,16 @@ def self.build_circuit(*)
5964
connections: Circuit::Resolver::Fixed.new(nil) # terminus.
6065
]
6166
]
62-
63-
Circuit::Builder.Circuit(*circuit_steps)
6467
end
68+
69+
# TODO: save memory by not creating identical circuits!
70+
# CIRCUIT = build_node(id: :bla)
6571
end
6672

6773
class Defaulted < Filter
68-
def self.build_circuit(*, default_provider:, **options)
74+
def self.build_node(default_provider:, id:, **options)
6975
# FIXME: playing with "inheritance" here
70-
conditioned_circuit = Conditioned.build_circuit
76+
conditioned_circuit = Conditioned.build_circuit(**options)
7177

7278
default_provider_node = Activity::Step.build(default_provider, binary: false)
7379

@@ -79,7 +85,12 @@ def self.build_circuit(*, default_provider:, **options)
7985
resolver: Circuit::Resolver::Fixed.new(:wrap_value_with_hash),
8086
]
8187

82-
Circuit::Adds.(conditioned_circuit, adds_instruction)
88+
circuit = Circuit::Adds.(conditioned_circuit, adds_instruction)
89+
90+
# FIXME: redundant.
91+
options_for_filter = options.slice(*members) # extract :read_name, :write_name.
92+
93+
create_node_for(circuit, id: id, **options_for_filter)
8394
end
8495
end
8596

‎test/filter_test.rb‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FilterTest < Minitest::Spec
1010
it "read a variable from the {application_ctx}, like In() => {:slug => :my_slug}" do
1111
my_node = Filter.build_node(
1212
id: nil,
13-
args_for_provider: [:read_variable_from_application_ctx],
13+
args_for_step_build: [:read_variable_from_application_ctx, {}],
1414
read_name: :slug,
1515
write_name: :my_slug,
1616
)
@@ -37,7 +37,7 @@ class FilterTest < Minitest::Spec
3737

3838
my_node = Filter.build_node(
3939
id: nil,
40-
args_for_provider: [my_input_provider],
40+
args_for_step_build: [my_input_provider, {}],
4141
write_name: :my_slug,
4242
)
4343

@@ -58,7 +58,7 @@ class FilterTest < Minitest::Spec
5858
assert_equal flow_options, {}
5959
end
6060

61-
it "invoke an {:instance_method}, wrap the value, FIXME: we're using the {args_for_provider} option!!!!!!!!!" do
61+
it "invoke an {:instance_method}, wrap the value" do
6262
my_exec_context = Class.new do
6363
def downcase_slug(ctx, slug:, **)
6464
slug.upcase
@@ -68,7 +68,7 @@ def downcase_slug(ctx, slug:, **)
6868
# FIXME: remove write_name where it's nil
6969
my_node = Filter.build_node(
7070
id: nil,
71-
args_for_provider: [:downcase_slug, StepInterface::InstanceMethod, exec_context: my_exec_context],
71+
args_for_step_build: [:downcase_slug, exec_context: my_exec_context],
7272
read_name: nil,
7373
write_name: :my_slug,
7474
)
@@ -95,7 +95,7 @@ def downcase_slug(ctx, slug:, **)
9595

9696
my_node = Filter.build_node(
9797
id: nil,
98-
args_for_provider: [my_input_provider, StepInterface],
98+
args_for_step_build: [my_input_provider, {}],
9999
)
100100

101101
lib_ctx, flow_options = assert_run my_node, seq: nil, node: true, target_ctx: original_target_ctx = {slug: "generator-1"}.freeze,
@@ -113,7 +113,7 @@ def downcase_slug(ctx, slug:, **)
113113

114114
my_node = Filter.build_node(
115115
id: nil,
116-
args_for_provider: [my_input_provider],
116+
args_for_step_build: [my_input_provider, {}],
117117
write_name: :my_slug,
118118
# adds: [Filter::Build::WRAP_VALUE_WITH_HASH], # FIXME: this is for Filter level, then we also have step_block on the Step level.
119119
)
@@ -177,7 +177,7 @@ def downcase_slug(ctx, slug:, **)
177177
it "writes value to aggregate if it's present (Conditioned)" do
178178
my_node = Filter::Conditioned.build_node(
179179
id: nil,
180-
args_for_provider: [nil], # FIXME: we don't need this here.
180+
args_for_step_build: [nil], # FIXME: we don't need this here.
181181
write_name: :slug,
182182
read_name: :slug,
183183
)
@@ -195,7 +195,6 @@ def downcase_slug(ctx, slug:, **)
195195
it "works even if the incoming signal is something other than {nil}" do
196196
my_node = Filter::Conditioned.build_node(
197197
id: nil,
198-
args_for_provider: [nil], # FIXME: we don't need this here.
199198
write_name: :slug,
200199
read_name: :slug,
201200
)
@@ -209,14 +208,41 @@ def downcase_slug(ctx, slug:, **)
209208
assert_equal flow_options, {}
210209
end
211210

211+
it "Conditioned always uses the same circuit" do
212+
skip
213+
require "benchmark/memory"
214+
215+
Benchmark.memory do |x|
216+
x.report("rebuild") do
217+
my_node = Filter::Conditioned.build_node(
218+
id: nil,
219+
write_name: :slug,
220+
read_name: :slug,
221+
)
222+
223+
my_node_2 = Filter::Conditioned.build_node(
224+
id: nil,
225+
write_name: :slug,
226+
read_name: :slug,
227+
)
228+
end
229+
230+
x.compare!
231+
end
232+
233+
raise
234+
end
212235
end
213236

214237
it "defaults value if absent, and reads value otherwise (Defaulted)" do
215238
my_provider_for_default = ->(ctx, params:, **) { params[:id] }
216239

217240
my_node = Filter::Defaulted.build_node(
218-
id: nil,default_provider: my_provider_for_default, read_name: :global_id, write_name: :my_global_id,
219-
args_for_provider: [nil] # FIXME: remove!
241+
id: nil,
242+
default_provider: my_provider_for_default,
243+
read_name: :global_id,
244+
write_name: :my_global_id,
245+
# args_for_step_build: [nil] # FIXME: remove!
220246
)
221247

222248

0 commit comments

Comments
 (0)