forked from cilium/ebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storing Reference and Symbol in every instruction increases the size of the type by 32 bytes. This is wasteful considering that most instructions carry neither. Introduce a metadata type that add only a single machine word to each instruction, and which can store arbitrary metadata.
- Loading branch information
Showing
13 changed files
with
203 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,33 @@ | ||
package asm | ||
|
||
// meta is a way to attach metadata to an instruction. | ||
// | ||
// An empty metadata only takes up a single machine word, so this is | ||
// useful for cases where metadata is mostly empty. | ||
type meta map[interface{}]interface{} | ||
|
||
// add a value to the metadata set. | ||
// | ||
// Avoids modifying old metadata by copying if necessary. | ||
func (m *meta) add(key, value interface{}) { | ||
have, ok := (*m)[key] | ||
n := len(*m) + 1 | ||
if ok { | ||
if have == value { | ||
// We already have this key with the same value, so we don't | ||
// need to do anything. | ||
return | ||
} | ||
|
||
n-- | ||
} | ||
|
||
// There is no such key, or the value is different. | ||
// Create a copy and overwrite the entry for key. | ||
cpy := make(meta, n) | ||
for t, v := range *m { | ||
cpy[t] = v | ||
} | ||
cpy[key] = value | ||
*m = cpy | ||
} |
This file contains 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,60 @@ | ||
package asm | ||
|
||
import ( | ||
"testing" | ||
"unsafe" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestMetadata(t *testing.T) { | ||
var m meta | ||
|
||
t.Log("size:", unsafe.Sizeof(m)) | ||
|
||
// A lookup in a nil meta should return nil. | ||
qt.Assert(t, m[bool(false)], qt.IsNil) | ||
|
||
// We can look up anything we inserted. | ||
m.add(bool(false), int(0)) | ||
qt.Assert(t, m, qt.HasLen, 1) | ||
qt.Assert(t, m[bool(false)], qt.Equals, int(0)) | ||
|
||
// We have copy on write semantics | ||
old := m | ||
m.add(bool(false), int(1)) | ||
qt.Assert(t, m, qt.HasLen, 1) | ||
qt.Assert(t, m[bool(false)], qt.Equals, int(1)) | ||
qt.Assert(t, old[bool(false)], qt.Equals, int(0)) | ||
|
||
// Newtypes are handled distinctly. | ||
type b bool | ||
m.add(b(false), int(42)) | ||
qt.Assert(t, m, qt.HasLen, 2) | ||
qt.Assert(t, m[bool(false)], qt.Equals, int(1)) | ||
qt.Assert(t, m[b(false)], qt.Equals, int(42)) | ||
} | ||
|
||
func BenchmarkMetadata(b *testing.B) { | ||
type k struct{} | ||
|
||
b.Run("add", func(b *testing.B) { | ||
m := meta{0: 0, 1: 1, 2: 2, 3: 3} | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
m.add(k{}, b.N) | ||
} | ||
}) | ||
|
||
b.Run("add existing", func(b *testing.B) { | ||
m := meta{0: 0, 1: 1, 2: 2, k{}: 0} | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
m.add(k{}, 0) | ||
} | ||
}) | ||
} |
Oops, something went wrong.