zeroshade commented on issue #2062:
URL: https://github.com/apache/iceberg-go/issues/2062#issuecomment-7224939080
## Alternative: value composite types
Before we go with copy-in/copy-out, I prototyped the option @laskoviymishka
set aside: composite types as values that wrap an unexported pointer. I ran it
against the whole repo at b9a9bd92. It closes every overwrite case raised here,
makes public reads zero-copy, and the type-switch break is mostly mechanical.
We can only make this change before the tag.
```go
type StructType struct{ p *structType } // ListType and MapType likewise
type structType struct{ fields []NestedField }
func NewStructType(fields ...NestedField) StructType
func (s StructType) Field(i int) NestedField // no copy
func (s *StructType) UnmarshalJSON(b []byte) error // replaces *s only
```
- **Immutable:** the shared data is unexported and never changes. `*p =
...`, `json.Unmarshal` and visitors can only change the caller's own copy.
- **Same `==` as today:** comparing two types compares the inner pointer.
Types stay usable as map keys, and zero values are valid empty types.
- **Unchanged APIs:** the `SchemaVisitor` signatures, `AsStruct()` and
`NestedType.Fields() []NestedField` already use values.
- **Defaults:** `NestedField` stays a plain struct. Container defaults are
copied in and out; scalar defaults cost a nil check.
- **Removed:** `FieldsRef`, `FindFieldByIDRef`, `internal.SchemaRef` and the
clone helpers. Nothing replaces them: no `init()` hooks and no visitor marker.
### Scenarios
| Scenario | `main` | Value types |
|---|---|---|
| `callZero(sc.FieldsRef)` | schema changed | method gone |
| `*inner = StructType{}` after `NewSchema` | name and ID lookups disagree |
caller's copy only |
| Visitor overwrites `f.Type` or a map default | schema changed | no effect |
| `json.Unmarshal(b, &typ)`, `typ` an `iceberg.Type` | stale ID index |
error or local copy |
| `NewSchema(fields...)`, then edit `fields` | schema renamed | schema owns
a copy |
The last two are new to this thread. The `Unmarshal` case needs no type
assertion, and the `NewSchema` aliasing is an existing bug on `main`.
### Cost
The schema has 50 top-level structs with 4 nested fields each. Each row is
n=6, compared with `benchstat`.
| | `main` | value types |
|---|---|---|
| `Fields()` | 6.44 µs, 151 allocs | 0.90 µs, 1 alloc |
| `Field(i)` over 50 fields | 5.63 µs, 150 allocs | 91 ns, 0 allocs |
| `FindFieldByID` | 135 ns, 3 allocs | 14 ns, 0 allocs |
| `AsStruct()` | 6.42 µs, 151 allocs | 1.8 ns, 0 allocs |
| `NewSchema` | 16.1 µs | 17.1 µs |
| `IndexByID` | 22.8 µs | 24.6 µs |
With copy-in/copy-out, reads stay at the `main` column. An `All()` iterator
is zero-alloc but takes 343 ns for those 50 fields, against 91 ns for a
`Field(i)` loop, because it doesn't inline. That makes it a convenience, not
the fast path.
### Migration
- **Size:** 74 files, +717/−1076. All tests pass and `go vet` is clean. An
AST rewriter and compiler-guided fixes did most of it.
- **Hazard:** `&st` still satisfies `Type` and silently misses every `case
iceberg.StructType`. A type-checked scan found two such bugs the compiler
accepted, in `table/update_schema.go` and `table/inspect/position_deletes.go`,
and it also finds `json.Unmarshal` calls with a value target. The migration PR
should run the scan once.
- **Release note:** use `iceberg.StructType`/`ListType`/`MapType` instead of
the pointer types, and don't take `&` of a type value.
### Still open in either approach
- **Shared `*Schema`:** it can still be overwritten, e.g. `json.Unmarshal`
into `Metadata.CurrentSchema()`. I'd document that rule, as `go/types` does.
- **`PartitionSpec.FieldsRef`:** it keeps the `init()` hook, as decided.
@Revanth14, this overlaps your clone/`Ref` removal PR. I can push the
prototype as a branch for you to take over or compare.
> AI-assisted, reviewed by a human.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]