Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Hazelcast.Examples/Org.Website.Samples/GlobalSerializerSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Hazelcast.Client;
using Hazelcast.Config;
using Hazelcast.IO;
Expand All @@ -30,15 +32,19 @@ public void Destroy()
{
}

public void Write(IObjectDataOutput output, object obj)
public object Read(IObjectDataInput input)
{
// out.write(MyFavoriteSerializer.serialize(obj))
var formatter = new BinaryFormatter();
var stream = new MemoryStream(input.ReadByteArray());
return formatter.Deserialize(stream);
}

public object Read(IObjectDataInput input)
public void Write(IObjectDataOutput output, object obj)
{
// return MyFavoriteSerializer.deserialize(input);
return null;
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, obj);
output.WriteByteArray(stream.GetBuffer());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Concurrent;
using System.Collections.Generic;
using Hazelcast.Client.Protocol.Util;
using Hazelcast.IO.Serialization;
Expand Down Expand Up @@ -41,25 +42,16 @@ internal static ClientMessage EncodeRequest(string name, IData predicate)
return clientMessage;
}

internal class ResponseParameters
internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue<KeyValuePair<IData, object>> result)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the optimization using a concrete implementation instead of the interface? Can you share the performance numbers how this improves performance?

What about elimination of the class ResponseParameters class with the static method?
I see that only 5 codecs are changed, should this be applied to all the other codecs as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a IMap optimization that we can backport in 4.0 series to all codecs

{
public IList<KeyValuePair<IData, IData>> response;
}

internal static ResponseParameters DecodeResponse(IClientMessage clientMessage)
{
var parameters = new ResponseParameters();
var responseSize = clientMessage.GetInt();
var response = new List<KeyValuePair<IData, IData>>(responseSize);
for (var responseIndex = 0; responseIndex < responseSize; responseIndex++)
{
var responseItemKey = clientMessage.GetData();
var responseItemVal = clientMessage.GetData();
var responseItem = new KeyValuePair<IData, IData>(responseItemKey, responseItemVal);
response.Add(responseItem);
var responseItem = new KeyValuePair<IData, object>(responseItemKey, responseItemVal);
result.Enqueue(responseItem);
}
parameters.response = response;
return parameters;
}
}
}
16 changes: 4 additions & 12 deletions Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntrySetCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Concurrent;
using System.Collections.Generic;
using Hazelcast.Client.Protocol.Util;
using Hazelcast.IO.Serialization;
Expand Down Expand Up @@ -39,25 +40,16 @@ internal static ClientMessage EncodeRequest(string name)
return clientMessage;
}

internal class ResponseParameters
internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue<KeyValuePair<IData, object>> result)
{
public IList<KeyValuePair<IData, IData>> response;
}

