hash := typ.Hasher(key, m.seed)
- // TODO(prattmic): For indirect key/elem, this is
- // allocating new objects for key/elem. That is
- // unnecessary; the new table could simply point to the
- // existing object.
- slotElem := tab.uncheckedPutSlot(typ, hash, key)
- typedmemmove(typ.Elem, slotElem, elem)
- tab.used++
+ tab.uncheckedPutSlot(typ, hash, key, elem)
}
directory := make([]*table, 1)
}
}
-// uncheckedPutSlot inserts an entry known not to be in the table, returning an
-// entry to the element slot where the element should be written. Used by
-// PutSlot after it has failed to find an existing entry to overwrite duration
-// insertion.
+// uncheckedPutSlot inserts an entry known not to be in the table.
+// This is used for grow/split where we are making a new table from
+// entries in an existing table.
//
-// Updates growthLeft if necessary, but does not update used.
+// Decrements growthLeft and increments used.
//
// Requires that the entry does not exist in the table, and that the table has
// room for another element without rehashing.
//
// Requires that there are no deleted entries in the table.
//
-// Never returns nil.
-func (t *table) uncheckedPutSlot(typ *abi.SwissMapType, hash uintptr, key unsafe.Pointer) unsafe.Pointer {
+// For indirect keys and/or elements, the key and elem pointers can be
+// put directly into the map, they do not need to be copied. This
+// requires the caller to ensure that the referenced memory never
+// changes (by sourcing those pointers from another indirect key/elem
+// map).
+func (t *table) uncheckedPutSlot(typ *abi.SwissMapType, hash uintptr, key, elem unsafe.Pointer) {
if t.growthLeft == 0 {
panic("invariant failed: growthLeft is unexpectedly 0")
}
slotKey := g.key(typ, i)
if typ.IndirectKey() {
- kmem := newobject(typ.Key)
- *(*unsafe.Pointer)(slotKey) = kmem
- slotKey = kmem
+ *(*unsafe.Pointer)(slotKey) = key
+ } else {
+ typedmemmove(typ.Key, slotKey, key)
}
- typedmemmove(typ.Key, slotKey, key)
slotElem := g.elem(typ, i)
if typ.IndirectElem() {
- emem := newobject(typ.Elem)
- *(*unsafe.Pointer)(slotElem) = emem
- slotElem = emem
+ *(*unsafe.Pointer)(slotElem) = elem
+ } else {
+ typedmemmove(typ.Elem, slotElem, elem)
}
t.growthLeft--
+ t.used++
g.ctrls().set(i, ctrl(h2(hash)))
- return slotElem
+ return
}
}
}
} else {
newTable = right
}
- // TODO(prattmic): For indirect key/elem, this is
- // allocating new objects for key/elem. That is
- // unnecessary; the new table could simply point to the
- // existing object.
- slotElem := newTable.uncheckedPutSlot(typ, hash, key)
- typedmemmove(typ.Elem, slotElem, elem)
- newTable.used++
+ newTable.uncheckedPutSlot(typ, hash, key, elem)
}
}
hash := typ.Hasher(key, m.seed)
- // TODO(prattmic): For indirect key/elem, this is
- // allocating new objects for key/elem. That is
- // unnecessary; the new table could simply point to the
- // existing object.
- slotElem := newTable.uncheckedPutSlot(typ, hash, key)
- typedmemmove(typ.Elem, slotElem, elem)
- newTable.used++
+ newTable.uncheckedPutSlot(typ, hash, key, elem)
}
}
}