markdown/elemheap.go
Michael Teichgräber cca2b1a3c2 mk_element: turn global allocation into a per-parser, reusable allocation.
This reduces memory usage within a single document, because
elements allocated during parsing of one Docblock can be reused
during parsing of the next Docblock.
2012-04-29 23:54:10 +02:00

50 lines
1.0 KiB
Go

package markdown
/*
Elements are not allocated one at a time, but in rows of
elemHeap.RowSize elements. After N elements have been
requested, a row is exhausted, and the next one will
be allocated. Previously allocated rows are tracked in
elemHeap.rows.
Pos() and setPos() methods allow to query and reset the
current position (row, and position within the row), which
allows reusing elements. It must be made sure, that previous
users of such storage don't access it anymore once setPos has
been called.
*/
type elemHeap struct {
rows [][]element
heapPos
rowSize int
}
type heapPos struct {
iRow int
row []element
}
func (h *elemHeap) nextRow() []element {
h.iRow++
if h.iRow == len(h.rows) {
h.rows = append(h.rows, make([]element, h.rowSize))
}
h.row = h.rows[h.iRow]
return h.row
}
func (h *elemHeap) init(size int) {
h.rowSize = size
h.rows = [][]element{make([]element, size)}
h.row = h.rows[h.iRow]
}
func (h *elemHeap) Pos() heapPos {
return h.heapPos
}
func (h *elemHeap) setPos(i heapPos) {
h.heapPos = i
}