Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More mlrval size-reduction #1132

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/sizes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package main

import (
"fmt"

"github.com/johnkerl/miller/internal/pkg/mlrval"
)

Expand All @@ -15,5 +17,6 @@ func main() {
mvs[0] = *mlrval.FromString("hello")
mvs[1] = *mlrval.FromString("world")
mvs[0].ShowSizes()
fmt.Println()
mvs[1].ShowSizes()
}
6 changes: 3 additions & 3 deletions internal/pkg/mlrval/mlrmap_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (mlrmap *Mlrmap) getWithMlrvalArrayIndex(index *Mlrval) (*Mlrval, error) {
current := mlrmap
var retval *Mlrval = nil
lib.InternalCodingErrorIf(!index.IsArray())
array := index.arrayval
array := index.x.arrayval
n := len(array)
for i, piece := range array {
next, err := current.GetWithMlrvalIndex(piece)
Expand All @@ -350,7 +350,7 @@ func (mlrmap *Mlrmap) getWithMlrvalArrayIndex(index *Mlrval) (*Mlrval, error) {
if !next.IsMap() {
return nil, fmt.Errorf("mlr: cannot multi-index non-map.")
}
current = next.mapval
current = next.x.mapval
} else {
retval = next.Copy()
}
Expand Down Expand Up @@ -784,7 +784,7 @@ func (mlrmap *Mlrmap) SortByKeyRecursively() {
// Old record will be GC'ed: just move pointers
value := mlrmap.Get(key)
if value.IsMap() {
value.mapval.SortByKeyRecursively()
value.x.mapval.SortByKeyRecursively()
}
other.PutReference(key, value)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/pkg/mlrval/mlrmap_flatten_unflatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ func (mlrmap *Mlrmap) CopyUnflattened(
// Is the field name something dot something?
if strings.Contains(pe.Key, separator) {
arrayOfIndices := SplitAXHelper(pe.Key, separator)
lib.InternalCodingErrorIf(len(arrayOfIndices.arrayval) < 1)
lib.InternalCodingErrorIf(len(arrayOfIndices.x.arrayval) < 1)
// If the input field name was "x.a" then remember the "x".
baseIndex := arrayOfIndices.arrayval[0].String()
baseIndex := arrayOfIndices.x.arrayval[0].String()
affectedBaseIndices[baseIndex] = true
// Use PutIndexed to assign $x["a"] = 7, or $x["b"] = 8, etc.
other.PutIndexed(
CopyMlrvalArray(arrayOfIndices.arrayval),
CopyMlrvalArray(arrayOfIndices.x.arrayval),
unflattenTerminal(pe.Value).Copy(),
)
} else {
Expand Down Expand Up @@ -187,13 +187,13 @@ func (mlrmap *Mlrmap) CopyUnflattenFields(
// Is the field name something dot something?
if strings.Contains(pe.Key, separator) {
arrayOfIndices := SplitAXHelper(pe.Key, separator)
lib.InternalCodingErrorIf(len(arrayOfIndices.arrayval) < 1)
lib.InternalCodingErrorIf(len(arrayOfIndices.x.arrayval) < 1)
// If the input field name was "x.a" then remember the "x".
baseIndex := arrayOfIndices.arrayval[0].String()
baseIndex := arrayOfIndices.x.arrayval[0].String()
if fieldNameSet[baseIndex] {
// Use PutIndexed to assign $x["a"] = 7, or $x["b"] = 8, etc.
other.PutIndexed(
CopyMlrvalArray(arrayOfIndices.arrayval),
CopyMlrvalArray(arrayOfIndices.x.arrayval),
unflattenTerminal(pe.Value).Copy(),
)
affectedBaseIndices[baseIndex] = true
Expand Down Expand Up @@ -247,7 +247,7 @@ func SplitAXHelper(input string, separator string) *Mlrval {
output := FromArray(make([]*Mlrval, len(fields)))

for i, field := range fields {
output.arrayval[i] = FromString(field)
output.x.arrayval[i] = FromString(field)
}

return output
Expand Down
14 changes: 7 additions & 7 deletions internal/pkg/mlrval/mlrval_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (mv *Mlrval) GetArrayLength() (int, bool) {
if mv.IsArray() {
return len(mv.arrayval), true
return len(mv.x.arrayval), true
} else {
return -999, false
}
Expand All @@ -35,21 +35,21 @@ func (mv *Mlrval) FlattenToMap(prefix string, delimiter string) Mlrval {
if mv.IsMap() {
// Without this, the for-loop below is zero-pass and fields with "{}"
// values would disappear entirely in a JSON-to-CSV conversion.
if mv.mapval.IsEmpty() {
if mv.x.mapval.IsEmpty() {
if prefix != "" {
retval.PutCopy(prefix, FromString("{}"))
}
}

for pe := mv.mapval.Head; pe != nil; pe = pe.Next {
for pe := mv.x.mapval.Head; pe != nil; pe = pe.Next {
nextPrefix := pe.Key
if prefix != "" {
nextPrefix = prefix + delimiter + nextPrefix
}
if pe.Value.IsMap() || pe.Value.IsArray() {
nextResult := pe.Value.FlattenToMap(nextPrefix, delimiter)
lib.InternalCodingErrorIf(nextResult.mvtype != MT_MAP)
for pf := nextResult.mapval.Head; pf != nil; pf = pf.Next {
for pf := nextResult.x.mapval.Head; pf != nil; pf = pf.Next {
retval.PutCopy(pf.Key, pf.Value.Copy())
}
} else {
Expand All @@ -60,21 +60,21 @@ func (mv *Mlrval) FlattenToMap(prefix string, delimiter string) Mlrval {
} else if mv.IsArray() {
// Without this, the for-loop below is zero-pass and fields with "[]"
// values would disappear entirely in a JSON-to-CSV conversion.
if len(mv.arrayval) == 0 {
if len(mv.x.arrayval) == 0 {
if prefix != "" {
retval.PutCopy(prefix, FromString("[]"))
}
}

for zindex, value := range mv.arrayval {
for zindex, value := range mv.x.arrayval {
nextPrefix := strconv.Itoa(zindex + 1) // Miller user-space indices are 1-up
if prefix != "" {
nextPrefix = prefix + delimiter + nextPrefix
}
if value.IsMap() || value.IsArray() {
nextResult := value.FlattenToMap(nextPrefix, delimiter)
lib.InternalCodingErrorIf(nextResult.mvtype != MT_MAP)
for pf := nextResult.mapval.Head; pf != nil; pf = pf.Next {
for pf := nextResult.x.mapval.Head; pf != nil; pf = pf.Next {
retval.PutCopy(pf.Key, pf.Value.Copy())
}
} else {
Expand Down
40 changes: 20 additions & 20 deletions internal/pkg/mlrval/mlrval_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (mv *Mlrval) ArrayGet(mindex *Mlrval) Mlrval {
if !mindex.IsInt() {
return *ERROR
}
value := arrayGetAliased(&mv.arrayval, int(mindex.intval))
value := arrayGetAliased(&mv.x.arrayval, int(mindex.intval))
if value == nil {
return *ABSENT
} else {
Expand Down Expand Up @@ -116,12 +116,12 @@ func (mv *Mlrval) ArrayPut(mindex *Mlrval, value *Mlrval) {
os.Exit(1)
}

ok := arrayPutAliased(&mv.arrayval, int(mindex.intval), value)
ok := arrayPutAliased(&mv.x.arrayval, int(mindex.intval), value)
if !ok {
fmt.Fprintf(
os.Stderr,
"mlr: array index %d out of bounds %d..%d\n",
mindex.intval, 1, len(mv.arrayval),
mindex.intval, 1, len(mv.x.arrayval),
)
os.Exit(1)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (mv *Mlrval) ArrayAppend(value *Mlrval) {
// Silent no-ops are not good UX ...
return
}
mv.arrayval = append(mv.arrayval, value)
mv.x.arrayval = append(mv.x.arrayval, value)
}

// ================================================================
Expand All @@ -222,7 +222,7 @@ func (mv *Mlrval) MapGet(key *Mlrval) Mlrval {
return *ERROR
}

mval, err := mv.mapval.GetWithMlrvalIndex(key)
mval, err := mv.x.mapval.GetWithMlrvalIndex(key)
if err != nil { // xxx maybe error-return in the API
return *ERROR
}
Expand All @@ -243,9 +243,9 @@ func (mv *Mlrval) MapPut(key *Mlrval, value *Mlrval) {
}

if key.IsString() {
mv.mapval.PutCopy(key.printrep, value)
mv.x.mapval.PutCopy(key.printrep, value)
} else if key.IsInt() {
mv.mapval.PutCopy(key.String(), value)
mv.x.mapval.PutCopy(key.String(), value)
}
// TODO: need to be careful about semantics here.
// Silent no-ops are not good UX ...
Expand Down Expand Up @@ -291,19 +291,19 @@ func (mv *Mlrval) PutIndexed(indices []*Mlrval, rvalue *Mlrval) error {
lib.InternalCodingErrorIf(len(indices) < 1)

if mv.IsMap() {
return putIndexedOnMap(mv.mapval, indices, rvalue)
return putIndexedOnMap(mv.x.mapval, indices, rvalue)

} else if mv.IsArray() {
return putIndexedOnArray(&mv.arrayval, indices, rvalue)
return putIndexedOnArray(&mv.x.arrayval, indices, rvalue)

} else {
baseIndex := indices[0]
if baseIndex.IsString() {
*mv = *FromEmptyMap()
return putIndexedOnMap(mv.mapval, indices, rvalue)
return putIndexedOnMap(mv.x.mapval, indices, rvalue)
} else if baseIndex.IsInt() {
*mv = *FromEmptyArray()
return putIndexedOnArray(&mv.arrayval, indices, rvalue)
return putIndexedOnArray(&mv.x.arrayval, indices, rvalue)
} else {
return errors.New(
"mlr: only maps and arrays are indexable; got " + mv.GetTypeName(),
Expand All @@ -326,7 +326,7 @@ func putIndexedOnMap(baseMap *Mlrmap, indices []*Mlrval, rvalue *Mlrval) error {
".",
)
}
*baseMap = *rvalue.mapval.Copy()
*baseMap = *rvalue.x.mapval.Copy()
return nil
}

Expand Down Expand Up @@ -438,10 +438,10 @@ func (mv *Mlrval) RemoveIndexed(indices []*Mlrval) error {
lib.InternalCodingErrorIf(len(indices) < 1)

if mv.IsMap() {
return removeIndexedOnMap(mv.mapval, indices)
return removeIndexedOnMap(mv.x.mapval, indices)

} else if mv.IsArray() {
return removeIndexedOnArray(&mv.arrayval, indices)
return removeIndexedOnArray(&mv.x.arrayval, indices)

} else {
return errors.New(
Expand Down Expand Up @@ -659,13 +659,13 @@ func NewMlrvalForAutoDeepen(mvtype MVType) (*Mlrval, error) {

func (mv *Mlrval) Arrayify() *Mlrval {
if mv.IsMap() {
if mv.mapval.IsEmpty() {
if mv.x.mapval.IsEmpty() {
return mv
}

convertible := true
i := 0
for pe := mv.mapval.Head; pe != nil; pe = pe.Next {
for pe := mv.x.mapval.Head; pe != nil; pe = pe.Next {
sval := strconv.Itoa(i + 1) // Miller user-space indices are 1-up
i++
if pe.Key != sval {
Expand All @@ -675,9 +675,9 @@ func (mv *Mlrval) Arrayify() *Mlrval {
}

if convertible {
arrayval := make([]*Mlrval, mv.mapval.FieldCount)
arrayval := make([]*Mlrval, mv.x.mapval.FieldCount)
i := 0
for pe := mv.mapval.Head; pe != nil; pe = pe.Next {
for pe := mv.x.mapval.Head; pe != nil; pe = pe.Next {
arrayval[i] = pe.Value.Copy()
i++
}
Expand All @@ -690,8 +690,8 @@ func (mv *Mlrval) Arrayify() *Mlrval {
} else if mv.IsArray() {
// TODO: comment (or rethink) that this modifies its inputs!!
output := mv.Copy()
for i := range mv.arrayval {
output.arrayval[i] = output.arrayval[i].Arrayify()
for i := range mv.x.arrayval {
output.x.arrayval[i] = output.x.arrayval[i].Arrayify()
}
return output

Expand Down
12 changes: 10 additions & 2 deletions internal/pkg/mlrval/mlrval_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ package mlrval
func (mv *Mlrval) Copy() *Mlrval {
other := *mv
if mv.mvtype == MT_MAP {
other.mapval = mv.mapval.Copy()
other.x = &mlrvalExtended{
mapval: mv.x.mapval.Copy(),
}
} else if mv.mvtype == MT_ARRAY {
other.arrayval = CopyMlrvalArray(mv.arrayval)
other.x = &mlrvalExtended{
arrayval: CopyMlrvalArray(mv.x.arrayval),
}
} else if mv.mvtype == MT_FUNC {
other.x = &mlrvalExtended{
funcval: mv.x.funcval,
}
}
return &other
}
10 changes: 5 additions & 5 deletions internal/pkg/mlrval/mlrval_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ func (mv *Mlrval) GetBoolValue() (boolValue bool, isBool bool) {

func (mv *Mlrval) GetArray() []*Mlrval {
if mv.IsArray() {
return mv.arrayval
return mv.x.arrayval
} else {
return nil
}
}

func (mv *Mlrval) GetMap() *Mlrmap {
if mv.IsMap() {
return mv.mapval
return mv.x.mapval
} else {
return nil
}
}

func (mv *Mlrval) GetFunction() interface{} {
if mv.Type() == MT_FUNC {
return mv.funcval
return mv.x.funcval
} else {
return nil
}
Expand Down Expand Up @@ -122,12 +122,12 @@ func (mv *Mlrval) AcquireBoolValue() bool {

func (mv *Mlrval) AcquireArrayValue() []*Mlrval {
lib.InternalCodingErrorIf(mv.mvtype != MT_ARRAY)
return mv.arrayval
return mv.x.arrayval
}

func (mv *Mlrval) AcquireMapValue() *Mlrmap {
lib.InternalCodingErrorIf(mv.mvtype != MT_MAP)
return mv.mapval
return mv.x.mapval
}

func (mv *Mlrval) GetNumericToFloatValueOrDie() (floatValue float64) {
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/mlrval/mlrval_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (mv *Mlrval) marshalJSONArray(

// TODO: libify
allTerminal := true
for _, element := range mv.arrayval {
for _, element := range mv.x.arrayval {
if element.IsArrayOrMap() {
allTerminal = false
break
Expand All @@ -429,11 +429,11 @@ func (mv *Mlrval) marshalJSONArraySingleLine(
elementNestingDepth int,
outputIsStdout bool,
) (string, error) {
n := len(mv.arrayval)
n := len(mv.x.arrayval)
var buffer bytes.Buffer
buffer.WriteByte('[')

for i, element := range mv.arrayval {
for i, element := range mv.x.arrayval {
elementString, err := element.marshalJSONAux(JSON_SINGLE_LINE, elementNestingDepth+1, outputIsStdout)
if err != nil {
return "", err
Expand Down Expand Up @@ -466,7 +466,7 @@ func (mv *Mlrval) marshalJSONArrayMultipleLines(
elementNestingDepth int,
outputIsStdout bool,
) (string, error) {
n := len(mv.arrayval)
n := len(mv.x.arrayval)
var buffer bytes.Buffer

// Write empty array as '[]'
Expand All @@ -475,7 +475,7 @@ func (mv *Mlrval) marshalJSONArrayMultipleLines(
buffer.WriteByte('\n')
}

for i, element := range mv.arrayval {
for i, element := range mv.x.arrayval {
elementString, err := element.marshalJSONAux(jsonFormatting, elementNestingDepth+1, outputIsStdout)
if err != nil {
return "", err
Expand Down Expand Up @@ -508,7 +508,7 @@ func (mv *Mlrval) marshalJSONMap(
outputIsStdout bool,
) (string, error) {
lib.InternalCodingErrorIf(mv.mvtype != MT_MAP)
s, err := mv.mapval.marshalJSONAux(jsonFormatting, elementNestingDepth, outputIsStdout)
s, err := mv.x.mapval.marshalJSONAux(jsonFormatting, elementNestingDepth, outputIsStdout)
if err != nil {
return "", err
}
Expand Down
Loading