Skip to content

Commit

Permalink
Optimizing bfs and merge algorithm in Candid.encode()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijaga committed Sep 12, 2023
1 parent 679a4d8 commit 66e1bcf
Show file tree
Hide file tree
Showing 7 changed files with 1,311 additions and 1,055 deletions.
86 changes: 86 additions & 0 deletions benchmarks/serde.bench.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Prim "mo:prim";
import Cycles "mo:base/ExperimentalCycles";
import IC "mo:base/ExperimentalInternetComputer";
import Debug "mo:base/Debug";
import Iter "mo:base/Iter";
import Serde "../src";

actor {
type Candid = Serde.Candid;
let { Candid } = Serde;

type Permission = {
#read : [Text];
#write : [Text];
#read_all : ();
#write_all : ();
#admin : ();
};

type User = {
name : Text;
age : Nat;
permission : Permission;
};

type Record = {
group : Text;
users : ?[User];
};

public query func cycles() : async Nat { Cycles.balance()};

public query func deserialize(n: Nat) : async (Nat64, Nat, Nat, Nat) {
let init_cycles = Cycles.balance();

let init_heap = Prim.rts_heap_size();
let init_memory = Prim.rts_memory_size();

let calls = IC.countInstructions(
func() {

let admin_record_candid : Candid = #Record([
("group", #Text("admins")),
("users", #Option(#Array([#Record([("age", #Nat(32)), ("name", #Text("John")), ("permission", #Variant("admin", #Null))])]))),
]);

let user_record_candid : Candid = #Record([
("group", #Text("users")),
("users", #Option(#Array([#Record([("age", #Nat(28)), ("name", #Text("Ali")), ("permission", #Variant("read_all", #Null))]), #Record([("age", #Nat(40)), ("name", #Text("James")), ("permission", #Variant("write_all", #Null))])]))),
]);

let empty_record_candid : Candid = #Record([
("group", #Text("empty")),
("users", #Option(#Array([]))),
]);

let null_record_candid : Candid = #Record([
("group", #Text("null")),
("users", #Option(#Null)),
]);

let base_record_candid : Candid = #Record([
("group", #Text("base")),
("users", #Option(#Array([#Record([("age", #Nat(32)), ("name", #Text("Henry")), ("permission", #Variant("read", #Array([#Text("posts"), #Text("comments")])))]), #Record([("age", #Nat(32)), ("name", #Text("Steven")), ("permission", #Variant("write", #Array([#Text("posts"), #Text("comments")])))])]))),
]);

let records : Candid = #Array([
null_record_candid,
empty_record_candid,
admin_record_candid,
user_record_candid,
base_record_candid,
]);

for (_ in Iter.range(1, n)){
let #ok(blob) = Candid.encodeOne(records, null);
let motoko : ?[Record] = from_candid (blob);
Debug.print(debug_show (motoko));
};
}
);

(calls, Prim.rts_heap_size() - init_heap, Prim.rts_memory_size() - init_memory, init_cycles - Cycles.balance())
};

};
6 changes: 6 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"args": ""
}
},
"canisters": {
"benchmarks": {
"type": "motoko",
"main": "benchmarks/serde.bench.mo"
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
Expand Down
5 changes: 2 additions & 3 deletions mops.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "1.1.0"
version = "2.0.0"
description = "A serialisation and deserialisation library for Motoko."
repository = "https://github.com/NatLabs/serde"
keywords = [ "json", "candid", "parser", "urlencoded", "serialization" ]
Expand All @@ -11,5 +11,4 @@ itertools = "0.1.2"
candid = "1.0.2"
xtended-numbers = "0.2.1"
json = "https://github.com/aviate-labs/json.mo#v0.2.0"
parser-combinators = "https://github.com/aviate-labs/parser-combinators.mo#v0.1.2"
buffer-deque = "0.0.1"
parser-combinators = "https://github.com/aviate-labs/parser-combinators.mo#v0.1.2"
5 changes: 1 addition & 4 deletions src/Candid/Blob/Decoder.mo
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,7 @@ module {

case (x) {
return #err(
"
Serde Decoding Error from fromArg() fn in Candid/Blob/Decoder.mo
Error Log: Could not match '" # debug_show (x) # "' type to any case
"
"\nSerde Decoding Error from fromArg() fn in Candid/Blob/Decoder.mo\n\tError Log: Could not match '" # debug_show (x) # "' type to any case"
);
};
};
Expand Down
Loading

0 comments on commit 66e1bcf

Please sign in to comment.