Optimize re-serializing deserialized functions
This commit is contained in:
parent
cce6c823f6
commit
f752a1592a
2 changed files with 18 additions and 11 deletions
|
|
@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This change
|
||||||
- New `coffi.mem/null` var for implementing custom types
|
- New `coffi.mem/null` var for implementing custom types
|
||||||
|
|
||||||
### Performance
|
### Performance
|
||||||
|
- Upcall functions serialized from functions returned by deserializing function pointers now use the backing function pointer directly
|
||||||
- Upcall and downcall classes have been changed to be memoized, meaning ASM is no longer invoked every time a function is serialized, which should drastically improve performance where functions are serialized in a hot loop
|
- Upcall and downcall classes have been changed to be memoized, meaning ASM is no longer invoked every time a function is serialized, which should drastically improve performance where functions are serialized in a hot loop
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
||||||
|
|
@ -575,25 +575,31 @@
|
||||||
(mem/global-arena))))
|
(mem/global-arena))))
|
||||||
|
|
||||||
(defmethod mem/serialize* ::fn
|
(defmethod mem/serialize* ::fn
|
||||||
[f [_fn arg-types ret-type & {:keys [raw-fn?]}] arena]
|
[f [_fn arg-types ret-type & {:keys [raw-fn?]} :as typ] arena]
|
||||||
(.upcallStub
|
(if-let [address (::address (meta f))]
|
||||||
(Linker/nativeLinker)
|
(do (assert (= typ (::type (meta f)))
|
||||||
^MethodHandle (cond-> f
|
"The type of a deserialized function must match the type it is re-serialized to.")
|
||||||
(not raw-fn?) (upcall-serde-wrapper arg-types ret-type)
|
address)
|
||||||
:always (upcall-handle arg-types ret-type))
|
(.upcallStub
|
||||||
^FunctionDescriptor (function-descriptor arg-types ret-type)
|
(Linker/nativeLinker)
|
||||||
^Arena arena
|
^MethodHandle (cond-> f
|
||||||
(make-array Linker$Option 0)))
|
(not raw-fn?) (upcall-serde-wrapper arg-types ret-type)
|
||||||
|
:always (upcall-handle arg-types ret-type))
|
||||||
|
^FunctionDescriptor (function-descriptor arg-types ret-type)
|
||||||
|
^Arena arena
|
||||||
|
(make-array Linker$Option 0))))
|
||||||
|
|
||||||
(defmethod mem/deserialize* ::fn
|
(defmethod mem/deserialize* ::fn
|
||||||
[addr [_fn arg-types ret-type & {:keys [raw-fn?]}]]
|
[addr [_fn arg-types ret-type & {:keys [raw-fn?] :as typ}]]
|
||||||
(when-not (mem/null? addr)
|
(when-not (mem/null? addr)
|
||||||
(vary-meta
|
(vary-meta
|
||||||
(-> ^MemorySegment addr
|
(-> ^MemorySegment addr
|
||||||
(downcall-handle (function-descriptor arg-types ret-type))
|
(downcall-handle (function-descriptor arg-types ret-type))
|
||||||
(downcall-fn arg-types ret-type)
|
(downcall-fn arg-types ret-type)
|
||||||
(cond-> (not raw-fn?) (make-serde-wrapper arg-types ret-type)))
|
(cond-> (not raw-fn?) (make-serde-wrapper arg-types ret-type)))
|
||||||
assoc ::address addr)))
|
assoc
|
||||||
|
::address addr
|
||||||
|
::type typ)))
|
||||||
|
|
||||||
;;; Static memory access
|
;;; Static memory access
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue