Skip to content

Commit

Permalink
Reduce memory allocations (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
sierpinskid authored Apr 12, 2024
1 parent 057ad24 commit 23204df
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
18 changes: 18 additions & 0 deletions Assets/Plugins/StreamChat/Core/Helpers/ICollectionExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ namespace StreamChat.Core.Helpers
/// </summary>
internal static class ICollectionExt
{

/// <summary>
/// In Unity 2019.4.40f1 List.Contains allocates memory. Use this allocation free alternative
/// </summary>
[Pure]
public static bool ContainsNoAlloc<TItem>(this List<TItem> source, TItem item)
{
for (var i = 0; i < source.Count; i++)
{
if (EqualityComparer<TItem>.Default.Equals(source[i], item))
{
return true;
}
}

return false;
}

[Pure]
public static List<TDto> TrySaveToDtoCollection<TSource, TDto>(this List<TSource> source)
where TSource : ISavableTo<TDto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ internal partial class APIErrorInternalDTO
[Newtonsoft.Json.JsonProperty("more_info", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string MoreInfo { get; set; }

private System.Collections.Generic.Dictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.Dictionary<string, object> AdditionalProperties
{
get { return _additionalProperties; }
set { _additionalProperties = value; }
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ APIError ILoadableFrom<APIErrorInternalDTO, APIError>.LoadFromDto(APIErrorIntern
ExceptionFields = dto.ExceptionFields;
Message = dto.Message;
MoreInfo = dto.MoreInfo;
AdditionalProperties = dto.AdditionalProperties;

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,11 @@ private void HandleNewWebsocketMessage(string msg)
return;
}

var time = DateTime.Now.TimeOfDay.ToString(@"hh\:mm\:ss");
EventReceived?.Invoke($"{time} - Event received: <b>{type}</b>");
if (EventReceived != null)
{
var time = DateTime.Now.TimeOfDay.ToString(@"hh\:mm\:ss");
EventReceived.Invoke($"{time} - Event received: <b>{type}</b>");
}

if (!_eventKeyToHandler.TryGetValue(type, out var handler))
{
Expand Down
15 changes: 9 additions & 6 deletions Assets/Plugins/StreamChat/Core/StatefulModels/StreamChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public async Task<IStreamMessage> SendNewMessageAsync(StreamSendMessageRequest s

var response = await LowLevelClient.InternalMessageApi.SendNewMessageAsync(Type, Id,
sendMessageRequest.TrySaveToDto());

//StreamTodo: we update internal cache message without server confirmation that message got accepted. e.g. message could be rejected
//It's ok to update the cache "in good faith" to not introduce update delay but we should handle if message got rejected
var streamMessage = InternalAppendOrUpdateMessage(response.Message);
return streamMessage;
}
Expand Down Expand Up @@ -757,7 +760,7 @@ internal void HandleChannelTruncatedEvent(NotificationChannelTruncatedEventInter

internal void InternalAddMember(StreamChannelMember member)
{
if (_members.Contains(member))
if (_members.ContainsNoAlloc(member))
{
return;
}
Expand All @@ -769,7 +772,7 @@ internal void InternalAddMember(StreamChannelMember member)

internal void InternalRemoveMember(StreamChannelMember member)
{
if (!_members.Contains(member))
if (!_members.ContainsNoAlloc(member))
{
return;
}
Expand All @@ -781,7 +784,7 @@ internal void InternalRemoveMember(StreamChannelMember member)

internal void InternalUpdateMember(StreamChannelMember member)
{
if (!_members.Contains(member))
if (!_members.ContainsNoAlloc(member))
{
_members.Add(member);
}
Expand Down Expand Up @@ -822,7 +825,7 @@ private StreamMessage InternalAppendOrUpdateMessage(MessageInternalDTO dto)
var streamMessage = Cache.TryCreateOrUpdate(dto, out var wasCreated);
if (wasCreated)
{
if (!_messages.Contains(streamMessage))
if (!_messages.ContainsNoAlloc(streamMessage))
{
_messages.Add(streamMessage);
MessageReceived?.Invoke(this, streamMessage);
Expand Down Expand Up @@ -959,7 +962,7 @@ internal void InternalHandleUserWatchingStartEvent(UserWatchingStartEventInterna
AssertCid(eventDto.Cid);

var user = Cache.TryCreateOrUpdate(eventDto.User, out var wasCreated);
if (wasCreated || !_watchers.Contains(user))
if (wasCreated || !_watchers.ContainsNoAlloc(user))
{
WatcherCount += 1;
_watchers.Add(user);
Expand Down Expand Up @@ -1010,7 +1013,7 @@ internal void InternalHandleTypingStarted(TypingStartEventInternalDTO eventDto)
var user = Cache.TryCreateOrUpdate(eventDto.User);
StreamAsserts.AssertNotNull(user, nameof(user));

if (!_typingUsers.Contains(user))
if (!_typingUsers.ContainsNoAlloc(user))
{
_typingUsers.Add(user);
UserStartedTyping?.Invoke(this, user);
Expand Down
1 change: 1 addition & 0 deletions Assets/Plugins/StreamChat/Core/WSEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ internal static class WSEventType
public const string NotificationRemovedFromChannel = "notification.removed_from_channel";
public const string NotificationMutesUpdated = "notification.mutes_updated";
public const string NotificationChannelMutesUpdated = "notification.channel_mutes_updated";
//StreamTodo: implement NOTIFICATION.MARK_UNREAD
}
}

0 comments on commit 23204df

Please sign in to comment.