mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 18:22:01 +00:00
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
67 lines
3.3 KiB
C#
67 lines
3.3 KiB
C#
namespace AIStudio.Tools.DocumentSegments;
|
|
|
|
/// <summary>
|
|
/// How large the text of a document may be for a format which holds it in memory, as the memory of this computer
|
|
/// allows, see IDocumentSegmentFormat.MaxTextSize.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// A format takes many times the size of the text of a document while reading and writing it, as bytes, as text, as
|
|
/// the tree of its parser or the DOM of the Open XML SDK, and for the new words: its memory factor, measured with
|
|
/// dense documents of many short paragraphs. The document may take a quarter of the memory of the computer; the rest
|
|
/// belongs to the system, to other programs, to AI Studio itself, and maybe to a local model. A Raspberry Pi with
|
|
/// 4 GB thus edits a web page of up to 11 MB, a computer with 16 GB one of up to 45 MB.
|
|
/// </para>
|
|
/// <para>
|
|
/// The limit is 512 MB on any computer, since .NET holds no string with more than about a billion characters, and at
|
|
/// least 1 MB, so that a message never names a limit of 0 MB.
|
|
/// </para>
|
|
/// </remarks>
|
|
public static class DocumentMemoryBudget
|
|
{
|
|
/// <summary>
|
|
/// The size of the largest text of a document on any computer, in bytes.
|
|
/// </summary>
|
|
public const long LARGEST_TEXT_SIZE = 512 * BYTES_PER_MEGABYTE;
|
|
|
|
/// <summary>
|
|
/// The number of bytes in a megabyte, in which the limit is counted and named.
|
|
/// </summary>
|
|
public const long BYTES_PER_MEGABYTE = 1024 * 1024;
|
|
|
|
/// <summary>
|
|
/// The part of the memory a document may take: a quarter.
|
|
/// </summary>
|
|
private const int SHARE_OF_MEMORY = 4;
|
|
|
|
/// <summary>
|
|
/// The memory to assume when .NET does not know it: as much as a small Raspberry Pi has.
|
|
/// </summary>
|
|
private const long ASSUMED_MEMORY = 4L * 1024 * BYTES_PER_MEGABYTE;
|
|
|
|
/// <summary>
|
|
/// Gets the size of the largest text of a document of a format on this computer.
|
|
/// </summary>
|
|
/// <param name="memoryFactor">How many times the size of its text a document of the format takes in memory.</param>
|
|
/// <returns>The size in bytes, in whole megabytes.</returns>
|
|
public static long MaxTextSize(int memoryFactor) => MaxTextSize(memoryFactor, GC.GetGCMemoryInfo().TotalAvailableMemoryBytes);
|
|
|
|
/// <summary>
|
|
/// Gets the size of the largest text of a document of a format on a computer with the given memory.
|
|
/// </summary>
|
|
/// <param name="memoryFactor">How many times the size of its text a document of the format takes in memory.</param>
|
|
/// <param name="totalMemory">The memory of the computer, or the limit of its container, in bytes; 0 when unknown.</param>
|
|
/// <returns>The size in bytes, in whole megabytes.</returns>
|
|
public static long MaxTextSize(int memoryFactor, long totalMemory)
|
|
{
|
|
var memory = totalMemory > 0 ? totalMemory : ASSUMED_MEMORY;
|
|
var megabytes = memory / SHARE_OF_MEMORY / memoryFactor / BYTES_PER_MEGABYTE;
|
|
return Math.Clamp(megabytes * BYTES_PER_MEGABYTE, BYTES_PER_MEGABYTE, LARGEST_TEXT_SIZE);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a size in whole megabytes, rounded up, so that a text just above a limit never reads as large as the limit.
|
|
/// </summary>
|
|
/// <param name="bytes">The size in bytes.</param>
|
|
public static long MegabytesOf(long bytes) => (bytes + BYTES_PER_MEGABYTE - 1) / BYTES_PER_MEGABYTE;
|
|
} |