I was able to convert the fixed package to use generics to allow a variable 
number of decimal places.

benchmark                     old ns/op     new ns/op     delta
BenchmarkAddFixed-8           0.98          0.96          -1.66%
BenchmarkMulFixed-8           5.44          22.8          +318.15%
BenchmarkDivFixed-8           3.71          12.9          +246.97%
BenchmarkCmpFixed-8           0.26          0.54          +108.74%
BenchmarkStringFixed-8        51.8          55.2          +6.44%
BenchmarkStringNFixed-8       51.9          55.8          +7.51%

I assume the decrease in performance is due to the methods calls to determine 
the number of places, scaling factor etc.

Even though the method calls return constants, it doesn’t seem the compiler is 
inlining the constant. I suspect because I needed to declare the struct at

type FixedN[T Places] struct {
     places T
     fp int64
}

and it is determining that places is not a constant.

I think if the original generics design was implemented, so:

type FixedN[T Places] struct {
     T
     fp int64
}

then it would be able to determine that the implementation is constant for the 
type.

Although to be honest, I am uncertain how the struct size is 8 in the first 
case - because then the compiler must be able to determine that places is a 
constant.

The full code is available here - 
https://github.com/robaho/fixed/blob/generics/fixed.go - maybe I am doing 
something wrong?

Thoughts?


> On Oct 16, 2024, at 10:29 AM, Ian Lance Taylor <i...@golang.org> wrote:
> 
> On Wed, Oct 16, 2024 at 6:24 AM robert engels <rob...@me.com> wrote:
>> 
>> Thanks. Is there something in the language spec that covers this ? The 
>> design doc is the only thing that appears in a Google search.
> 
> I have to admit that I don't see it in the current spec.
> 
> Ian
> 
>>> On Oct 16, 2024, at 8:09 AM, Ian Lance Taylor <i...@golang.org> wrote:
>>> 
>>> On Tue, Oct 15, 2024 at 10:18 PM Robert Engels <rob...@me.com> wrote:
>>>> 
>>>> Hmm, I thought I had it “working” using this code 
>>>> https://go.dev/play/p/pzKacmi3aPe but there is a compiler error, but 
>>>> according to the accepted proposal documentation this should be valid 
>>>> https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#embedded-type-parameter-methods
>>>> 
>>>> Is this a compiler bug?
>>> 
>>> No, we decided not to implement that feature.  At least not yet.  See
>>> https://go.dev/issue/43621.
>>> 
>>> Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/A64CC382-3652-4377-9A7A-E06BF5B5460E%40me.com.

Reply via email to