Skip to content

Commit

Permalink
using SortedSetExt value option methods
Browse files Browse the repository at this point in the history
cleanup FreeList
  • Loading branch information
luithefirst committed Oct 9, 2024
1 parent 82ef593 commit 0f62dd3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 450 deletions.
1 change: 1 addition & 0 deletions src/Aardvark.Rendering.GL/Core/Utilities/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ module Driver =
module MemoryManagementUtilities =
open System.Collections.Generic

[<Obsolete("Use Aardvark.Base FreeList")>]
type FreeList<'k, 'v when 'k : comparison>() =
static let comparer = { new IComparer<'k * HashSet<'v>> with member x.Compare((l,_), (r,_)) = compare l r }
let sortedSet = SortedSetExt comparer
Expand Down
12 changes: 6 additions & 6 deletions src/Aardvark.Rendering.GL/Runtime/RenderTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,17 @@ module RenderTasks =
for op in ops do
match op with
| Add(_, cmd) ->
let (l, s, _r) = state.FindNeighbours((cmd, null))
if s.HasValue then
let (struct(hasL, hasV, _), l, _, _) = state.FindNeighboursV((cmd, null))
if hasV then
Log.warn "[NativeRenderProgram] duplicate add of: %A" cmd
else
let l = if l.HasValue then snd l.Value else null
let l = if hasL then snd l else null
let self = program.InsertAfter(l, cmd)
state.Add((cmd, self)) |> ignore
| Rem(_, cmd) ->
let (_, s, _) = state.FindNeighbours((cmd, null))
if s.HasValue then
let _, f = s.Value
let (hasValue, value) = state.FindValue((cmd, null))
if hasValue then
let _, f = value
f.Dispose()
state.Remove(cmd, null) |> ignore
else
Expand Down
50 changes: 35 additions & 15 deletions src/Aardvark.Rendering.Vulkan/Core/Device.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,24 +2094,44 @@ and DeviceFreeList() =
else
store

[<Obsolete("use TryGetAlignedV")>]
member x.TryGetAligned(align : int64, size : int64, [<Optional; DefaultParameterValue(false)>] export : bool) =
let min = new DeviceBlock(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1L, size, false, null, null)
let store = getStore export
let view = store.GetViewBetween(min, null)

let res =
view |> Seq.tryFind (fun b ->
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
s >= size
)
let mutable foundSlot = false
let mutable e = view.GetEnumerator()
while not foundSlot && e.MoveNext() do
let b = e.Current
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
foundSlot <- s >= size

if foundSlot then
store.Remove e.Current |> ignore
Some e.Current
else
None

match res with
| Some res ->
store.Remove res |> ignore
Some res
| None ->
None
member x.TryGetAlignedV(align : int64, size : int64, [<Optional; DefaultParameterValue(false)>] export : bool) =
let min = new DeviceBlock(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1L, size, false, null, null)
let store = getStore export
let view = store.GetViewBetween(min, null)

let mutable foundSlot = false
let mutable e = view.GetEnumerator()
while not foundSlot && e.MoveNext() do
let b = e.Current
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
foundSlot <- s >= size

if foundSlot then
store.Remove e.Current |> ignore
ValueSome e.Current
else
ValueNone

