mirror of
https://github.com/modernuo/ModernUO
synced 2026-08-11 22:23:06 -04:00
## Summary Adds two pieces of pathfinding tooling on top of PR #2448's lazy `.swb` infrastructure: - **`PathfindRecorder`** — admin-toggled JSONL telemetry capture; one record per `BitmapAStarAlgorithm.Find` call. Output format matches the BDN harness corpus, so production traffic can be captured and replayed in benchmarks without an adapter. - **Public bake helpers on `StepCache`** — `ComputeLiveTileDataHash`, `TryReadTileDataHashFromFile`, `BakeMap`, `ClearResidentChunks`. Lets the benchmark project (and any future bake utility) drive cache fill + persist without exposing internal types. The companion BDN harness update lives in [ModernUO-Benchmarks#kb/pathfinding-pr4-bench](https://github.com/modernuo/ModernUO-Benchmarks/tree/kb/pathfinding-pr4-bench): porting `Benchmarks/PathfindInGame/` from the `kb/ai_pathfinding` branch to the API shipped in #2446–#2448. ## What's in this PR ### `PathfindRecorder` (`PathfindRecorder.cs`) - Holds a single `StreamWriter` open while recording; its internal buffer absorbs per-record writes without per-call `File.AppendAllText`. - Single `bool` check on the hot path; cheap when disabled. - Disabling flushes + disposes; an IO failure during write also disables the recorder. - Server config: - `pathfinding.recorder.enable` — bool, default `false`. Read on boot via `GetOrUpdateSetting`. - `pathfinding.recorder.path` — default `<basedir>/Data/Pathfinding/recordings/pathfinds.jsonl`. - Hooked into `BitmapAStarAlgorithm.Find` — runs once per call, does nothing when disabled. - Admin command: `[PathRecord [on|off|flush|status]` (default `status`). ### Public cache helpers - `static ulong StepCache.ComputeLiveTileDataHash()` — wraps the file module's hash function for staleness checks. - `static bool StepCache.TryReadTileDataHashFromFile(string, out ulong)` — peeks at a `.swb` file's hash field (20 bytes). - `int StepCache.BakeMap(int, string)` — walks every chunk in the map, populates resident set, saves. Offline / fixture use; blocks for many seconds on a full-map walk. - `void StepCache.ClearResidentChunks()` — drops chunks + zeros counters but keeps lazy readers open. Lets benchmark loops measure "first query after boot" cost across iterations without the lazy-reader reopen overhead.
149 lines
5 KiB
C#
149 lines
5 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2026 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: HashUtility.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.IO.Hashing;
|
|
using System.Numerics;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Server;
|
|
|
|
public static class HashUtility
|
|
{
|
|
// *************** DO NOT CHANGE THIS NUMBER ****************
|
|
// * Computed hashes might be serialized against this seed! *
|
|
// **********************************************************
|
|
private const ulong xxHash3Seed = 9609125370673258709ul; // Randomly generated 64-bit prime number
|
|
private const uint xxHash1Seed = 665738807u; // Randomly generated 32-bit prime number
|
|
|
|
[ThreadStatic]
|
|
private static XxHash3 _xxHash3;
|
|
|
|
[ThreadStatic]
|
|
private static XxHash32 _xxHash32;
|
|
|
|
public static ulong ComputeHash64(ReadOnlySpan<char> str)
|
|
{
|
|
if (str.Length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));
|
|
hasher.Append(MemoryMarshal.Cast<char, byte>(str));
|
|
|
|
var result = hasher.GetCurrentHashAsUInt64();
|
|
hasher.Reset();
|
|
|
|
return result;
|
|
}
|
|
|
|
public static ulong ComputeHash64(ReadOnlySpan<byte> bytes)
|
|
{
|
|
if (bytes.Length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));
|
|
hasher.Append(bytes);
|
|
|
|
var result = hasher.GetCurrentHashAsUInt64();
|
|
hasher.Reset();
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// One-shot streaming hash. Reads from the stream's current position to its end and
|
|
/// returns the XxHash3 result. The caller is responsible for setting the stream
|
|
/// position before the call (and restoring it afterward, if the stream will be reused).
|
|
/// Returns 0 on a null or unreadable stream.
|
|
/// </summary>
|
|
public static ulong ComputeHash64(Stream stream)
|
|
{
|
|
if (stream == null || !stream.CanRead)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));
|
|
hasher.Append(stream);
|
|
|
|
var result = hasher.GetCurrentHashAsUInt64();
|
|
hasher.Reset();
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a fresh <see cref="XxHash3"/> seeded with HashUtility's standard seed.
|
|
/// Use this when you need to combine multiple inputs into a single hash via
|
|
/// successive Append calls — the one-shot ComputeHash64 overloads are stateless
|
|
/// (Reset between callers) and can't compose. Caller owns the returned instance.
|
|
/// </summary>
|
|
public static XxHash3 CreateXxHash3() => new(unchecked((long)xxHash3Seed));
|
|
|
|
public static uint ComputeHash32(ReadOnlySpan<char> str)
|
|
{
|
|
if (str == ReadOnlySpan<char>.Empty)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed));
|
|
hasher.Append(MemoryMarshal.Cast<char, byte>(str));
|
|
|
|
var result = hasher.GetCurrentHashAsUInt32();
|
|
hasher.Reset();
|
|
|
|
return result;
|
|
}
|
|
|
|
public static unsafe int GetNetFrameworkHashCode(this string? str)
|
|
{
|
|
if (str == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
fixed (char* src = &str.GetPinnableReference())
|
|
{
|
|
uint hash1 = (5381 << 16) + 5381;
|
|
var hash2 = hash1;
|
|
|
|
var ptr = (uint*)src;
|
|
var length = str.Length;
|
|
|
|
while (length > 2)
|
|
{
|
|
length -= 4;
|
|
// Where length is 4n-1 (e.g. 3,7,11,15,19) this additionally consumes the null terminator
|
|
hash1 = (BitOperations.RotateLeft(hash1, 5) + hash1) ^ ptr[0];
|
|
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ ptr[1];
|
|
ptr += 2;
|
|
}
|
|
|
|
if (length > 0)
|
|
{
|
|
// Where length is 4n-3 (e.g. 1,5,9,13,17) this additionally consumes the null terminator
|
|
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ ptr[0];
|
|
}
|
|
|
|
return (int)(hash1 + hash2 * 1566083941);
|
|
}
|
|
}
|
|
}
|