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

API review for wv2winrt Cacheable Properties #2908

Merged
merged 3 commits into from
Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions specs/wv2winrt-cacheable-properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
wv2winrt Cacheable Properties
===

# Background

WebView2 supports WinRT projection into JavaScript similar to how the previous edgehtml
WebView support WinRT projection. However unlike the previous WebView, for WebView2 the
WinRT objects live in a different process from the JavaScript that invokes the WinRT.
Because of this cross-process access, performance is something we're working on improving.
To that end, this feature allows you to mark individual runtimeclass properties as
cacheable so that the JavaScript running in the renderer process can cache the result of
the property value the first time and avoid subsequent cross-process calls each time the
property is accessed. Working with partner apps with existing JavaScript that uses WinRT
this was identified as something in particular that could help improve runtime
performance.

# Examples

```c# (but really MIDL3)
[default_interface]
runtimeclass Toaster
{
// This property changes value throughout the lifetime of the object so is not
// marked readonly.
Boolean Available { get; };

// This property has one value for the lifetime of the object so we mark it
// cacheable to improve runtime performance.
[cacheable]
String Model { get; };

// ...
}
```

# API Details

```c# (but really MIDL3)
namespace Microsoft.Web.WebView2.Core
{
/// You can use the `cacheable` attribute on a runtimeclass property
/// or runtimeclass method to indicate that the property value or
/// method return value can be cached.
///
/// You can apply it to an instance property if the property
/// value doesn't change for the lifetime of its object instance.
/// You can apply it to a static property if the property value
/// doesn't change for the lifetime of the process.
/// You can apply it to an instance method if when the method is called
/// with the same parameters it always returns the same value for the
/// lifetime of its object instance.
/// You can apply it to a static method if when the method is called
/// with the same parameters it always returns the same value for the
/// lifetime of the process.
///
/// When an object is projected into JavaScript via
/// `CoreWebView2.AddHostObjectToScript`, WebView2 will cache property values
/// marked with this attribute. This can potentially improve performance by
/// reducing the number of cross-process calls to obtain the latest value.
[attributeusage(target_property, target_method)]
[attributename("cacheable")]
attribute CacheableAttribute
{
}
}
```

# Appendix

Names considered for the attribute:
* **Cacheable**: A familiar term also used by python that more closely matches this feature.
* **ReadOnly**: Similar to C#'s readonly keyword which indicates a value won't change (once
initialized). But does not convey that the implementer cannot change the value.
* **Immutable**: Similar to readonly
* **Const**: Does a better job indicating that the value does not change even by the
implementer.
* **Memoizable**: A broader term than cacheable that also applies to methods but more specific
than cacheable in that it better defines the kind of caching.

For the sample code, the only code you have to write is applying the attribute to the
property. The only effect this has is to potentially improve performance so there's no other
code to demonstrate anything. Accordingly, not sure what else to do in the sample code other
than the MIDL3.