Where's the documentation for FastProxy? #681
-
| I could use some examples of basic usage. In particular, what counts as a fast object? In my custom solution in ClearScript 7.4, I had objects implement an interface, and V8SplitProxyManaged would check for that interface and take the fast path if found. In ClearScript 7.5, I see IV8FastHostObject and IV8FastHostObjectOperations. Which of my C# objects should implement which interface for ClearScript to take the fast path with them? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| Hi @AnsisMalins,
Sorry about that. We haven't had a chance to put a FastProxy tutorial together.
A fast host object is simply one that implements Here's a basic example. Consider this class: public class Foo { public long Value { get; set; } public double GetScaledValue(double scaleFactor) => Value * scaleFactor; }Here's a version of the same class with FastProxy support: public class FastFoo : V8FastHostObject<FastFoo> { public long Value { get; set; } public double GetScaledValue(double scaleFactor) => Value * scaleFactor; static FastFoo() { Configure(static configuration => { configuration.AddPropertyAccessors( nameof(Value), static (FastFoo self, in V8FastResult value) => value.Set(self.Value), static (FastFoo self, in V8FastArg value) => self.Value = value.GetInt64() ); configuration.AddMethodGetter( nameof(GetScaledValue), static (FastFoo self, in V8FastArgs args, in V8FastResult result) => result.Set(self.GetScaledValue(args.GetDouble(0))) ); }); } }Note the differences – derivation from the Here's a test program that uses using var engine = new V8ScriptEngine(); engine.AddHostType(typeof(Console)); engine.Script.foo = new Foo(); engine.Script.fastFoo = new FastFoo(); engine.Script.stopWatch = new Stopwatch(); engine.Execute(@" const iterations = 1000000; const scaleFactor = 1.0000123; function test(name, foo) { stopWatch.Restart(); for (let i = 0; i < iterations; ++i) { foo.Value += i & 1 ? i : -i; foo.Value = Math.round(foo.GetScaledValue(scaleFactor)); } stopWatch.Stop(); Console.WriteLine('{0}: final value = {1}; elapsed time = {2} ms', name, foo.Value, stopWatch.ElapsedMilliseconds); } test('normal', foo); test(' fast', fastFoo); ");On our test machine, this program produces the following sample output: Please let us know if you have additional questions about the FastProxy API. Good luck! |
Beta Was this translation helpful? Give feedback.
Hi @AnsisMalins,
Sorry about that. We haven't had a chance to put a FastProxy tutorial together.
A fast host object is simply one that implements
IV8FastHostObject. You could implement that interface yourself, but the API provides several helpers to simplify things while retaining flexibility.Here's a basic example. Consider this class:
Here's a version of the same class with FastProxy support: