Skip to content

Commit aa4f32e

Browse files
committed
test log extensions for memory usage, #BUILD
1 parent d319d29 commit aa4f32e

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace PointCloudConverter
3030
{
3131
public partial class MainWindow : Window
3232
{
33-
static readonly string version = "27.10.2025";
33+
static readonly string version = "31.10.2025";
3434
static readonly string appname = "PointCloud Converter - " + version;
3535
static readonly string rootFolder = AppDomain.CurrentDomain.BaseDirectory;
3636

@@ -899,7 +899,7 @@ static bool ParseFile(ImportSettings importSettings, int fileIndex, int? taskId,
899899
// filtering is done after scaling and offsets
900900
if (importSettings.useFilter)
901901
{
902-
var cell = ((int)Math.Floor(px / importSettings.filterDistance), (int)Math.Floor(py / importSettings.filterDistance), (int)Math.Floor(pz / importSettings.filterDistance));
902+
var cell = ((int)MathF.Floor(px / importSettings.filterDistance), (int)MathF.Floor(py / importSettings.filterDistance), (int)MathF.Floor(pz / importSettings.filterDistance));
903903

904904
if (!occupiedCells.TryAdd(cell, 0))
905905
{

Tools/LogExtensions.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using PointCloudConverter.Logger;
2+
using System.Buffers;
3+
using System.Text.Json;
4+
5+
public static class LogExtensions
6+
{
7+
// Call this from ProgressTick
8+
public static void WriteProgressUtf8(ILogger log, int threadIndex, long current, long total, int percent, string filePath)
9+
{
10+
using var writer = PooledJsonWriter.Rent(); // pooled buffer + Utf8JsonWriter
11+
writer.WriteStartObject();
12+
writer.WriteString("event", "Progress");
13+
writer.WriteNumber("thread", threadIndex);
14+
writer.WriteNumber("currentPoint", current);
15+
writer.WriteNumber("totalPoints", total);
16+
writer.WriteNumber("percentage", percent);
17+
writer.WriteString("file", filePath);
18+
writer.WriteEndObject();
19+
writer.Flush();
20+
21+
// Send raw UTF-8 to logger (you implement this; falls back to Console)
22+
log.Write(writer.WrittenSpan, LogEvent.Progress);
23+
}
24+
}
25+
26+
/// <summary>Small pooled IBufferWriter + Utf8JsonWriter holder.</summary>
27+
internal sealed class PooledJsonWriter : IDisposable
28+
{
29+
private static readonly ArrayPool<byte> Pool = ArrayPool<byte>.Shared;
30+
private byte[] _buffer;
31+
private int _written;
32+
private readonly Utf8JsonWriter _json;
33+
34+
private PooledJsonWriter()
35+
{
36+
_buffer = Pool.Rent(512); // grows if needed
37+
_json = new Utf8JsonWriter(new BufferWriter(this), new JsonWriterOptions { SkipValidation = true });
38+
}
39+
40+
public static PooledJsonWriter Rent() => new PooledJsonWriter();
41+
42+
public void WriteStartObject() => _json.WriteStartObject();
43+
public void WriteEndObject() => _json.WriteEndObject();
44+
public void WriteString(string name, string value) => _json.WriteString(name, value);
45+
public void WriteNumber(string name, long value) => _json.WriteNumber(name, value);
46+
public void WriteNumber(string name, int value) => _json.WriteNumber(name, value);
47+
public void Flush() => _json.Flush();
48+
49+
public ReadOnlySpan<byte> WrittenSpan => new ReadOnlySpan<byte>(_buffer, 0, _written);
50+
51+
public void Dispose()
52+
{
53+
_json.Dispose();
54+
var buf = _buffer;
55+
_buffer = null;
56+
_written = 0;
57+
if (buf != null) Pool.Return(buf);
58+
}
59+
60+
// Minimal pooled IBufferWriter<byte>
61+
private sealed class BufferWriter : IBufferWriter<byte>
62+
{
63+
private readonly PooledJsonWriter _owner;
64+
public BufferWriter(PooledJsonWriter owner) => _owner = owner;
65+
66+
public void Advance(int count) => _owner._written += count;
67+
68+
public Memory<byte> GetMemory(int sizeHint = 0)
69+
{
70+
Ensure(sizeHint);
71+
return _owner._buffer.AsMemory(_owner._written);
72+
}
73+
74+
public Span<byte> GetSpan(int sizeHint = 0)
75+
{
76+
Ensure(sizeHint);
77+
return _owner._buffer.AsSpan(_owner._written);
78+
}
79+
80+
private void Ensure(int sizeHint)
81+
{
82+
if (sizeHint < 1) sizeHint = 1;
83+
int need = _owner._written + sizeHint;
84+
if (need <= _owner._buffer.Length) return;
85+
86+
int newSize = Math.Max(need, _owner._buffer.Length * 2);
87+
var newBuf = Pool.Rent(newSize);
88+
Buffer.BlockCopy(_owner._buffer, 0, newBuf, 0, _owner._written);
89+
Pool.Return(_owner._buffer);
90+
_owner._buffer = newBuf;
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)