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

perf: use a reusable bytes.Buffer to speed up MutableTree.String #456

Merged

Commits on Dec 16, 2021

  1. perf: use a reusable bytes.Buffer to speed up MutableTree.String

    The prior code used a naive string concatenation
    str += "..."
    
    which is very slow and inefficient especially when iterating over
    all the keys, but also it performed an unnecessary byteslice->string
    conversion for all arguments inside (nodeDB).traverse* using
    
        str += fmt.Sprintf("%s: %x\n", string(key), value)
    
    notice the `string(key)` passed into fmt.Sprintf("%s"?
    At bare minimum that code should just simply be:
    
        str += fmt.Sprintf("%s: %x\n", key, value)
    
    per https://twitter.com/orijtech/status/1462100381803102213?s=20
    which saves a whole lot of cycles and RAM.
    
    This change uses:
    * (*bytes.Buffer) in combination with fmt.Fprintf
    * A sync.Pool with (*bytes.Buffer) values to reuse buffers
    
    and the results are profound:
    
    ```shell
    $ benchstat before.txt after.txt
    name          old time/op    new time/op    delta
    TreeString-8     111ms ± 8%       4ms ± 4%  -96.01%  (p=0.000 n=20+20)
    
    name          old alloc/op   new alloc/op   delta
    TreeString-8     734MB ± 0%       2MB ± 1%  -99.72%  (p=0.000 n=20+20)
    
    name          old allocs/op  new allocs/op  delta
    TreeString-8     37.0k ± 0%     28.7k ± 0%  -22.40%  (p=0.000 n=20+20)
    ```
    
    Fixes cosmos#455
    odeke-em committed Dec 16, 2021
    Configuration menu
    Copy the full SHA
    d2df784 View commit details
    Browse the repository at this point in the history