make more use of stream.CopyTo on (de)compressors, plus several cosmetics

This commit is contained in:
UbitUmarov
2025-06-19 20:11:42 +01:00
parent 6180fe5340
commit 4b1df46f6a
14 changed files with 47 additions and 66 deletions

View File

@@ -27,7 +27,6 @@
using System.Net;
using System.Reflection;
using System.Text;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
using OpenSim.Framework;

View File

@@ -324,7 +324,7 @@ namespace OpenSim.Data.MySQL
metadata.CreatorID = dbReader["CreatorID"].ToString();
// Current SHA1s are not stored/computed.
metadata.SHA1 = new byte[] { };
metadata.SHA1 = Array.Empty<byte>();
retList.Add(metadata);
}

View File

@@ -231,27 +231,20 @@ namespace OpenSim.Data.MySQL
asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
}
byte[] data;
if (m_enableCompression)
{
MemoryStream outputStream = new MemoryStream();
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
{
// Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue));
// We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream.
compressionStream.Close();
byte[] compressedData = outputStream.ToArray();
asset.Data = compressedData;
}
using MemoryStream inMs = new(asset.Data);
using MemoryStream outputStream = new();
using GZipStream compressionStream = new(outputStream, CompressionMode.Compress);
inMs.CopyTo(compressionStream);
compressionStream.Flush();
data = outputStream.ToArray();
}
else
data = asset.Data;
byte[] hash;
using (HashAlgorithm hasher = SHA256.Create())
hash = hasher.ComputeHash(asset.Data);
// m_log.DebugFormat(
// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}",
// asset.ID, asset.Name, hash, compressedData.Length);
byte[] hash = SHA256.HashData(data);
try
{
@@ -297,7 +290,7 @@ namespace OpenSim.Data.MySQL
dbcon))
{
cmd.Parameters.AddWithValue("?Hash", hash);
cmd.Parameters.AddWithValue("?Data", asset.Data);
cmd.Parameters.AddWithValue("?Data", data);
cmd.ExecuteNonQuery();
}
}

View File

@@ -78,9 +78,8 @@ namespace OpenSim.Framework.AssetLoader.Filesystem
if (fInfo.Exists)
{
FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] idata = new byte[numBytes];
BinaryReader br = new BinaryReader(fStream);
idata = br.ReadBytes((int)numBytes);
byte[] idata = br.ReadBytes((int)numBytes);
br.Close();
fStream.Close();
info.Data = idata;

View File

@@ -794,8 +794,8 @@ namespace OpenSim.Framework
public static byte[] ulongToByteArray(ulong uLongValue)
{
return new byte[8]
{
return
[
(byte)(uLongValue >> 56),
(byte)(uLongValue >> 48),
(byte)(uLongValue >> 40),
@@ -804,18 +804,18 @@ namespace OpenSim.Framework
(byte)(uLongValue >> 16),
(byte)(uLongValue >> 8),
(byte)uLongValue
};
];
}
public static byte[] uintToByteArray(uint value)
{
return new byte[4]
{
return
[
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
};
];
}
static readonly char[] base64Chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',

View File

@@ -119,7 +119,7 @@ namespace OpenSim.Framework
// Copy the temporary stream to the network stream
formDataStream.Seek(0, SeekOrigin.Begin);
using (Stream requestStream = request.GetRequestStream())
formDataStream.CopyStream(requestStream, (int)formDataStream.Length);
formDataStream.CopyTo(requestStream, (int)formDataStream.Length);
}
#endregion Stream Writing

View File

@@ -1508,7 +1508,7 @@ namespace OpenSim.Framework
return null;
if (RenderMaterials.overrides is null || RenderMaterials.overrides.Length == 0)
return new byte[] { 0 }; // store so outdated viewer caches can be updated
return [0]; // store so outdated viewer caches can be updated
int nentries = 0;
for (int i = 0; i < RenderMaterials.overrides.Length; i++)
@@ -1517,7 +1517,7 @@ namespace OpenSim.Framework
nentries++;
}
if(nentries == 0)
return new byte[] { 0 };
return [0];
osUTF8 sb = OSUTF8Cached.Acquire();
sb.Append((byte)nentries);

View File

