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

Convert mlrval polymorphism from struct to unionish interface #1133

Merged
merged 4 commits into from
Nov 27, 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
4 changes: 3 additions & 1 deletion cmd/sizes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Experiments for type-inference performance optimization
// ================================================================

// go build github.com/johnkerl/miller/cmd/sizes
/*
go build github.com/johnkerl/miller/cmd/sizes
*/

package main

Expand Down
12 changes: 6 additions & 6 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.x.arrayval
array := index.intf.([]*Mlrval)
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.x.mapval
current = next.intf.(*Mlrmap)
} else {
retval = next.Copy()
}
Expand Down Expand Up @@ -782,11 +782,11 @@ func (mlrmap *Mlrmap) SortByKeyRecursively() {

for _, key := range keys {
// Old record will be GC'ed: just move pointers
value := mlrmap.Get(key)
if value.IsMap() {
value.x.mapval.SortByKeyRecursively()
val := mlrmap.Get(key)
if val.IsMap() {
val.intf.(*Mlrmap).SortByKeyRecursively()
}
other.PutReference(key, value)
other.PutReference(key, val)
}

*mlrmap = *other
Expand Down
16 changes: 9 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,14 @@ 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.x.arrayval) < 1)
arrayval := arrayOfIndices.intf.([]*Mlrval)
lib.InternalCodingErrorIf(len(arrayval) < 1)
// If the input field name was "x.a" then remember the "x".
baseIndex := arrayOfIndices.x.arrayval[0].String()
baseIndex := arrayval[0].String()
affectedBaseIndices[baseIndex] = true
// Use PutIndexed to assign $x["a"] = 7, or $x["b"] = 8, etc.
other.PutIndexed(
CopyMlrvalArray(arrayOfIndices.x.arrayval),
CopyMlrvalArray(arrayval),
unflattenTerminal(pe.Value).Copy(),
)
} else {
Expand Down Expand Up @@ -187,13 +188,14 @@ 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.x.arrayval) < 1)
arrayval := arrayOfIndices.intf.([]*Mlrval)
lib.InternalCodingErrorIf(len(arrayval) < 1)
// If the input field name was "x.a" then remember the "x".
baseIndex := arrayOfIndices.x.arrayval[0].String()
baseIndex := arrayval[0].String()
if fieldNameSet[baseIndex] {
// Use PutIndexed to assign $x["a"] = 7, or $x["b"] = 8, etc.
other.PutIndexed(
CopyMlrvalArray(arrayOfIndices.x.arrayval),
CopyMlrvalArray(arrayval),
unflattenTerminal(pe.Value).Copy(),
)
affectedBaseIndices[baseIndex] = true
Expand Down Expand Up @@ -247,7 +249,7 @@ func SplitAXHelper(input string, separator string) *Mlrval {
output := FromArray(make([]*Mlrval, len(fields)))

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

return output
Expand Down
18 changes: 9 additions & 9 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.x.arrayval), true
return len(mv.intf.([]*Mlrval)), 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.x.mapval.IsEmpty() {
if mv.intf.(*Mlrmap).IsEmpty() {
if prefix != "" {
retval.PutCopy(prefix, FromString("{}"))
}
}

for pe := mv.x.mapval.Head; pe != nil; pe = pe.Next {
for pe := mv.intf.(*Mlrmap).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.x.mapval.Head; pf != nil; pf = pf.Next {
for pf := nextResult.intf.(*Mlrmap).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.x.arrayval) == 0 {
if len(mv.intf.([]*Mlrval)) == 0 {
if prefix != "" {
retval.PutCopy(prefix, FromString("[]"))
}
}

for zindex, value := range mv.x.arrayval {
for zindex, value := range mv.intf.([]*Mlrval) {
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.x.mapval.Head; pf != nil; pf = pf.Next {
for pf := nextResult.intf.(*Mlrmap).Head; pf != nil; pf = pf.Next {
retval.PutCopy(pf.Key, pf.Value.Copy())
}
} else {
Expand All @@ -92,8 +92,8 @@ func (mv *Mlrval) FlattenToMap(prefix string, delimiter string) Mlrval {
// Increment is used by stats1.
func (mv *Mlrval) Increment() {
if mv.mvtype == MT_INT {
mv.intval++
mv.intf = mv.intf.(int64) + 1
} else if mv.mvtype == MT_FLOAT {
mv.floatval++
mv.intf = mv.intf.(float64) + 1.0
}
}
10 changes: 5 additions & 5 deletions internal/pkg/mlrval/mlrval_cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ func cmp_b_ss(input1, input2 *Mlrval) int {
return string_cmp(input1.printrep, input2.printrep)
}
func cmp_b_ii(input1, input2 *Mlrval) int {
return int_cmp(input1.intval, input2.intval)
return int_cmp(input1.intf.(int64), input2.intf.(int64))
}
func cmp_b_if(input1, input2 *Mlrval) int {
return float_cmp(float64(input1.intval), input2.floatval)
return float_cmp(float64(input1.intf.(int64)), input2.intf.(float64))
}
func cmp_b_fi(input1, input2 *Mlrval) int {
return float_cmp(input1.floatval, float64(input2.intval))
return float_cmp(input1.intf.(float64), float64(input2.intf.(int64)))
}
func cmp_b_ff(input1, input2 *Mlrval) int {
return float_cmp(input1.floatval, input2.floatval)
return float_cmp(input1.intf.(float64), input2.intf.(float64))
}
func cmp_b_bb(input1, input2 *Mlrval) int {
return int_cmp(int64(lib.BoolToInt(input1.boolval)), int64(lib.BoolToInt(input2.boolval)))
return int_cmp(int64(lib.BoolToInt(input1.intf.(bool))), int64(lib.BoolToInt(input2.intf.(bool))))
}

// TODO: cmp on array & map
Expand Down
Loading