Skip to content

Commit

Permalink
Avoid deep copying the schema in observe(on:)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Jun 18, 2024
1 parent aacc8c3 commit a6720bc
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Realm/RLMCollection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ - (bool)invalidate {
}

RLMThreadSafeReference *tsr = [RLMThreadSafeReference referenceWithThreadConfined:collection];
RLMRealmConfiguration *config = realm.configuration;
RLMRealmConfiguration *config = realm.configurationSharingSchema;
dispatch_async(queue, ^{
std::lock_guard lock(token->_mutex);
if (!token->_realm) {
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMObjectBase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ - (void)registrationComplete:(void (^)())completion {
RLMThreadSafeReference *tsr = [RLMThreadSafeReference referenceWithThreadConfined:(id)obj];
auto token = [[RLMObjectNotificationToken alloc] init];
token->_realm = obj->_realm;
RLMRealmConfiguration *config = obj->_realm.configuration;
RLMRealmConfiguration *config = obj->_realm.configurationSharingSchema;
dispatch_async(queue, ^{
@autoreleasepool {
[token addNotificationBlock:block threadSafeReference:tsr config:config keyPaths:keyPaths queue:queue];
Expand Down
10 changes: 9 additions & 1 deletion Realm/RLMRealm.mm
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ - (RLMRealmConfiguration *)configuration {
return configuration;
}

- (RLMRealmConfiguration *)configurationSharingSchema {
RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
configuration.configRef = _realm->config();
configuration.dynamic = _dynamic;
[configuration setCustomSchemaWithoutCopying:_schema];
return configuration;
}

- (void)beginWriteTransaction {
[self beginWriteTransactionWithError:nil];
}
Expand Down Expand Up @@ -1076,7 +1084,7 @@ - (RLMRealm *)freeze {

- (RLMRealm *)thaw {
[self verifyThread];
return self.isFrozen ? [RLMRealm realmWithConfiguration:self.configuration error:nil] : self;
return self.isFrozen ? [RLMRealm realmWithConfiguration:self.configurationSharingSchema error:nil] : self;
}

- (RLMRealm *)frozenCopy {
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMRealmUtil.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ @implementation RLMPinnedRealm {
- (instancetype)initWithRealm:(RLMRealm *)realm {
if (self = [super init]) {
_pin = realm->_realm->duplicate();
_configuration = realm.configuration;
_configuration = realm.configurationSharingSchema;
}
return self;
}
Expand Down
5 changes: 5 additions & 0 deletions Realm/RLMRealm_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ FOUNDATION_EXTERN void RLMRealmSubscribeToAll(RLMRealm *);
@property (nonatomic, readonly, nullable) id actor;
@property (nonatomic, readonly) bool isFlexibleSync;

// `-configuration` does a deep copy of the schema as if the user mutates the
// RLMSchema in use by a RLMRealm things will break horribly. When we know that
// the configuration won't be exposed we can skip the copy.
- (RLMRealmConfiguration *)configurationSharingSchema NS_RETURNS_RETAINED;

+ (void)resetRealmState;

- (void)registerEnumerator:(RLMFastEnumerator *)enumerator;
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMResults.mm
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ - (void)completionWithThreadSafeReference:(RLMThreadSafeReference * _Nullable)re
confinedTo:(RLMScheduler *)confinement
completion:(RLMResultsCompletionBlock)completion
error:(NSError *_Nullable)error {
RLMRealmConfiguration *configuration = _realm.configuration;
RLMRealmConfiguration *configuration = _realm.configurationSharingSchema;
[confinement invoke:^{
if (error) {
return completion(nil, error);
Expand Down
6 changes: 3 additions & 3 deletions RealmSwift/Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ public enum RealmPublishers {
try? Realm(RLMRealm(configuration: config, queue: scheduler as? DispatchQueue))
}
static private func realm<S: Scheduler>(_ sourceRealm: Realm, _ scheduler: S) -> Realm? {
return realm(sourceRealm.rlmRealm.configuration, scheduler)
return realm(sourceRealm.rlmRealm.configurationSharingSchema(), scheduler)
}

/// A publisher which emits an asynchronously opened Realm.
Expand Down Expand Up @@ -1300,7 +1300,7 @@ public enum RealmPublishers {
private let scheduler: S

internal init(_ upstream: Upstream, _ scheduler: S, _ realm: Realm) {
self.config = realm.rlmRealm.configuration
self.config = realm.rlmRealm.configurationSharingSchema()
self.upstream = upstream
self.scheduler = scheduler
}
Expand Down Expand Up @@ -1377,7 +1377,7 @@ public enum RealmPublishers {
self.upstream
.map { (obj: Output) -> Handover in
guard let realm = obj.realm, !realm.isFrozen else { return .object(obj) }
return .tsr(ThreadSafeReference(to: obj), config: realm.rlmRealm.configuration)
return .tsr(ThreadSafeReference(to: obj), config: realm.rlmRealm.configurationSharingSchema())
}
.receive(on: scheduler)
.compactMap { (handover: Handover) -> Output? in
Expand Down
2 changes: 1 addition & 1 deletion RealmSwift/Impl/RealmCollectionImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal func with<A: Actor, Value: ThreadConfined, Return: Sendable>(
}

let tsr = ThreadSafeReference(to: value)
let config = Unchecked(wrappedValue: value.realm!.rlmRealm.configuration)
let config = Unchecked(wrappedValue: value.realm!.rlmRealm.configurationSharingSchema())
return try await actor.invoke { actor in
if Task.isCancelled {
return nil
Expand Down

0 comments on commit a6720bc

Please sign in to comment.