-
Notifications
You must be signed in to change notification settings - Fork 24
fix(authz): optimize subject mapping evaluation performance #2945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
strantalis
wants to merge
6
commits into
opentdf:main
Choose a base branch
from
strantalis:fix/sm-perf-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98b5df7
perf(authz): optimize subject mapping evaluation performance
strantalis b7c396c
perf(authz): fix selector dedupe in JIT PDP
strantalis c5ec090
feat(examples): add complex authorization benchmarks
strantalis 9bbc024
revert(examples): remove complex authorization benchmarks
strantalis 22987e4
perf(authz): optimize flatten and subject mapping for small inputs
strantalis 0901679
test(authz): add test for hierarchyRule defensive code path
strantalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,14 @@ import ( | |
| "fmt" | ||
| ) | ||
|
|
||
| // indexThreshold is the minimum number of items before we build an index. | ||
| // For smaller structures, linear scan is faster than map overhead. | ||
| const indexThreshold = 8 | ||
|
|
||
| type Flattened struct { | ||
| Items []Item `json:"flattened"` | ||
| // index provides O(1) selector lookups; populated by Flatten for structures >= indexThreshold | ||
| index map[string][]interface{} | ||
| } | ||
|
|
||
| type Item struct { | ||
|
|
@@ -15,7 +21,15 @@ type Item struct { | |
| } | ||
|
|
||
| func GetFromFlattened(flat Flattened, selector string) []interface{} { | ||
| itemsToReturn := []interface{}{} | ||
| // Fast-path: use prebuilt index for O(1) lookup | ||
| if flat.index != nil { | ||
| if vals, ok := flat.index[selector]; ok { | ||
| return vals | ||
| } | ||
| return nil | ||
| } | ||
| // Fallback: linear scan for small structures or backwards compatibility | ||
| var itemsToReturn []interface{} | ||
| for _, item := range flat.Items { | ||
| if item.Key == selector { | ||
| itemsToReturn = append(itemsToReturn, item.Value) | ||
|
|
@@ -24,18 +38,39 @@ func GetFromFlattened(flat Flattened, selector string) []interface{} { | |
| return itemsToReturn | ||
| } | ||
|
|
||
| // Flatten returns a Flattened struct with an index for O(1) lookups via GetFromFlattened. | ||
| // For small structures (< indexThreshold items), the index is skipped and lookups use linear scan. | ||
| func Flatten(m map[string]interface{}) (Flattened, error) { | ||
| flattened := Flattened{} | ||
| items, err := flattenInterface(m) | ||
| if err != nil { | ||
| return Flattened{}, err | ||
| } | ||
| flattened.Items = items | ||
| return flattened, nil | ||
|
|
||
| // Build index in a separate pass, only for larger structures | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we could improve this further by updating |
||
| idx := buildIndex(items) | ||
|
|
||
| return Flattened{ | ||
| Items: items, | ||
| index: idx, | ||
| }, nil | ||
| } | ||
|
|
||
| // buildIndex constructs the selector index from flattened items. | ||
| // Returns nil for small structures where linear scan is faster. | ||
| func buildIndex(items []Item) map[string][]interface{} { | ||
| if len(items) < indexThreshold { | ||
| return nil | ||
| } | ||
|
|
||
| idx := make(map[string][]interface{}, len(items)) | ||
| for _, it := range items { | ||
| idx[it.Key] = append(idx[it.Key], it.Value) | ||
| } | ||
| return idx | ||
| } | ||
|
|
||
| func flattenInterface(i interface{}) ([]Item, error) { | ||
| o := []Item{} | ||
| var o []Item | ||
| switch child := i.(type) { | ||
| case map[string]interface{}: | ||
| for k, v := range child { | ||
|
|
@@ -44,20 +79,23 @@ func flattenInterface(i interface{}) ([]Item, error) { | |
| return nil, err | ||
| } | ||
| for _, item := range nm { | ||
| o = append(o, Item{Key: "." + k + item.Key, Value: item.Value}) | ||
| key := "." + k + item.Key | ||
| o = append(o, Item{Key: key, Value: item.Value}) | ||
| } | ||
| } | ||
| case []interface{}: | ||
| for idx, item := range child { | ||
| k := fmt.Sprintf("[%v]", idx) | ||
| k2 := "[]" | ||
| for index, item := range child { | ||
| kIdx := fmt.Sprintf("[%v]", index) | ||
strantalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| kAny := "[]" | ||
| flattenedItem, err := flattenInterface(item) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, item := range flattenedItem { | ||
| o = append(o, Item{Key: k + item.Key, Value: item.Value}) | ||
| o = append(o, Item{Key: k2 + item.Key, Value: item.Value}) | ||
| for _, it := range flattenedItem { | ||
| keyIdx := kIdx + it.Key | ||
| keyAny := kAny + it.Key | ||
| o = append(o, Item{Key: keyIdx, Value: it.Value}) | ||
| o = append(o, Item{Key: keyAny, Value: it.Value}) | ||
| } | ||
| } | ||
| case bool, int, string, float64, float32: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package flattening | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|
|
||
| // Benchmark with larger dataset to show lookup performance | ||
| func BenchmarkGetFromFlattened_Large(b *testing.B) { | ||
| // Create a larger flattened structure via Flatten | ||
| largeInput := make(map[string]interface{}) | ||
| for i := 0; i < 100; i++ { | ||
| largeInput[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i) | ||
| } | ||
| flatInput, err := Flatten(largeInput) | ||
| if err != nil { | ||
| b.Fatal(err) | ||
| } | ||
|
|
||
| // Query for a key in the middle | ||
| queryString := ".key50" | ||
| b.ResetTimer() | ||
| for n := 0; n < b.N; n++ { | ||
| _ = GetFromFlattened(flatInput, queryString) | ||
| } | ||
| } | ||
|
|
||
| // Benchmark multiple lookups on same flattened entity | ||
| func BenchmarkGetFromFlattened_MultipleLookups(b *testing.B) { | ||
| largeInput := make(map[string]interface{}) | ||
| for i := 0; i < 50; i++ { | ||
| largeInput[fmt.Sprintf("attr%d", i)] = fmt.Sprintf("value%d", i) | ||
| } | ||
| flatInput, err := Flatten(largeInput) | ||
| if err != nil { | ||
| b.Fatal(err) | ||
| } | ||
|
|
||
| queries := []string{".attr0", ".attr10", ".attr25", ".attr40", ".attr49"} | ||
| b.ResetTimer() | ||
| for n := 0; n < b.N; n++ { | ||
| for _, q := range queries { | ||
| _ = GetFromFlattened(flatInput, q) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark with nested structure (common in entity representations) | ||
| func BenchmarkFlatten_NestedEntity(b *testing.B) { | ||
| nestedInput := map[string]interface{}{ | ||
| "user": map[string]interface{}{ | ||
| "id": "user123", | ||
| "name": "Test User", | ||
| "email": "[email protected]", | ||
| "attributes": map[string]interface{}{ | ||
| "department": "Engineering", | ||
| "level": "Senior", | ||
| "groups": []interface{}{"group1", "group2", "group3"}, | ||
| }, | ||
| }, | ||
| "roles": []interface{}{ | ||
| map[string]interface{}{"name": "admin", "scope": "global"}, | ||
| map[string]interface{}{"name": "reader", "scope": "local"}, | ||
| }, | ||
| } | ||
| b.ResetTimer() | ||
| for n := 0; n < b.N; n++ { | ||
| _, err := Flatten(nestedInput) | ||
| if err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.