Skip to content
This repository was archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Make StringTable size expandable. (#1338)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Make StringTable size expandable.

**Release note**:

```release-note
NONE
```
  • Loading branch information
ozevren authored and istio-merge-robot committed Sep 29, 2017
1 parent 4b08627 commit c2c6b0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/il/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
)

const (
// maxStrings is the maximum number of strings that can be placed in the strings table.
maxStrings = 100
// allocSize is the allocation size when extending the string table.
allocSize = 512
nullString = "<<DEADBEEF>>"
)

Expand All @@ -37,7 +37,7 @@ func newStringTable() *StringTable {

t := &StringTable{
stringToID: make(map[string]uint32),
idToString: make([]string, maxStrings),
idToString: make([]string, allocSize),
nextID: 0,
}

Expand All @@ -63,10 +63,17 @@ func (t *StringTable) GetID(s string) uint32 {

id, exists = t.stringToID[s]
if !exists {
t.stringToID[s] = t.nextID
t.idToString[t.nextID] = s
id = t.nextID
t.nextID++

if len(t.idToString) <= int(id) {
tmp := make([]string, len(t.idToString)+allocSize)
copy(tmp, t.idToString)
t.idToString = tmp
}

t.stringToID[s] = id
t.idToString[id] = s
}

return id
Expand Down
26 changes: 26 additions & 0 deletions pkg/il/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package il

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -91,3 +92,28 @@ func TestGetString(t *testing.T) {
t.Fatal()
}
}

func TestExpansion(t *testing.T) {
s := newStringTable()

strMap := make(map[uint32]string)

for i := 1; i < allocSize*10+1; i++ {
str := fmt.Sprintf("str-%d", i)
id := s.GetID(str)
strMap[id] = str
}

for id, str := range strMap {

actualStr := s.GetString(id)
if actualStr != str {
t.Fatal()
}

actualID := s.GetID(str)
if actualID != id {
t.Fatal()
}
}
}

0 comments on commit c2c6b0a

Please sign in to comment.