Skip to content

Commit

Permalink
Add schema cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Aug 25, 2021
1 parent 1d21139 commit 810e55a
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* `list.Filter(query, arguments)` will filter the list and return the filtered collection. It is roughly equivalent to `list.AsRealmQueryable().Filter(query, arguments)`.

The resulting queryable collection will behave identically to the results obtained by calling `realm.All<T>()`, i.e. it will emit notifications when it changes and automatically update itself. (Issue [#1499](https://github.com/realm/realm-dotnet/issues/1499))
* Added a cache for the Realm schema. This will speed up `Realm.GetInstance` invocations where `RealmConfiguration.ObjectClasses` is explicitly set. The speed gains will depend on the number and complexity of your model classes. A reference benchmark that tests a schema containing all valid Realm property types showed a 25% speed increase of Realm.GetInstance. (Issue [#2194](https://github.com/realm/realm-dotnet/issues/2194))

### Compatibility
* Realm Studio: 11.0.0 or later.
Expand Down
13 changes: 10 additions & 3 deletions Realm/Realm/Schema/ObjectSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace Realms.Schema
[DebuggerDisplay("Name = {Name}, Properties = {Count}")]
public class ObjectSchema : IReadOnlyCollection<Property>
{
private static readonly IDictionary<TypeInfo, ObjectSchema> _cache = new Dictionary<TypeInfo, ObjectSchema>();

private readonly ReadOnlyDictionary<string, Property> _properties;

/// <summary>
Expand Down Expand Up @@ -109,6 +111,10 @@ private ObjectSchema(string name, IDictionary<string, Property> properties)
public static ObjectSchema FromType(TypeInfo type)
{
Argument.NotNull(type, nameof(type));
if (_cache.TryGetValue(type, out var result))
{
return result;
}

Argument.Ensure(type.IsRealmObject() || type.IsEmbeddedObject(), $"The class {type.FullName} must descend directly from RealmObject", nameof(type));

Expand Down Expand Up @@ -148,9 +154,10 @@ public static ObjectSchema FromType(TypeInfo type)
builder.Add(schemaProperty);
}

var ret = builder.Build();
ret.Type = type;
return ret;
result = builder.Build();
result.Type = type;
_cache[type] = result;
return result;
}

internal class Builder : List<Property>
Expand Down
2 changes: 1 addition & 1 deletion Tests/Benchmarks/PerformanceTests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Realm .NET SDK uses [Benchmark.NET](http://benchmarkdotnet.org) to run synth

## Running the benchmarks

To run all tests, use `dotnet run -c Release -f net5.0 -- -f *`. To run only a subset of the tests, replace `*` with a regex that will match the tests you want to run. To list all benchmarks, use `dotnet run -c Release -f net5.0 -- --list`. Refere to [the docs](https://benchmarkdotnet.org/articles/guides/console-args.html) for a complete list of the supported arguments.
To run all tests, use `dotnet run -c Release -f net5.0 -- -f *`. To run only a subset of the tests, replace `*` with a regex that will match the tests you want to run. To list all benchmarks, use `dotnet run -c Release -f net5.0 -- --list`. Refer to [the docs](https://benchmarkdotnet.org/articles/guides/console-args.html) for a complete list of the supported arguments.

## Best practices

Expand Down
Loading

0 comments on commit 810e55a

Please sign in to comment.