Skip to content

Commit e2f137d

Browse files
committed
Adjust default minimum size hint; add doc comments
1 parent 56ba9db commit e2f137d

2 files changed

Lines changed: 58 additions & 16 deletions

File tree

src/Lucene.Net.Analysis.Common/Analysis/Util/OpenStringBuilder.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ IAppendable IAppendable.Append(ICharSequence value, int startIndex, int count)
425425
// See ArrayBufferWriter for an inspiration and reference implementation:
426426
// https://github.com/dotnet/runtime/blob/v10.0.6/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
427427

428+
/// <summary>
429+
/// Notifies <see cref="IBufferWriter{T}"/> that <paramref name="count"/> amount of data was written to the output <see cref="Span{T}"/>/<see cref="Memory{T}"/>
430+
/// </summary>
431+
/// <exception cref="ArgumentException">
432+
/// Thrown when <paramref name="count"/> is negative.
433+
/// </exception>
434+
/// <exception cref="InvalidOperationException">
435+
/// Thrown when attempting to advance past the end of the underlying buffer.
436+
/// </exception>
437+
/// <remarks>
438+
/// You must request a new buffer after calling Advance to continue writing more data and cannot write to a previously acquired buffer.
439+
/// </remarks>
428440
public void Advance(int count)
429441
{
430442
if (count < 0)
@@ -436,13 +448,41 @@ public void Advance(int count)
436448
m_len += count;
437449
}
438450

451+
/// <summary>
452+
/// Returns a <see cref="Memory{T}"/> to write to that is at least the requested length (specified by <paramref name="sizeHint"/>).
453+
/// If no <paramref name="sizeHint"/> is provided (or it's equal to <c>0</c>), some buffer of minimum length 16 is returned.
454+
/// </summary>
455+
/// <exception cref="ArgumentException">
456+
/// Thrown when <paramref name="sizeHint"/> is negative.
457+
/// </exception>
458+
/// <remarks>
459+
/// This will never return an empty <see cref="Memory{T}"/>.
460+
/// <para />
461+
/// There is no guarantee that successive calls will return the same buffer or the same-sized buffer.
462+
/// <para />
463+
/// You must request a new buffer after calling <see cref="Advance"/> to continue writing more data and cannot write to a previously acquired buffer.
464+
/// </remarks>
439465
public Memory<char> GetMemory(int sizeHint = 0)
440466
{
441467
CheckAndResizeBufferForBufferWriter(sizeHint);
442468
Debug.Assert(m_buf.Length > m_len);
443469
return m_buf.AsMemory(m_len);
444470
}
445471

472+
/// <summary>
473+
/// Returns a <see cref="Span{T}"/> to write to that is at least the requested length (specified by <paramref name="sizeHint"/>).
474+
/// If no <paramref name="sizeHint"/> is provided (or it's equal to <c>0</c>), some buffer of minimum length 16 is returned.
475+
/// </summary>
476+
/// <exception cref="ArgumentException">
477+
/// Thrown when <paramref name="sizeHint"/> is negative.
478+
/// </exception>
479+
/// <remarks>
480+
/// This will never return an empty <see cref="Span{T}"/>.
481+
/// <para />
482+
/// There is no guarantee that successive calls will return the same buffer or the same-sized buffer.
483+
/// <para />
484+
/// You must request a new buffer after calling <see cref="Advance"/> to continue writing more data and cannot write to a previously acquired buffer.
485+
/// </remarks>
446486
public Span<char> GetSpan(int sizeHint = 0)
447487
{
448488
CheckAndResizeBufferForBufferWriter(sizeHint);
@@ -459,7 +499,12 @@ private void CheckAndResizeBufferForBufferWriter(int sizeHint)
459499

460500
if (sizeHint == 0)
461501
{
462-
sizeHint = 1;
502+
// LUCENENET NOTE: 16 is a reasonable, arbitrary minimum for text.
503+
// ArrayBufferWriter uses 1, just to ensure that it does not return
504+
// an empty buffer. If the caller does not care what size they get
505+
// back (by not providing a sizeHint), they have to work with what
506+
// is returned. This value should be enough for most short strings.
507+
sizeHint = 16;
463508
}
464509

465510
EnsureCapacity(sizeHint);

src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -570,19 +570,15 @@ public void Advance(int count)
570570
/// <summary>
571571
/// Returns a <see cref="Memory{T}"/> to write to, starting at the current
572572
/// term length, that is at least <paramref name="sizeHint"/> characters in
573-
/// length. The buffer is grown if necessary; the returned memory is never
574-
/// empty.
573+
/// length (or 10, if not provided or <c>0</c>). The buffer is grown if necessary;
574+
/// the returned memory is never empty.
575575
/// <para/>
576576
/// LUCENENET specific - implements <see cref="System.Buffers.IBufferWriter{T}"/>.
577577
/// </summary>
578578
/// <param name="sizeHint">
579579
/// The minimum requested length of the returned <see cref="Memory{T}"/>.
580-
/// A value of <c>0</c> requests a non-empty buffer of unspecified size.
580+
/// A value of <c>0</c> requests a buffer of minimum length 10.
581581
/// </param>
582-
/// <returns>
583-
/// A <see cref="Memory{T}"/> of at least <paramref name="sizeHint"/>
584-
/// characters (and at least one character).
585-
/// </returns>
586582
/// <exception cref="ArgumentOutOfRangeException">
587583
/// <paramref name="sizeHint"/> is negative.
588584
/// </exception>
@@ -596,19 +592,15 @@ public Memory<char> GetMemory(int sizeHint = 0)
596592
/// <summary>
597593
/// Returns a <see cref="Span{T}"/> to write to, starting at the current
598594
/// term length, that is at least <paramref name="sizeHint"/> characters in
599-
/// length. The buffer is grown if necessary; the returned span is never
600-
/// empty.
595+
/// length (or 10, if not provided or <c>0</c>). The buffer is grown if necessary;
596+
/// the returned span is never empty.
601597
/// <para/>
602598
/// LUCENENET specific - implements <see cref="System.Buffers.IBufferWriter{T}"/>.
603599
/// </summary>
604600
/// <param name="sizeHint">
605601
/// The minimum requested length of the returned <see cref="Span{T}"/>.
606-
/// A value of <c>0</c> requests a non-empty buffer of unspecified size.
602+
/// A value of <c>0</c> requests a buffer of minimum length 10.
607603
/// </param>
608-
/// <returns>
609-
/// A <see cref="Span{T}"/> of at least <paramref name="sizeHint"/>
610-
/// characters (and at least one character).
611-
/// </returns>
612604
/// <exception cref="ArgumentOutOfRangeException">
613605
/// <paramref name="sizeHint"/> is negative.
614606
/// </exception>
@@ -628,7 +620,12 @@ private void CheckAndResizeBufferForBufferWriter(int sizeHint)
628620

629621
if (sizeHint == 0)
630622
{
631-
sizeHint = 1;
623+
// LUCENENET NOTE: 10 is a reasonable, arbitrary minimum for text.
624+
// ArrayBufferWriter uses 1, just to ensure that it does not return
625+
// an empty buffer. If the caller does not care what size they get
626+
// back (by not providing a sizeHint), they have to work with what
627+
// is returned. This value should be enough for most short strings.
628+
sizeHint = MIN_BUFFER_SIZE;
632629
}
633630

634631
_ = InternalResizeBuffer(termLength + sizeHint);

0 commit comments

Comments
 (0)