internal static ResponseParameters DecodeResponse(IClientMessage clientMessage)
{
var parameters = new ResponseParameters();
var responseSize = clientMessage.GetInt();
var response = new List<KeyValuePair<IData, IData>>(responseSize);
for (var responseIndex = 0; responseIndex < responseSize; responseIndex++)
{
var responseItemKey = clientMessage.GetData();
var responseItemVal = clientMessage.GetData();
var responseItem = new KeyValuePair<IData, IData>(responseItemKey, responseItemVal);
response.Add(responseItem);
var responseItem = new KeyValuePair<IData, object>(responseItemKey, responseItemVal);
result.Enqueue(responseItem);
}
parameters.response = response;
return parameters;
}
}
}
31 changes: 13 additions & 18 deletions Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapGetAllCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Hazelcast.Client.Protocol.Util;
using Hazelcast.IO;
Expand All @@ -22,53 +25,45 @@ namespace Hazelcast.Client.Protocol.Codec
{
internal static class MapGetAllCodec
{
private static int CalculateRequestDataSize(string name, IList<IData> keys)
private static int CalculateRequestDataSize(string name, ArrayList keys)
{
var dataSize = ClientMessage.HeaderSize;
dataSize += ParameterUtil.CalculateDataSize(name);
dataSize += Bits.IntSizeInBytes;
foreach (var keysItem in keys)
for (int i = 0; i < keys.Count; i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this change affect performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on .Net foreach is slower than for loop

{
dataSize += ParameterUtil.CalculateDataSize(keysItem);
dataSize += ParameterUtil.CalculateDataSize((IData)keys[i]);
}
return dataSize;
}

internal static ClientMessage EncodeRequest(string name, IList<IData> keys)
internal static ClientMessage EncodeRequest(string name, ArrayList keys)
{
var requiredDataSize = CalculateRequestDataSize(name, keys);
var clientMessage = ClientMessage.CreateForEncode(requiredDataSize);
clientMessage.SetMessageType((int) MapMessageType.MapGetAll);
clientMessage.SetRetryable(false);
clientMessage.Set(name);
clientMessage.Set(keys.Count);
foreach (var keysItem in keys)
for (int i = 0; i < keys.Count; i++)
{
clientMessage.Set(keysItem);
clientMessage.Set((IData)keys[i]);
}
clientMessage.UpdateFrameLength();
return clientMessage;
}

internal class ResponseParameters
internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue<KeyValuePair<IData, object>> result)
{
public IList<KeyValuePair<IData, IData>> response;
}

internal static ResponseParameters DecodeResponse(IClientMessage clientMessage)
{
var parameters = new ResponseParameters();
var responseSize = clientMessage.GetInt();
var response = new List<KeyValuePair<IData, IData>>(responseSize);
for (var responseIndex = 0; responseIndex < responseSize; responseIndex++)
{
var responseItemKey = clientMessage.GetData();
var responseItemVal = clientMessage.GetData();
var responseItem = new KeyValuePair<IData, IData>(responseItemKey, responseItemVal);
response.Add(responseItem);

var responseItem = new KeyValuePair<IData, object>(responseItemKey, responseItemVal);
result.Enqueue(responseItem);
}
parameters.response = response;
return parameters;
}
}
}
21 changes: 9 additions & 12 deletions Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapPutAllCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Hazelcast.Client.Protocol.Util;
using Hazelcast.IO;
Expand All @@ -22,35 +24,30 @@ namespace Hazelcast.Client.Protocol.Codec
{
internal static class MapPutAllCodec
{
private static int CalculateRequestDataSize(string name, IList<KeyValuePair<IData, IData>> entries)
private static int CalculateRequestDataSize(string name, ArrayList entries)
{
var dataSize = ClientMessage.HeaderSize;
dataSize += ParameterUtil.CalculateDataSize(name);
dataSize += Bits.IntSizeInBytes;
foreach (var entriesItem in entries)
{
var entriesItemKey = entriesItem.Key;
var entriesItemVal = entriesItem.Value;
dataSize += ParameterUtil.CalculateDataSize(entriesItemKey);
dataSize += ParameterUtil.CalculateDataSize(entriesItemVal);
dataSize += ParameterUtil.CalculateDataSize((IData)entriesItem);
}
return dataSize;
}

internal static ClientMessage EncodeRequest(string name, IList<KeyValuePair<IData, IData>> entries)
internal static ClientMessage EncodeRequest(string name, ArrayList entries)
{
var requiredDataSize = CalculateRequestDataSize(name, entries);
var clientMessage = ClientMessage.CreateForEncode(requiredDataSize);
clientMessage.SetMessageType((int) MapMessageType.MapPutAll);
clientMessage.SetRetryable(false);
clientMessage.Set(name);
clientMessage.Set(entries.Count);
foreach (var entriesItem in entries)
clientMessage.Set(entries.Count / 2);
for (int i = 0; i < entries.Count; i+=2)
{
var entriesItemKey = entriesItem.Key;
var entriesItemVal = entriesItem.Value;
clientMessage.Set(entriesItemKey);
clientMessage.Set(entriesItemVal);
clientMessage.Set((IData)entries[i]);
clientMessage.Set((IData)entries[i+1]);
}
clientMessage.UpdateFrameLength();
return clientMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal static ClientMessage EncodeRequest(string name, IData predicate)

internal class ResponseParameters
{
public IList<IData> response;
public List<IData> response;
}

internal static ResponseParameters DecodeResponse(IClientMessage clientMessage)
Expand Down
4 changes: 2 additions & 2 deletions Hazelcast.Net/Hazelcast.Client.Protocol.Util/BufferBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public virtual BufferBuilder Append(IClientProtocolBuffer srcBuffer, int srcOffs
/// <see cref="IClientProtocolBuffer" />
/// that encapsulates the internal buffer.
/// </returns>
public virtual IClientProtocolBuffer Buffer()
public virtual IClientProtocolBuffer ProtocolBuffer()
{
return _protocolBuffer;
}
Expand Down Expand Up @@ -100,7 +100,7 @@ private void EnsureCapacity(int additionalCapacity)
{
var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity);
var newBuffer = new byte[newCapacity];
Array.Copy(_protocolBuffer.ByteArray(), 0, newBuffer, 0, _capacity);
Buffer.BlockCopy(_protocolBuffer.ByteArray(), 0, newBuffer, 0, _capacity);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the performance improvement? :
Array.Copy iterates each element while Buffer.BlockCopy performs block of memcpy? So, can we assume that we always need to prefer memcpy to iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.BlockCopy is faster than Array.Copy. both are native calls and I'm not sure of the reason. I can measure improvement.

_capacity = newCapacity;
_protocolBuffer.Wrap(newBuffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public virtual void OnData(ByteBuffer buffer)
if (_message.IsFlagSet(ClientMessage.EndFlag))
{
var msgLength = builder.Position();
var cm = ClientMessage.CreateForDecode(builder.Buffer(), 0);
var cm = ClientMessage.CreateForDecode(builder.ProtocolBuffer(), 0);
cm.SetFrameLength(msgLength);
//HANDLE-MESSAGE
HandleMessage(cm);
Expand Down
Loading