@@ -630,37 +630,34 @@ namespace OpenSim.Framework
Array ret = null;
try
{
using (MemoryStream inp = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
using (MemoryStream inp = new MemoryStream((2 * sizeof(int)) + (SizeX * SizeY * sizeof(float))))
{
using (BinaryWriter bw = new BinaryWriter(inp))
{
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write(SizeX);
bw.Write(SizeY);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
//bw.Write((float)m_heightmap[xx, yy]);
bw.Write(MathF.Round(m_heightmap[xx, yy], 3, MidpointRounding.AwayFromZero));
}
bw.Flush();
inp.Seek(0, SeekOrigin.Begin);
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
{
inp.CopyStream(compressionStream, int.MaxValue);
compressionStream.Close();
ret = outputStream.ToArray();
}
}
using MemoryStream outputStream = new MemoryStream();
using GZipStream compressionStream = new(outputStream, CompressionMode.Compress);
inp.CopyTo(compressionStream);
compressionStream.Flush();
ret = outputStream.ToArray();
}
}
m_log.Debug($"{LogHeader} V2DGzip {ret.Length} bytes");
}
catch (Exception ex)
{
m_log.Error($"{LogHeader} V2DGzip error: {ex.Message}");
}
catch {}
m_log.DebugFormat("{0} V2DGzip {1} bytes", LogHeader, ret.Length);
return ret;
}
@@ -787,7 +784,6 @@ namespace OpenSim.Framework
{
using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
decompressionStream.Flush();
decompressionStream.CopyTo(outputStream);
}
}

View File

@@ -26,6 +26,7 @@
*/
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
@@ -804,18 +805,18 @@ namespace OpenSim.Framework
/// </remarks>
public static int CopyStream(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy)
{
byte[] buffer = new byte[4096];
byte[] buffer = ArrayPool<byte>.Shared.Rent(8196);
int readBytes;
int totalCopiedBytes = 0;
while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(4096, maximumBytesToCopy))) > 0)
while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(8196, maximumBytesToCopy))) > 0)
{
int writeBytes = Math.Min(maximumBytesToCopy, readBytes);
copyTo.Write(buffer, 0, writeBytes);
totalCopiedBytes += writeBytes;
maximumBytesToCopy -= writeBytes;
}
ArrayPool<byte>.Shared.Return(buffer);
return totalCopiedBytes;
}

View File

@@ -8533,7 +8533,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
)
return true;
float qdelta = MathF.Abs(x.BodyRotation.Dot(m_thisAgentUpdateArgs.BodyRotation));
float qdelta = MathF.Abs(x.BodyRotation.Dot(ref m_thisAgentUpdateArgs.BodyRotation));
return qdelta < QDELTABody; // significant if body rotation above(below cos) threshold
}

View File

@@ -515,7 +515,6 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
return null;
}
private readonly string stringUUIDZero = UUID.Zero.ToString();
public string Store(AssetBase asset)
{
string id;
@@ -527,7 +526,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
id = StoreForeign(asset);
if (m_Cache != null)
{
if (!string.IsNullOrEmpty(id) && !id.Equals(stringUUIDZero))
if (!string.IsNullOrEmpty(id) && !id.Equals(UUID.ZeroString))
m_Cache.Cache(asset);
}
return id;

View File

@@ -1649,7 +1649,7 @@ namespace OpenSim.Region.Framework.Scenes
/// </remarks>
public void MakeChildAgent(ulong newRegionHandle)
{
m_updateAgentReceivedAfterTransferEvent.Reset();
m_updateAgentReceivedAfterTransferEvent?.Reset();
m_haveGroupInformation = false;
m_gotCrossUpdate = false;
m_crossingFlags = 0;
@@ -1660,6 +1660,9 @@ namespace OpenSim.Region.Framework.Scenes
m_log.DebugFormat("[SCENE PRESENCE]: Making {0} a child agent in {1} from root region {2}",
Name, Scene.RegionInfo.RegionName, newRegionHandle);
if(disposed)
return;
// Reset the m_originRegionID as it has dual use as a flag to signal that the UpdateAgent() call orignating
// from the source simulator has completed on a V2 teleport.
lock (m_originRegionIDAccessLock)

View File

@@ -448,12 +448,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
using (MemoryStream inMs = new(primShape.SculptData, physOffset + 2 , physSize - 2)) // skip first 2 bytes in header
{
using DeflateStream decompressionStream = new(inMs, CompressionMode.Decompress);
byte[] readBuffer = ArrayPool<byte>.Shared.Rent(8192);
int readLen = 0;
while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
outMs.Write(readBuffer, 0, readLen);
ArrayPool<byte>.Shared.Return(readBuffer);
decompressionStream.CopyTo(outMs);
}
outMs.Seek(0, SeekOrigin.Begin);
decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs);

View File

@@ -363,11 +363,7 @@ namespace OpenSim.Services.FSAssetService
string GetSHA256Hash(byte[] data)
{
byte[] hash;
using (SHA256 sha = SHA256.Create())
hash = sha.ComputeHash(data);
return BitConverter.ToString(hash).Replace("-", String.Empty);
return Util.SHA256Hash(data);
}
public string HashToPath(string hash)