-
Notifications
You must be signed in to change notification settings - Fork 52
Performance improvement #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this change affect performance? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the performance improvement? : There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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