Skip to content

Commit 71de5e4

Browse files
authored
Implement -hash and -copy of GDLLogEvent, copy on log, and don't assign extensionBytes in log writer (#2204)
* Implement -hash and -copy of GDLLogEvent Also implements a custom setter for setting the extension that changes the default behavior to set extensionBytes upon assignment of extension. Copy the log upon logging, as the comments promised. Remove setting extensionBytes in the log writer. Implement a missing method * Copy the log object upon logging * Don't assign extensionBytes in the log writer * Make an implicit loss of precision explicit. * Add a comment on performance
1 parent e90eb78 commit 71de5e4

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogEvent.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ - (instancetype)initWithLogMapID:(NSString *)logMapID logTarget:(NSInteger)logTa
3131
return self;
3232
}
3333

34+
- (instancetype)copy {
35+
GDLLogEvent *copy = [[GDLLogEvent alloc] initWithLogMapID:_logMapID logTarget:_logTarget];
36+
copy.extension = _extension;
37+
copy.extensionBytes = _extensionBytes;
38+
copy.qosTier = _qosTier;
39+
copy.clockSnapshot = _clockSnapshot;
40+
return copy;
41+
}
42+
43+
- (NSUInteger)hash {
44+
// This loses some precision, but it's probably fine.
45+
NSUInteger timeHash = (NSUInteger)(_clockSnapshot.timeMillis ^ _clockSnapshot.uptimeMillis);
46+
return [_logMapID hash] ^ _logTarget ^ [_extensionBytes hash] ^ _qosTier ^ timeHash;
47+
}
48+
49+
- (void)setExtension:(id<GDLLogProto>)extension {
50+
// If you're looking here because of a performance issue in -protoBytes slowing the assignment
51+
// of extension, one way to address this is to add a queue to this class,
52+
// dispatch_(barrier_ if concurrent)async here, and implement the getter with a dispatch_sync.
53+
if (extension != _extension) {
54+
_extension = extension;
55+
_extensionBytes = [extension protoBytes];
56+
}
57+
}
58+
3459
#pragma mark - NSSecureCoding and NSCoding Protocols
3560

3661
/** NSCoding key for logMapID property. */

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogWriter.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ - (void)writeLog:(GDLLogEvent *)log
6161
return;
6262
}
6363
}
64-
65-
// Convert the extension to bytes after transforming.
66-
NSAssert([log.extension respondsToSelector:@selector(protoBytes)],
67-
@"The log event's extension must respond to -protoBytes.");
68-
if ([log.extension respondsToSelector:@selector(protoBytes)]) {
69-
log.extensionBytes = [log.extension protoBytes];
70-
log.extension = nil;
71-
} else {
72-
GDLLogWarning(GDLMCWExtensionMissingBytesImpl, @"%@",
73-
@"The log event's extension must respond to -protoBytes.");
74-
}
7564
// TODO(mikehaney24): [[GDLLogStorage sharedInstance] storeLog:transformedLog];
7665
});
7766
}

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogger.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ - (instancetype)initWithLogMapID:(NSString *)logMapID
5050

5151
- (void)logTelemetryEvent:(GDLLogEvent *)logEvent {
5252
NSAssert(logEvent, @"You can't log a nil event");
53-
[[GDLLogWriter sharedInstance] writeLog:logEvent afterApplyingTransformers:_logTransformers];
53+
GDLLogEvent *copiedLog = [logEvent copy];
54+
[[GDLLogWriter sharedInstance] writeLog:copiedLog afterApplyingTransformers:_logTransformers];
5455
}
5556

5657
- (void)logDataEvent:(GDLLogEvent *)logEvent {
5758
NSAssert(logEvent, @"You can't log a nil event");
58-
[[GDLLogWriter sharedInstance] writeLog:logEvent afterApplyingTransformers:_logTransformers];
59+
GDLLogEvent *copiedLog = [logEvent copy];
60+
[[GDLLogWriter sharedInstance] writeLog:copiedLog afterApplyingTransformers:_logTransformers];
5961
}
6062

6163
- (GDLLogEvent *)newEvent {

0 commit comments

Comments
 (0)