Browse Source

Using a reference to an array instead of pointer arithmetic. This means it will benefit from bounds checking on the array.

tags/v0.8.0
Martin Evans 2 years ago
parent
commit
a03fdc4818
1 changed files with 21 additions and 21 deletions
  1. +21
    -21
      LLama/Native/SafeLLamaGrammarHandle.cs

+ 21
- 21
LLama/Native/SafeLLamaGrammarHandle.cs View File

@@ -3,6 +3,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using LLama.Exceptions;
using LLama.Grammars;

@@ -52,31 +53,30 @@ namespace LLama.Native
var rulePointers = ArrayPool<IntPtr>.Shared.Rent(rules.Count);
try
{
fixed (LLamaGrammarElement* allElementsPtr = allElements)
{
var elementIndex = 0;
var ruleIndex = 0;
foreach (var rule in rules)
{
Debug.Assert(elementIndex < allElements.Length);
// We're taking pointers into `allElements` below, so this pin is required to fix
// that memory in place while those pointers are in use!
using var pin = allElements.AsMemory().Pin();

// Save a pointer to the start of this rule
rulePointers[ruleIndex++] = (IntPtr)(allElementsPtr + elementIndex);
var elementIndex = 0;
var ruleIndex = 0;
foreach (var rule in rules)
{
// Save a pointer to the start of this rule
rulePointers[ruleIndex++] = (IntPtr)Unsafe.AsPointer(ref allElements[elementIndex]);

// Copy all of the rule elements into the flat array
foreach (var element in rule.Elements)
allElements[elementIndex++] = element;
}
// Copy all of the rule elements into the flat array
foreach (var element in rule.Elements)
allElements[elementIndex++] = element;
}

// Sanity check some things that should be true if the copy worked as planned
Debug.Assert((ulong)ruleIndex == nrules);
Debug.Assert(elementIndex == totalElements);
// Sanity check some things that should be true if the copy worked as planned
Debug.Assert((ulong)ruleIndex == nrules);
Debug.Assert(elementIndex == totalElements);

// Make the actual call through to llama.cpp
fixed (void* ptr = rulePointers)
{
return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index);
}
// Make the actual call through to llama.cpp
fixed (void* ptr = rulePointers)
{
return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index);
}
}
finally


Loading…
Cancel
Save