Skip to content

Commit 2894c3d

Browse files
committed
CSHARP-1972: Optimize BsonUtils.ToHexString and ObjectId.ToString.
1 parent f309626 commit 2894c3d

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

src/MongoDB.Bson/BsonUtils.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2016 MongoDB Inc.
1+
/* Copyright 2010-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ namespace MongoDB.Bson
2626
/// </summary>
2727
public static class BsonUtils
2828
{
29-
3029
// public static methods
3130
/// <summary>
3231
/// Gets a friendly class name suitable for use in error messages.
@@ -120,6 +119,16 @@ public static DateTime ToDateTimeFromMillisecondsSinceEpoch(long millisecondsSin
120119
}
121120
}
122121

122+
/// <summary>
123+
/// Converts a value to a hex character.
124+
/// </summary>
125+
/// <param name="value">The value (assumed to be between 0 and 15).</param>
126+
/// <returns>The hex character.</returns>
127+
public static char ToHexChar(int value)
128+
{
129+
return (char)(value + (value < 10 ? '0' : 'a' - 10));
130+
}
131+
123132
/// <summary>
124133
/// Converts a byte array to a hex string.
125134
/// </summary>
@@ -131,12 +140,18 @@ public static string ToHexString(byte[] bytes)
131140
{
132141
throw new ArgumentNullException("bytes");
133142
}
134-
var sb = new StringBuilder(bytes.Length * 2);
135-
foreach (var b in bytes)
143+
144+
var length = bytes.Length;
145+
var c = new char[length * 2];
146+
147+
for (int i = 0, j = 0; i < length; i++)
136148
{
137-
sb.AppendFormat("{0:x2}", b);
149+
var b = bytes[i];
150+
c[j++] = ToHexChar(b >> 4);
151+
c[j++] = ToHexChar(b & 0x0f);
138152
}
139-
return sb.ToString();
153+
154+
return new string(c);
140155
}
141156

142157
/// <summary>

src/MongoDB.Bson/IO/JsonWriter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2016 MongoDB Inc.
1+
/* Copyright 2010-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -491,18 +491,16 @@ public override void WriteObjectId(ObjectId objectId)
491491
ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
492492
}
493493

494-
var bytes = objectId.ToByteArray();
495-
496494
WriteNameHelper(Name);
497495
switch (_jsonWriterSettings.OutputMode)
498496
{
499497
case JsonOutputMode.Strict:
500-
_textWriter.Write("{{ \"$oid\" : \"{0}\" }}", BsonUtils.ToHexString(bytes));
498+
_textWriter.Write("{{ \"$oid\" : \"{0}\" }}", objectId.ToString());
501499
break;
502500

503501
case JsonOutputMode.Shell:
504502
default:
505-
_textWriter.Write("ObjectId(\"{0}\")", BsonUtils.ToHexString(bytes));
503+
_textWriter.Write("ObjectId(\"{0}\")", objectId.ToString());
506504
break;
507505
}
508506

src/MongoDB.Bson/ObjectModel/ObjectId.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2016 MongoDB Inc.
1+
/* Copyright 2010-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -541,7 +541,32 @@ public void ToByteArray(byte[] destination, int offset)
541541
/// <returns>A string representation of the value.</returns>
542542
public override string ToString()
543543
{
544-
return BsonUtils.ToHexString(ToByteArray());
544+
var c = new char[24];
545+
c[0] = BsonUtils.ToHexChar((_a >> 28) & 0x0f);
546+
c[1] = BsonUtils.ToHexChar((_a >> 24) & 0x0f);
547+
c[2] = BsonUtils.ToHexChar((_a >> 20) & 0x0f);
548+
c[3] = BsonUtils.ToHexChar((_a >> 16) & 0x0f);
549+
c[4] = BsonUtils.ToHexChar((_a >> 12) & 0x0f);
550+
c[5] = BsonUtils.ToHexChar((_a >> 8) & 0x0f);
551+
c[6] = BsonUtils.ToHexChar((_a >> 4) & 0x0f);
552+
c[7] = BsonUtils.ToHexChar(_a & 0x0f);
553+
c[8] = BsonUtils.ToHexChar((_b >> 28) & 0x0f);
554+
c[9] = BsonUtils.ToHexChar((_b >> 24) & 0x0f);
555+
c[10] = BsonUtils.ToHexChar((_b >> 20) & 0x0f);
556+
c[11] = BsonUtils.ToHexChar((_b >> 16) & 0x0f);
557+
c[12] = BsonUtils.ToHexChar((_b >> 12) & 0x0f);
558+
c[13] = BsonUtils.ToHexChar((_b >> 8) & 0x0f);
559+
c[14] = BsonUtils.ToHexChar((_b >> 4) & 0x0f);
560+
c[15] = BsonUtils.ToHexChar(_b & 0x0f);
561+
c[16] = BsonUtils.ToHexChar((_c >> 28) & 0x0f);
562+
c[17] = BsonUtils.ToHexChar((_c >> 24) & 0x0f);
563+
c[18] = BsonUtils.ToHexChar((_c >> 20) & 0x0f);
564+
c[19] = BsonUtils.ToHexChar((_c >> 16) & 0x0f);
565+
c[20] = BsonUtils.ToHexChar((_c >> 12) & 0x0f);
566+
c[21] = BsonUtils.ToHexChar((_c >> 8) & 0x0f);
567+
c[22] = BsonUtils.ToHexChar((_c >> 4) & 0x0f);
568+
c[23] = BsonUtils.ToHexChar(_c & 0x0f);
569+
return new string(c);
545570
}
546571

547572
// explicit IConvertible implementation

src/MongoDB.Bson/Serialization/Serializers/ByteArraySerializer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -124,12 +124,7 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
124124
break;
125125

126126
case BsonType.String:
127-
var sb = new StringBuilder(value.Length * 2);
128-
for (int i = 0; i < value.Length; i++)
129-
{
130-
sb.Append(string.Format("{0:x2}", value[i]));
131-
}
132-
bsonWriter.WriteString(sb.ToString());
127+
bsonWriter.WriteString(BsonUtils.ToHexString(value));
133128
break;
134129

135130
default:

0 commit comments

Comments
 (0)