Skip to content

Commit b1d3d02

Browse files
committed
Pass rep directly to borrow when component implements the resource type
1 parent f25a7db commit b1d3d02

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

design/mvp/CanonicalABI.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,18 @@ def lower_own(cx, src, rt):
968968

969969
def lower_borrow(cx, src, rt):
970970
assert(isinstance(src, Handle))
971+
if cx.inst is rt.impl:
972+
return src.rep
971973
cx.borrow_scope.add(src)
972974
h = BorrowHandle(src.rep, rt, 0, cx.borrow_scope)
973975
return cx.inst.handles.insert(h)
974976
```
977+
The special case in `lower_borrow` is an optimization, recognizing that, when
978+
a borrowed handle is passed to the component that implemented the resource
979+
type, the only thing the borrowed handle is good for is calling
980+
`resource.rep`, so lowering might as well avoid the overhead of creating an
981+
intermediate borrow handle.
982+
975983
Note that the `rt` value that is stored in the runtime `Handle` captures what
976984
is statically known about the handle right before losing this information in
977985
the homogeneous `HandleTable`. Moreoever, as described above, distinct type

design/mvp/canonical-abi/definitions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,8 @@ def lower_own(cx, src, rt):
844844

845845
def lower_borrow(cx, src, rt):
846846
assert(isinstance(src, Handle))
847+
if cx.inst is rt.impl:
848+
return src.rep
847849
cx.borrow_scope.add(src)
848850
h = BorrowHandle(src.rep, rt, 0, cx.borrow_scope)
849851
return cx.inst.handles.insert(h)

design/mvp/canonical-abi/run_tests.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,10 @@ def dtor(x):
384384
nonlocal dtor_value
385385
dtor_value = x
386386

387-
rt = ResourceType(ComponentInstance(), dtor)
387+
rt = ResourceType(ComponentInstance(), dtor) # usable in imports and exports
388388

389389
inst = ComponentInstance()
390+
rt2 = ResourceType(inst, dtor) # only usable in exports
390391
opts = mk_opts()
391392

392393
def host_import(args):
@@ -400,10 +401,11 @@ def host_import(args):
400401
def core_wasm(args):
401402
nonlocal dtor_value
402403

403-
assert(len(args) == 3)
404+
assert(len(args) == 4)
404405
assert(args[0].t == 'i32' and args[0].v == 0)
405406
assert(args[1].t == 'i32' and args[1].v == 1)
406407
assert(args[2].t == 'i32' and args[2].v == 2)
408+
assert(args[3].t == 'i32' and args[3].v == 13)
407409
assert(canon_resource_rep(inst, rt, 0) == 42)
408410
assert(canon_resource_rep(inst, rt, 1) == 43)
409411
assert(canon_resource_rep(inst, rt, 2) == 44)
@@ -436,8 +438,8 @@ def core_wasm(args):
436438

437439
return [Value('i32', 0), Value('i32', 1), Value('i32', 3)]
438440

439-
ft = FuncType([Own(rt),Own(rt),Borrow(rt)],[Own(rt),Own(rt),Own(rt)])
440-
args = [OwnHandle(42, rt, 0), OwnHandle(43, rt, 0), OwnHandle(44, rt, 0)]
441+
ft = FuncType([Own(rt),Own(rt),Borrow(rt),Borrow(rt2)],[Own(rt),Own(rt),Own(rt)])
442+
args = [OwnHandle(42, rt, 0), OwnHandle(43, rt, 0), OwnHandle(44, rt, 0), OwnHandle(13, rt2, 0)]
441443
got,post_return = canon_lift(opts, inst, core_wasm, ft, args)
442444

443445
assert(len(got) == 3)

0 commit comments

Comments
 (0)