Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(didc-js): Javascript multi purpose library with candid utils #577

Merged
merged 7 commits into from
Oct 25, 2024

Conversation

keplervital
Copy link
Member

@keplervital keplervital commented Oct 17, 2024

Overview
This library adds similar tools as didc but aims to be used in a Javascript environment.

Requirements

  • Can encode textual candid into a hex string.
  • Can decode a hex string into candid textual representation.
  • Can extract the methods of a candid service IDL.

Considered Solutions
We've also considered a similar approach as the candid ui which leverages a canister to expose a query method, but we ruled that out because we want to use this as the user types in a form which would result in many network requests.

Recommended Solution
Leverage candid_parser to create a javascript package exposing a compiled WebAssembly that uses those crates.

Considerations
This is a new library, there's no consumer yet.

Copy link

Name Max Mem (Kb) Encode Decode
blob 4_224 20_459_319 ($\textcolor{green}{-0.00\%}$) 12_083_680
btreemap 75_456 4_022_699_205 ($\textcolor{green}{-0.00\%}$) 15_670_422_014
nns 128 2_257_486 ($\textcolor{green}{-0.70\%}$) 5_489_730 ($\textcolor{green}{-0.35\%}$)
nns_list_proposal 1_088 7_052_353 ($\textcolor{green}{-0.55\%}$) 81_898_688 ($\textcolor{red}{0.00\%}$)
option_list 128 7_140_179 ($\textcolor{green}{-0.00\%}$) 25_749_409
text 6_336 20_455_442 17_839_559
variant_list 128 7_142_188 24_354_404
vec_int16 16_704 168_582_443 ($\textcolor{green}{-0.00\%}$) 1_073_765_826
  • Parser cost: 18_786_809 ($\textcolor{red}{0.00\%}$)
  • Extra args: 3_240_083
Click to see raw report

---------------------------------------------------

Benchmark: blob
  total:
    instructions: 32.55 M (-0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 20.46 M (-0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 12.08 M (no change)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: text
  total:
    instructions: 38.30 M (no change)
    heap_increase: 99 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 20.46 M (no change)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 17.84 M (no change)
    heap_increase: 33 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: vec_int16
  total:
    instructions: 1.24 B (-0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 168.58 M (-0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 1.07 B (no change)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: btreemap
  total:
    instructions: 19.69 B (-0.00%) (change within noise threshold)
    heap_increase: 1179 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 4.02 B (-0.00%) (change within noise threshold)
    heap_increase: 159 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 15.67 B (no change)
    heap_increase: 1020 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: option_list
  total:
    instructions: 32.89 M (-0.00%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.14 M (-0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 25.75 M (no change)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: variant_list
  total:
    instructions: 31.50 M (no change)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.14 M (no change)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 24.35 M (no change)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns
  total:
    instructions: 27.37 M (-0.13%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  0. Parsing (scope):
    instructions: 18.79 M (0.00%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 2.26 M (-0.70%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 5.49 M (-0.35%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns_list_proposal
  total:
    instructions: 88.95 M (-0.04%) (change within noise threshold)
    heap_increase: 17 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    instructions: 7.05 M (-0.55%) (change within noise threshold)
    heap_increase: 3 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    instructions: 81.90 M (0.00%) (change within noise threshold)
    heap_increase: 14 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: extra_args
  total:
    instructions: 3.24 M (no change)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------
Successfully persisted results to canbench_results.yml

@chenyan-dfinity
Copy link
Contributor

Please consider using https://github.com/dfinity/candid/blob/master/rust/candid_parser/src/bindings/javascript.rs instead of idl2json to derive JSON object. idl2json is incomplete and isn't compatible with agent-js's candid format.

@keplervital
Copy link
Member Author

Please consider using https://github.com/dfinity/candid/blob/master/rust/candid_parser/src/bindings/javascript.rs instead of idl2json to derive JSON object. idl2json is incomplete and isn't compatible with agent-js's candid format.

The JS binding use case is different, this binding gives back JavaScript code iirc, which is not what I’m after.

The target format aims to be a displayable value that we want back, but if you’re not happy with that idl2json lib because it’s incomplete, I’m happy to keep only candid as a target format since it already gives me enough for what I’m building.

@chenyan-dfinity
Copy link
Contributor

Right, it gives back JS code, and if you eval the code, you get a JS value that can be displayed. Candid UI uses that to display JSON format. If it sounds too hacky, we can just remove idl2json.

@keplervital keplervital marked this pull request as ready for review October 23, 2024 09:57
Cargo.toml Outdated Show resolved Hide resolved
tools/README.md Outdated Show resolved Hide resolved
@keplervital keplervital merged commit 1c1cf80 into master Oct 25, 2024
6 checks passed
@keplervital keplervital deleted the kepler/didc-js branch October 25, 2024 08:22
keplervital added a commit to dfinity/orbit that referenced this pull request Oct 25, 2024
Adds candid encoding/decoding to external canister method calls through
the recently published
[@dfinity/didc](https://www.npmjs.com/package/@dfinity/didc) lib from
the candid [PR](dfinity/candid#577).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants