1. 16 Aug, 2021 1 commit
  2. 29 Jul, 2021 1 commit
  3. 09 Apr, 2021 1 commit
  4. 07 Apr, 2021 1 commit
    • Daniel Martí's avatar
      schema/gen/go: avoid Maybe pointers for small types · f3d42e04
      Daniel Martí authored
      If we know that a schema type can be represented in Go with a small
      amount of bytes, using a pointer to store its "maybe" is rarely a good
      idea. For example, an optional string only weighs twice as much as a
      pointer, so a pointer adds overhead and will barely ever save any
      memory.
      
      Add a function to work out the byte size of a schema.TypeKind, relying
      on reflection and the basicnode package. Debug prints are also present
      if one wants to double-check the numbers. As of today, they are:
      
      	sizeOf(small): 32 (4x pointer size)
      	sizeOf(Bool): 1
      	sizeOf(Int): 8
      	sizeOf(Float): 8
      	sizeOf(String): 16
      	sizeOf(Bytes): 24
      	sizeOf(List): 24
      	sizeOf(Map): 32
      	sizeOf(Link): 16
      
      Below is the result on go-merkledag's BenchmarkRoundtrip after
      re-generating go-codec-dagpb with this change. Note that the dag-pb
      schema contains multiple optional fields, such as strings.
      
      	name         old time/op    new time/op    delta
      	Roundtrip-8    4.24µs ± 3%    3.78µs ± 0%  -10.87%  (p=0.004 n=6+5)
      
      	name         old alloc/op   new alloc/op   delta
      	Roundtrip-8    6.38kB ± 0%    6.24kB ± 0%   -2.26%  (p=0.002 n=6+6)
      
      	name         old allocs/op  new allocs/op  delta
      	Roundtrip-8       103 ± 0%        61 ± 0%  -40.78%  (p=0.002 n=6+6)
      
      Schema typekinds which don't directly map to basicnode prototypes, such
      as structs and unions, are left as a TODO for now.
      
      I did not do any measurements to arrive at the magic number of 4x, which
      is documented in the code. We might well increase it in the future, with
      more careful benchmarking. For now, it seems like a conservative starting
      point that should cover all basic types.
      
      Finally, re-generate within this repo.
      f3d42e04
  5. 18 Jan, 2021 1 commit
  6. 10 Jan, 2021 1 commit
    • Daniel Martí's avatar
      schema/gen/go: please vet a bit more · 6796504d
      Daniel Martí authored
      In particular, this removes ~50 out of the 2.7k warnings in 'go vet
      ./...' in this repository. Mainly, the "unreachable code" ones.
      
      This was caused by edge cases in some of the generated code which caused
      an unconditional return or panic statement to be followed by other code.
      Fix all of them with a bit more template logic.
      
      Some of the Next methods go a bit further. If they serve no purpose as
      the switch has no cases to be matched, just unconditionally return an
      error. In the future we can perhaps reuse a single function for that.
      
      Finally, I was having a hard time actually following the logic in
      kindedUnionNodeAssemblerMethodTemplateMunge, so I've indented the code a
      bit to follow the template logic and scoping.
      
      These changes move us towards pleasing vet, which is nice, but also make
      the code waste a bit less space.
      6796504d
  7. 31 Dec, 2020 1 commit
  8. 25 Dec, 2020 1 commit
    • Daniel Martí's avatar
      all: rename schema.Kind to TypeKind, ipld.ReprKind to Kind · 2d7d25c4
      Daniel Martí authored
      As discussed on the issue thread, ipld.Kind and schema.TypeKind are more
      intuitive, closer to the spec wording, and just generally better in the
      long run.
      
      The changes are almost entirely automated via the commands below. Very
      minor changes were needed in some of the generators, and then gofmt.
      
      	sed -ri 's/\<Kind\(\)/TypeKind()/g' **/*.go
      	git checkout fluent # since it uses reflect.Value.Kind
      
      	sed -ri 's/\<Kind_/TypeKind_/g' **/*.go
      	sed -i 's/\<Kind\>/TypeKind/g' **/*.go
      	sed -i 's/ReprKind/Kind/g' **/*.go
      
      Plus manually undoing a few renames, as per Eric's review.
      
      Fixes #94.
      2d7d25c4
  9. 17 Dec, 2020 1 commit
    • Daniel Martí's avatar
      all: rename AssignNode to ConvertFrom · 6e6625bd
      Daniel Martí authored
      This should be more intuitive to Go programmers, since assignments are
      generally trivial operations, but conversions imply that extra work
      might be needed to adapt the value to fit in the recipient.
      
      The entire change is just:
      
      	sed -ri 's/AssignNode/ConvertFrom/g' **/*.go
      
      Downstream users can very likely use the same line to fix their function
      declarations and calls.
      
      Fixes #95.
      6e6625bd
  10. 16 Dec, 2020 1 commit
    • Daniel Martí's avatar
      all: rewrite interfaces and APIs to support int64 · f6e9a891
      Daniel Martí authored
      We only supported representing Int nodes as Go's "int" builtin type.
      This is fine on 64-bit, but on 32-bit, it limited those node values to
      just 32 bits. This is a problem in practice, because it's reasonable to
      want more than 32 bits for integers.
      
      Moreover, this meant that IPLD would change behavior if built for a
      32-bit platform; it would not be able to decode large integers, for
      example, when in fact that was just a software limitation that 64-bit
      builds did not have.
      
      To fix this problem, consistently use int64 for AsInt and AssignInt.
      
      A lot more functions are part of this rewrite as well; mainly, those
      revolving around collections and iterating. Some might never need more
      than 32 bits in practice, but consistency and portability is preferred.
      Moreover, many are interfaces, and we want IPLD interfaces to be
      flexible, which will be important for ADLs.
      
      Below are some GNU sed lines which can be used to quickly update
      function signatures to use int64:
      
      	sed -ri 's/(func.* AsInt.*)\<int\>/\1int64/g' **/*.go
      	sed -ri 's/(func.* AssignInt.*)\<int\>/\1int64/g' **/*.go
      	sed -ri 's/(func.* Length.*)\<int\>/\1int64/g' **/*.go
      	sed -ri 's/(func.* LookupByIndex.*)\<int\>/\1int64/g' **/*.go
      	sed -ri 's/(func.* Next.*)\<int\>/\1int64/g' **/*.go
      	sed -ri 's/(func.* ValuePrototype.*)\<int\>/\1int64/g' **/*.go
      
      Note that the function bodies, as well as the code that calls said
      functions, may need to be manually updated with the integer type change.
      That cannot be automated, because it's possible that an automated fix
      would silently introduce potential overflows not being handled.
      
      Some TODOs and FIXMEs for overflow checks are removed, since we remove
      some now unnecessary int64->int conversions. On the other hand, the
      older codecs based on refmt need to gain some overflow check TODOs,
      since refmt uses ints. That is okay for now, since we'll phase out refmt
      pretty soon.
      
      While at it, update codectools to use int64 for token Length fields, so
      that it properly supports full IPLD integers without machine-dependent
      behavior and overflow checks. The budget integer is also updated to be
      int64, since the lengths it uses are now int64.
      
      Note that this refactor needed changes to the Go code generator as well
      as some of the tests, for the purpose of updating all the code.
      
      Finally, note that the code-generated iterator structs do not use int64
      fields internally, even though they must return int64 numbers to
      implement the interface. This is because they use the numeric fields to
      count up to a small finite amount (such as the number of fields in a Go
      struct), or up to the length of a map/slice. Neither of them can ever
      outgrow "int".
      
      Fixes #124.
      f6e9a891
  11. 14 Dec, 2020 1 commit
  12. 04 Dec, 2020 1 commit
    • Daniel Martí's avatar
      all: fix a lot of "unkeyed literal" vet warnings · 354f194f
      Daniel Martí authored
      Reduces the output of 'go vet ./...' from 374 lines to 96. Many warnings
      remain, but I have lost my patience for today.
      
      Most of the changes below were automated, especially the single-line
      mixins expressions. Unfortunately, many of the Traits structs required
      manual copy-pasting.
      354f194f
  13. 01 Dec, 2020 1 commit
    • Daniel Martí's avatar
      node/gendemo: use the new code generator · 9fe4b32b
      Daniel Martí authored
      The files in this codegen demo correspond to an older version of the Go
      code generator. That's not terrible in itself, but it did make repeated
      uses of 'go test' fail:
      
      	$ go test
      	testing: warning: no tests to run
      	PASS
      	ok  	github.com/ipld/go-ipld-prime/node/gendemo	0.112s
      	$ go test
      	# github.com/ipld/go-ipld-prime/node/gendemo [github.com/ipld/go-ipld-prime/node/gendemo.test]
      	./minima.go:10:2: midvalue redeclared in this block
      		previous declaration at ./ipldsch_minima.go:13:27
      	[...]
      9fe4b32b