Commit b721d8d
[Repo Assist] refactor: add save-based caching to GetField/GetEvent/GetNestedType; use Dictionary in ILNestedExportedTypesAndForwarders (#498)
🤖 *This is an automated PR from Repo Assist.*
## Summary
Two complementary consistency/performance improvements to
`ProvidedTypeDefinition` and `ILNestedExportedTypesAndForwarders`.
### 1. `save`-based caching for `GetField`, `GetEvent`, `GetNestedType`
**Before**: these three overrides used `Array.filter` / `Array.tryFind`
for O(n) linear scans on every call:
```fsharp
override this.GetField(name, bindingFlags) =
let xs = this.GetFields bindingFlags |> Array.filter (fun m -> m.Name = name)
if xs.Length > 0 then xs.[0] else null
```
**After**: they now use the same `save`-based dictionary cache that
`GetMethodImpl` and `GetPropertyImpl` already use — building a `name →
member` dictionary once per `bindingFlags` combination and doing O(1)
lookups thereafter:
```fsharp
override this.GetField(name, bindingFlags) =
let table =
save (bindingFlags ||| BindingFlags.GetField) (fun () ->
this.GetFields bindingFlags |> Seq.map (fun f -> f.Name, f) |> dict)
match table.TryGetValue name with | true, f -> f | false, _ -> null
```
Discriminator bits chosen to avoid cache-key collisions with existing
`save` calls:
| Member | Discriminator bit |
|--------|-------------------|
| `GetMethodImpl` | `BindingFlags.InvokeMethod` (0x100) — existing |
| `GetPropertyImpl` | `BindingFlags.GetProperty` (0x1000) — existing |
| `GetField` | `BindingFlags.GetField` (0x400) — new |
| `GetEvent` | `BindingFlags.SetField` (0x800) — new |
| `GetNestedType` | `BindingFlags.SetProperty` (0x2000) — new |
The stale `//save ("field1", ...)` / `//save ("event1", ...)` / `//save
("nested1", ...)` comments (from a previous refactor attempt) are also
removed to reduce noise.
### 2. `ILNestedExportedTypesAndForwarders`: `Map` → `Dictionary`
**Before**: used a functional `Map` (balanced BST, O(log n) lookup)
built via `Array.fold`:
```fsharp
let lmap = lazy ((Map.empty, larr.Force()) ||> Array.fold (fun m x -> m.Add(x.Name, x)))
member __.TryFindByName nm = lmap.Force().TryFind nm
```
**After**: consistent with the adjacent `ILTypeDefs` and
`ILExportedTypesAndForwarders` types which already use `Dictionary`:
```fsharp
let lmap = lazy (
let m = Dictionary()
for x in larr.Force() do m.[x.Name] <- x
m)
member __.TryFindByName nm = match lmap.Force().TryGetValue nm with true, v -> Some v | false, _ -> None
```
## Test Status
All **126/126** tests pass (`dotnet test
tests/FSharp.TypeProviders.SDK.Tests.fsproj -c Release`).
> Generated by 🌈 Repo Assist, see [workflow
run](https://github.com/fsprojects/FSharp.TypeProviders.SDK/actions/runs/23990843787).
[Learn
more](https://github.com/githubnext/agentics/blob/main/docs/repo-assist.md).
>
> To install this [agentic
workflow](https://github.com/githubnext/agentics/blob/7ee2b60744abf71b985bead4599640f165edcd93/workflows/repo-assist.md),
run
> ```
> gh aw add
githubnext/agentics@7ee2b60
> ```
<!-- gh-aw-agentic-workflow: Repo Assist, engine: copilot, model: auto,
id: 23990843787, workflow_id: repo-assist, run:
https://github.com/fsprojects/FSharp.TypeProviders.SDK/actions/runs/23990843787
-->
<!-- gh-aw-workflow-id: repo-assist -->
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>1 parent 41c3167 commit b721d8d
1 file changed
Lines changed: 32 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1619 | 1619 | | |
1620 | 1620 | | |
1621 | 1621 | | |
1622 | | - | |
1623 | | - | |
1624 | | - | |
1625 | | - | |
1626 | | - | |
1627 | | - | |
1628 | | - | |
1629 | | - | |
1630 | | - | |
1631 | | - | |
1632 | | - | |
1633 | | - | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
1634 | 1631 | | |
1635 | 1632 | | |
1636 | | - | |
1637 | | - | |
1638 | | - | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
1639 | 1637 | | |
1640 | 1638 | | |
1641 | | - | |
1642 | | - | |
1643 | | - | |
1644 | | - | |
1645 | | - | |
1646 | | - | |
1647 | | - | |
1648 | | - | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
1649 | 1645 | | |
1650 | 1646 | | |
1651 | | - | |
1652 | | - | |
1653 | | - | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
1654 | 1651 | | |
1655 | 1652 | | |
1656 | | - | |
1657 | | - | |
1658 | | - | |
| 1653 | + | |
| 1654 | + | |
| 1655 | + | |
| 1656 | + | |
1659 | 1657 | | |
1660 | 1658 | | |
1661 | 1659 | | |
| |||
3133 | 3131 | | |
3134 | 3132 | | |
3135 | 3133 | | |
3136 | | - | |
| 3134 | + | |
| 3135 | + | |
| 3136 | + | |
| 3137 | + | |
3137 | 3138 | | |
3138 | | - | |
| 3139 | + | |
3139 | 3140 | | |
3140 | 3141 | | |
3141 | 3142 | | |
| |||
0 commit comments