|
| 1 | +wv2winrt Cacheable Properties |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +WebView2 supports WinRT projection into JavaScript similar to how the previous edgehtml |
| 7 | +WebView support WinRT projection. However unlike the previous WebView, for WebView2 the |
| 8 | +WinRT objects live in a different process from the JavaScript that invokes the WinRT. |
| 9 | +Because of this cross-process access, performance is something we're working on improving. |
| 10 | +To that end, this feature allows you to mark individual runtimeclass properties as |
| 11 | +cacheable so that the JavaScript running in the renderer process can cache the result of |
| 12 | +the property value the first time and avoid subsequent cross-process calls each time the |
| 13 | +property is accessed. Working with partner apps with existing JavaScript that uses WinRT |
| 14 | +this was identified as something in particular that could help improve runtime |
| 15 | +performance. |
| 16 | + |
| 17 | +# Examples |
| 18 | + |
| 19 | +```c# (but really MIDL3) |
| 20 | + [default_interface] |
| 21 | + runtimeclass Toaster |
| 22 | + { |
| 23 | + // This property changes value throughout the lifetime of the object so is not |
| 24 | + // marked readonly. |
| 25 | + Boolean Available { get; }; |
| 26 | + |
| 27 | + // This property has one value for the lifetime of the object so we mark it |
| 28 | + // cacheable to improve runtime performance. |
| 29 | + [cacheable] |
| 30 | + String Model { get; }; |
| 31 | + |
| 32 | + // ... |
| 33 | + } |
| 34 | +``` |
| 35 | + |
| 36 | +# API Details |
| 37 | + |
| 38 | +```c# (but really MIDL3) |
| 39 | +namespace Microsoft.Web.WebView2.Core |
| 40 | +{ |
| 41 | + /// You can use the `cacheable` attribute on a runtimeclass property |
| 42 | + /// or runtimeclass method to indicate that the property value or |
| 43 | + /// method return value can be cached. |
| 44 | + /// |
| 45 | + /// You can apply it to an instance property if the property |
| 46 | + /// value doesn't change for the lifetime of its object instance. |
| 47 | + /// You can apply it to a static property if the property value |
| 48 | + /// doesn't change for the lifetime of the process. |
| 49 | + /// You can apply it to an instance method if when the method is called |
| 50 | + /// with the same parameters it always returns the same value for the |
| 51 | + /// lifetime of its object instance. |
| 52 | + /// You can apply it to a static method if when the method is called |
| 53 | + /// with the same parameters it always returns the same value for the |
| 54 | + /// lifetime of the process. |
| 55 | + /// |
| 56 | + /// When an object is projected into JavaScript via |
| 57 | + /// `CoreWebView2.AddHostObjectToScript`, WebView2 will cache property values |
| 58 | + /// marked with this attribute. This can potentially improve performance by |
| 59 | + /// reducing the number of cross-process calls to obtain the latest value. |
| 60 | + [attributeusage(target_property, target_method)] |
| 61 | + [attributename("cacheable")] |
| 62 | + attribute CacheableAttribute |
| 63 | + { |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +# Appendix |
| 69 | + |
| 70 | +Names considered for the attribute: |
| 71 | + * **Cacheable**: A familiar term also used by python that more closely matches this feature. |
| 72 | + * **ReadOnly**: Similar to C#'s readonly keyword which indicates a value won't change (once |
| 73 | + initialized). But does not convey that the implementer cannot change the value. |
| 74 | + * **Immutable**: Similar to readonly |
| 75 | + * **Const**: Does a better job indicating that the value does not change even by the |
| 76 | + implementer. |
| 77 | + * **Memoizable**: A broader term than cacheable that also applies to methods but more specific |
| 78 | + than cacheable in that it better defines the kind of caching. |
| 79 | + |
| 80 | +For the sample code, the only code you have to write is applying the attribute to the |
| 81 | +property. The only effect this has is to potentially improve performance so there's no other |
| 82 | +code to demonstrate anything. Accordingly, not sure what else to do in the sample code other |
| 83 | +than the MIDL3. |
0 commit comments