From 02898525d442a11ed6c7b8f188117fe517ef847b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Mon, 3 Oct 2022 21:41:23 +0200 Subject: [PATCH 1/4] Benchmarks/Michelson: SPLIT_TICKET has a linear max model In the protocol, the model for the SPLIT_TICKET instruction is linear in the max of the sizes of two of its inputs. This commit reflects this in the corresponding benchmark. --- .../lib_benchmarks_proto/interpreter_model.ml | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml index bfeb4cc4e774..1d640eb8d91b 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_model.ml @@ -238,27 +238,6 @@ module Models = struct ~coeff2:(fv (sf "%s_micheline_int_bytes" name)) ~coeff3:(fv (sf "%s_micheline_string_bytes" name)) - let split_ticket_model name = - let module M = struct - type arg_type = int * (int * unit) - - module Def (X : Costlang.S) = struct - open X - - type model_type = size -> size -> size - - let arity = Model.arity_2 - - let model = - lam ~name:"size1" @@ fun size1 -> - lam ~name:"size2" @@ fun size2 -> - free ~name:(fv (sf "%s_const" name)) - + (free ~name:(fv (sf "%s_add_coeff" name)) * max size1 size2) - + (free ~name:(fv (sf "%s_cmp_coeff" name)) * min size1 size2) - end - end in - (module M : Model.Model_impl with type arg_type = int * (int * unit)) - let open_chest_model name = let module M = struct type arg_type = int * (int * unit) @@ -446,7 +425,7 @@ let ir_model ?specialization instr_or_cont = | N_IComb_get | N_IComb | N_IComb_set | N_IUncomb -> model_1 instr_or_cont (affine_model name) | N_ITicket | N_IRead_ticket -> model_0 instr_or_cont (const1_model name) - | N_ISplit_ticket -> model_2 instr_or_cont (split_ticket_model name) + | N_ISplit_ticket -> model_2 instr_or_cont (linear_max_model name) | N_IJoin_tickets -> model_4 instr_or_cont (join_tickets_model name) | N_ISapling_verify_update -> model_2 instr_or_cont (verify_update_model name) -- GitLab From ad4e5979937ae6a7639d9307f2b0fe7a43a3db99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Mon, 3 Oct 2022 21:50:47 +0200 Subject: [PATCH 2/4] Proto/Michelson/Gas: simplify the model of SPLIT_TICKET This commit removes an unused argument in the model of the SPLIT_TICKET instruction. --- src/proto_alpha/lib_protocol/michelson_v1_gas.ml | 2 +- src/proto_alpha/lib_protocol/michelson_v1_gas.mli | 6 +----- src/proto_alpha/lib_protocol/script_interpreter_defs.ml | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml index 38b80c962ea5..4f6bc65f1a23 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.ml @@ -1319,7 +1319,7 @@ module Cost_of = struct let hash_key _ = atomic_step_cost cost_N_IHash_key - let split_ticket _ amount_a amount_b = + let split_ticket amount_a amount_b = atomic_step_cost (cost_N_ISplit_ticket (int_bytes amount_a) (int_bytes amount_b)) diff --git a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli index 9a06aacfc629..59b4cd7df69c 100644 --- a/src/proto_alpha/lib_protocol/michelson_v1_gas.mli +++ b/src/proto_alpha/lib_protocol/michelson_v1_gas.mli @@ -350,11 +350,7 @@ module Cost_of : sig val read_ticket : Gas.cost - val split_ticket : - Script_typed_ir.ticket_amount -> - 'a Script_int.num -> - 'a Script_int.num -> - Gas.cost + val split_ticket : 'a Script_int.num -> 'a Script_int.num -> Gas.cost val join_tickets : 'a Script_typed_ir.comparable_ty -> diff --git a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml index a6e85c0c2b7d..08e3ef4451cc 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml @@ -240,8 +240,8 @@ let cost_of_instr : type a s r f. (a, s, r, f) kinstr -> a -> s -> Gas.cost = let outputs = List.length tx.outputs in Interp_costs.sapling_verify_update_deprecated ~inputs ~outputs | ISplit_ticket _ -> - let ticket = accu and (amount_a, amount_b), _ = stack in - Interp_costs.split_ticket ticket.amount amount_a amount_b + let (amount_a, amount_b), _ = stack in + Interp_costs.split_ticket amount_a amount_b | IJoin_tickets (_, ty, _) -> let ticket_a, ticket_b = accu in Interp_costs.join_tickets ty ticket_a ticket_b -- GitLab From b102950222bff91c9174c8c9c00882f104dcc36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 12 Oct 2022 21:16:38 +0200 Subject: [PATCH 3/4] Benchmarks/Michelson: fix the intercept benchmark for SPLIT_TICKET --- .../lib_benchmarks_proto/interpreter_benchmarks.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml index 4dee5096896a..2bddb435b360 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml @@ -2841,21 +2841,21 @@ module Registration_section = struct let stack_type = ticket unit @$ cpair nat nat @$ bot let () = - let zero = Script_int.zero_n in + let one = Script_int.one_n in let ticket = { ticketer = Alpha_context.Contract.Implicit Environment.Signature.Public_key_hash.zero; contents = (); - amount = Ticket_amount.one; + amount = Ticket_amount.(add one one); } in benchmark_with_fixed_stack ~intercept:true ~name:Interpreter_workload.N_ISplit_ticket ~stack_type - ~stack:(ticket, ((zero, zero), eos)) + ~stack:(ticket, ((one, one), eos)) ~kinstr:split_ticket_instr () -- GitLab From fe5de081b4c44c7e2f77577cdc4a8f8a1f1ceb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Cauderlier?= Date: Wed, 12 Oct 2022 22:10:54 +0200 Subject: [PATCH 4/4] Benchmarks/Michelson: benchmark SPLIT_TICKET on independent values --- .../lib_benchmarks_proto/interpreter_benchmarks.ml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml index 2bddb435b360..3d6bfcdc4027 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml @@ -2867,11 +2867,15 @@ module Registration_section = struct make_default_samplers config.Default_config.sampler in fun () -> - let half_amount = Samplers.Random_value.value nat rng_state in - let half_amount = Script_int.add_n half_amount Script_int.one_n in - let amount = Script_int.add_n half_amount half_amount in + let x_amount = + Script_int.succ_n @@ Samplers.Random_value.value nat rng_state + in + let y_amount = + Script_int.succ_n @@ Samplers.Random_value.value nat rng_state + in + let amount = Script_int.add_n x_amount y_amount in let amount = - (* this is safe because half_amount > 0 *) + (* this is safe because x_amount > 0 and y_amount > 0 *) WithExceptions.Option.get ~loc:__LOC__ @@ Ticket_amount.of_n amount in @@ -2879,7 +2883,7 @@ module Registration_section = struct let ticket = {ticket with amount} in Ex_stack_and_kinstr { - stack = (ticket, ((half_amount, half_amount), eos)); + stack = (ticket, ((x_amount, y_amount), eos)); stack_type; kinstr = split_ticket_instr; }) -- GitLab