member x.Insert(b : DeviceBlock) =
let store = getStore b.Memory.IsExported
Expand Down Expand Up @@ -2163,8 +2183,8 @@ and DeviceMemoryManager internal(heap : DeviceHeap, blockSize : int64, keepReser

else
lock free (fun () ->
match free.TryGetAligned(align, size, export) with
| Some b ->
match free.TryGetAlignedV(align, size, export) with
| ValueSome b ->
let alignedOffset = next align b.Offset
let alignedSize = b.Size - (alignedOffset - b.Offset)
if alignedOffset > b.Offset then
Expand All @@ -2189,7 +2209,7 @@ and DeviceMemoryManager internal(heap : DeviceHeap, blockSize : int64, keepReser
b.IsFree <- false
b :> DevicePtr

| None ->
| ValueNone ->
addBlock x export
x.Alloc(align, size, export)
)
Expand Down
83 changes: 55 additions & 28 deletions src/Aardvark.Rendering/Utilities/Memory.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,34 +134,61 @@ module Management =
if v % align = 0n then v
else v + (align - v % align)


[<Obsolete("use TryGetGreaterOrEqualV")>]
member x.TryGetGreaterOrEqual(size : nativeint) =
let query = Block(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1n, size, true)
let (_, _, r) = store.FindNeighbours(query)
if r.HasValue then
let r = r.Value
let (struct(_, _, hasR), _, _, r) = store.FindNeighboursV(query)
if hasR then
store.Remove r |> ignore
Some r
else
None

member x.TryGetGreaterOrEqualV(size : nativeint) =
let query = Block(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1n, size, true)
let (struct(_, _, hasR), _, _, r) = store.FindNeighboursV(query)
if hasR then
store.Remove r |> ignore
ValueSome r
else
ValueNone

[<Obsolete("use TryGetAlignedV")>]
member x.TryGetAligned(align : nativeint, size : nativeint) =
let min = Block(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1n, size, true)
let view = store.GetViewBetween(min, null)

let res =
view |> Seq.tryFind (fun b ->
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
s >= size
)
let mutable foundSlot = false
let mutable e = view.GetEnumerator()
while not foundSlot && e.MoveNext() do
let b = e.Current
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
foundSlot <- s >= size

if foundSlot then
store.Remove e.Current |> ignore
Some e.Current
else
None

member x.TryGetAlignedV(align : nativeint, size : nativeint) =
let min = Block(Unchecked.defaultof<_>, Unchecked.defaultof<_>, -1n, size, true)
let view = store.GetViewBetween(min, null)

match res with
| Some res ->
store.Remove res |> ignore
Some res
| None ->
None
let mutable foundSlot = false
let mutable e = view.GetEnumerator()
while not foundSlot && e.MoveNext() do
let b = e.Current
let o = next align b.Offset
let s = b.Size - (o - b.Offset)
foundSlot <- s >= size

if foundSlot then
store.Remove e.Current |> ignore
ValueSome e.Current
else
ValueNone

member x.Insert(b : Block<'a>) =
store.Add b |> ignore
Expand Down Expand Up @@ -239,8 +266,8 @@ module Management =
Block<'a>(x, store, 0n, 0n, true, null, null)
else
lock free (fun () ->
match free.TryGetAligned(align, size) with
| Some b ->
match free.TryGetAlignedV(align, size) with
| ValueSome b ->
let alignedOffset = next align b.Offset
let alignedSize = b.Size - (alignedOffset - b.Offset)
if alignedOffset > b.Offset then
Expand All @@ -262,7 +289,7 @@ module Management =

b.IsFree <- false
b
| None ->
| ValueNone ->
grow size
x.Alloc(align, size)

Expand All @@ -273,8 +300,8 @@ module Management =
Block<'a>(x, store, 0n, 0n, true, null, null)
else
lock free (fun () ->
match free.TryGetGreaterOrEqual size with
| Some b ->
match free.TryGetGreaterOrEqualV size with
| ValueSome b ->
if b.Size > size then
let rest = Block<'a>(x, store, b.Offset + size, b.Size - size, true, b, b.Next)

Expand All @@ -287,7 +314,7 @@ module Management =

b.IsFree <- false
b
| None ->
| ValueNone ->
grow size
x.Alloc size
)
Expand Down Expand Up @@ -481,8 +508,8 @@ module Management =
empty
else
lock free (fun () ->
match free.TryGetAligned(align, size) with
| Some b ->
match free.TryGetAlignedV(align, size) with
| ValueSome b ->
let alignedOffset = next align b.Offset
let alignedSize = b.Size - (alignedOffset - b.Offset)
if alignedOffset > b.Offset then
Expand All @@ -502,7 +529,7 @@ module Management =

b.IsFree <- false
b
| None ->
| ValueNone ->
grow size
x.Alloc(align, size)

Expand All @@ -513,8 +540,8 @@ module Management =
empty
else
lock free (fun () ->
match free.TryGetGreaterOrEqual size with
| Some b ->
match free.TryGetGreaterOrEqualV size with
| ValueSome b ->
if b.Size > size then
let rest = Block<'a>(x, b.Memory, b.Offset + size, b.Size - size, true, b, b.Next)

Expand All @@ -526,7 +553,7 @@ module Management =

b.IsFree <- false
b
| None ->
| ValueNone ->
grow size
x.Alloc size
)
Expand Down
30 changes: 11 additions & 19 deletions src/Aardvark.Rendering/Utilities/Trie.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ type ILinked<'a when 'a :> ILinked<'a>> =

[<AutoOpen>]
module private TrieDictionaryImplementation =
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open System.Collections.Generic
open System
[<AbstractClass>]
type TrieDictionary<'k, 'v>() =
abstract AlterWithNeighbours : key : 'k * action : (voption<'v> -> voption<'v> -> voption<'v> -> voption<'v>) -> voption<'v> * voption<'v> * voption<'v>
Expand All @@ -30,31 +27,26 @@ module private TrieDictionaryImplementation =
}

member x.Find(k : 'k) =
let mutable l = Unchecked.defaultof<_>
let mutable s = Unchecked.defaultof<_>
let mutable r = Unchecked.defaultof<_>
store.FindNeighbours (struct(k, Unchecked.defaultof<'v>), &l, &s, &r)
let (struct(hasL, hasV, hasR), l, v, r) = store.FindNeighboursV (struct(k, Unchecked.defaultof<'v>))

let l =
if l.HasValue then
let struct(_, v) = l.Value
ValueSome v
if hasL then
ValueSome (sndv l)
else ValueNone

let s =
if s.HasValue then
let struct(_, v) = s.Value
ValueSome v
let v =
if hasV then
ValueSome (sndv v)
else
ValueNone

let r =
if r.HasValue then
let struct(_, v) = r.Value
ValueSome v
else ValueNone
if hasR then
ValueSome (sndv r)
else
ValueNone

struct (l, s, r)
struct (l, v, r)

override x.Count = store.Count

Expand Down
Loading

0 comments on commit 0f62dd3

Please sign in to comment.