Skip to content

Commit 3a7a255

Browse files
mpvlrsc
authored andcommitted
internal/export/idna: make more space for mapping index
This prepares for an upcoming Unicode upgrade. Beyond Unicode 13 the size of mappings will grow beyond what can be represented in the allocated 13 bits. Instread of doubling the size of info, we introduce a table of indices into the mappings data. This also allows us to remove the length byte, reducing the overhead of introducing this new table. This change allows for about a 5x growth of the number of mappings, which should suffice for the foreseeable future. Change-Id: Id475dc2473145a1f36bd83b983fa4aa170df6206 Reviewed-on: https://go-review.googlesource.com/c/text/+/501515 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d61dd50 commit 3a7a255

File tree

5 files changed

+1620
-1458
lines changed

5 files changed

+1620
-1458
lines changed

internal/export/idna/gen.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func genTables() {
106106
gen.WriteUnicodeVersion(w)
107107

108108
w.WriteVar("mappings", string(mappings))
109+
w.WriteVar("mappingIndex", mappingIndex)
109110
w.WriteVar("xorData", string(xorData))
110111

111112
sz, err := t.Gen(w, triegen.Compact(&normCompacter{}))
@@ -116,10 +117,14 @@ func genTables() {
116117
}
117118

118119
var (
119-
// mappings contains replacement strings for mapped runes, each prefixed
120-
// with a byte containing the length of the following string.
120+
// mappings contains replacement strings for mapped runes.
121121
mappings = []byte{}
122-
mapCache = map[string]int{}
122+
123+
// mappingIndex contains an offset in mappingBytes representing the start
124+
// of a mapping. Then next entry in mappingIndex points past the end of the
125+
// string.
126+
mappingIndex = []uint16{0}
127+
mapCache = map[string]int{}
123128

124129
// xorData is like mappings, except that it contains XOR data.
125130
// We split these two tables so that we don't get an overflow.
@@ -133,13 +138,13 @@ func makeEntry(r rune, mapped string) info {
133138

134139
if len(orig) != len(mapped) {
135140
// Store the mapped value as is in the mappings table.
136-
index := len(mappings)
141+
index := len(mappingIndex) - 1
137142
if x, ok := mapCache[mapped]; ok {
138143
index = x
139144
} else {
140145
mapCache[mapped] = index
141-
mappings = append(mappings, byte(len(mapped)))
142146
mappings = append(mappings, mapped...)
147+
mappingIndex = append(mappingIndex, uint16(len(mappings)))
143148
}
144149
return info(index) << indexShift
145150
}

0 commit comments

Comments
 (0)