Files
wuffs/lang/check/optimize.go
T
Nigel Tao b61a827354 Optimize std/adler32 for SSE4.2
On a mid-range x86_64 laptop:

wuffs_adler32_10k/clang9                                  1.35GB/s ± 0%  10.78GB/s ± 1%  +700.65%  (p=0.008 n=5+5)
wuffs_adler32_100k/clang9                                 1.35GB/s ± 0%  12.88GB/s ± 0%  +856.09%  (p=0.008 n=5+5)

wuffs_adler32_10k/gcc10                                   1.85GB/s ± 0%  11.21GB/s ± 0%  +506.34%  (p=0.008 n=5+5)
wuffs_adler32_100k/gcc10                                  1.84GB/s ± 0%  11.43GB/s ± 0%  +521.51%  (p=0.008 n=5+5)

mimic_adler32_10k                                         1.76GB/s ± 0%   1.76GB/s ± 0%      ~     (p=0.841 n=5+5)
mimic_adler32_100k                                        1.72GB/s ± 0%   1.72GB/s ± 0%      ~     (p=0.222 n=5+5)

wuffs_zlib_decode_10k/clang9                               124MB/s ± 0%    139MB/s ± 0%   +12.29%  (p=0.016 n=4+5)
wuffs_zlib_decode_100k/clang9                              148MB/s ± 0%    155MB/s ± 0%    +4.21%  (p=0.008 n=5+5)

wuffs_zlib_decode_10k/gcc10                                133MB/s ± 1%    141MB/s ± 0%    +5.75%  (p=0.008 n=5+5)
wuffs_zlib_decode_100k/gcc10                               159MB/s ± 0%    172MB/s ± 0%    +7.77%  (p=0.016 n=5+4)

mimic_zlib_decode_10k                                      133MB/s ± 0%    133MB/s ± 0%    +0.38%  (p=0.008 n=5+5)
mimic_zlib_decode_100k                                     160MB/s ± 0%    160MB/s ± 0%      ~     (p=0.841 n=5+5)

wuffs_png_decode_image_19k_8bpp/clang9                    90.6MB/s ± 0%   94.7MB/s ± 0%    +4.58%  (p=0.016 n=5+4)
wuffs_png_decode_image_40k_24bpp/clang9                    129MB/s ± 0%    135MB/s ± 0%    +4.49%  (p=0.008 n=5+5)
wuffs_png_decode_image_77k_8bpp/clang9                     335MB/s ± 0%    351MB/s ± 0%    +4.62%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_ignore_checksum/clang9   322MB/s ± 0%    320MB/s ± 1%    -0.71%  (p=0.016 n=4+5)
wuffs_png_decode_image_552k_32bpp_verify_checksum/clang9   256MB/s ± 0%    308MB/s ± 0%   +20.24%  (p=0.008 n=5+5)
wuffs_png_decode_image_4002k_24bpp/clang9                  131MB/s ± 0%    138MB/s ± 0%    +5.24%  (p=0.008 n=5+5)

wuffs_png_decode_image_19k_8bpp/gcc10                     95.1MB/s ± 0%  100.8MB/s ± 1%    +6.08%  (p=0.008 n=5+5)
wuffs_png_decode_image_40k_24bpp/gcc10                     131MB/s ± 0%    136MB/s ± 0%    +3.65%  (p=0.008 n=5+5)
wuffs_png_decode_image_77k_8bpp/gcc10                      350MB/s ± 1%    371MB/s ± 0%    +6.06%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_ignore_checksum/gcc10    327MB/s ± 0%    333MB/s ± 0%    +1.66%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_verify_checksum/gcc10    274MB/s ± 0%    318MB/s ± 0%   +15.93%  (p=0.008 n=5+5)
wuffs_png_decode_image_4002k_24bpp/gcc10                   132MB/s ± 0%    138MB/s ± 0%    +4.73%  (p=0.008 n=5+5)

mimic_png_decode_image_19k_8bpp                           58.1MB/s ± 0%   58.2MB/s ± 0%    +0.27%  (p=0.008 n=5+5)
mimic_png_decode_image_40k_24bpp                          73.0MB/s ± 1%   73.3MB/s ± 0%    +0.45%  (p=0.008 n=5+5)
mimic_png_decode_image_77k_8bpp                            177MB/s ± 0%    177MB/s ± 0%      ~     (p=0.421 n=5+5)
mimic_png_decode_image_552k_32bpp_ignore_checksum               skipped
mimic_png_decode_image_552k_32bpp_verify_checksum          146MB/s ± 0%    146MB/s ± 0%      ~     (p=0.310 n=5+5)
mimic_png_decode_image_4002k_24bpp                         104MB/s ± 0%    104MB/s ± 0%      ~     (p=0.056 n=5+5)
2021-01-26 22:31:54 +11:00

187 lines
5.2 KiB
Go

// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package check
// TODO: should bounds checking even be responsible for selecting between
// optimized implementations of e.g. read_u8? Instead, the Wuffs code could
// explicitly call either read_u8 or read_u8_fast, with the latter having
// stronger preconditions.
//
// Doing so might need some syntactical distinction (not just a question mark)
// between "foo?" and "bar?" if one of those methods can still return an error
// code but never actually suspend.
import (
"math/big"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
// splitReceiverMethodArgs returns the "receiver", "method" and "args" in the
// expression "receiver.method(args)".
func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.ID, args []*a.Node) {
if n.Operator() != t.IDOpenParen {
return nil, 0, nil
}
args = n.Args()
n = n.LHS().AsExpr()
if n.Operator() != t.IDDot {
return nil, 0, nil
}
return n.LHS().AsExpr(), n.Ident(), args
}
func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, advanceExpr *a.Expr, update bool) (retOK bool, retErr error) {
// TODO: do two passes? The first one to non-destructively check retOK. The
// second one to drop facts if indeed retOK? Otherwise, an advance
// precondition failure will lose some of the facts in its error message.
if advanceExpr != nil {
return q.optimizeIOMethodAdvanceExpr(receiver, advanceExpr, update)
}
// Check if receiver looks like "a[i .. j]" where i and j are constants and
// ((j - i) >= advance).
if receiver.Operator() == t.IDDotDot {
icv := (*big.Int)(nil)
if i := receiver.MHS().AsExpr(); i == nil {
icv = zero
} else if i.ConstValue() != nil {
icv = i.ConstValue()
}
jcv := (*big.Int)(nil)
if j := receiver.RHS().AsExpr(); (j != nil) && (j.ConstValue() != nil) {
jcv = j.ConstValue()
}
if (icv != nil) && (jcv != nil) {
n := big.NewInt(0).Sub(jcv, icv)
if n.Cmp(advance) >= 0 {
retOK = true
}
}
}
retErr = q.facts.update(func(x *a.Expr) (*a.Expr, error) {
// TODO: update (discard?) any facts that merely mention
// receiver.length(), even if they aren't an exact match.
op := x.Operator()
if (op != t.IDXBinaryGreaterEq) &&
(op != t.IDXBinaryGreaterThan) &&
(op != t.IDXBinaryEqEq) {
return x, nil
}
rcv := x.RHS().AsExpr().ConstValue()
if rcv == nil {
return x, nil
}
// Check that lhs is "receiver.length()".
lhs := x.LHS().AsExpr()
if lhs.Operator() != t.IDOpenParen || len(lhs.Args()) != 0 {
return x, nil
}
lhs = lhs.LHS().AsExpr()
if lhs.Operator() != t.IDDot || lhs.Ident() != t.IDLength {
return x, nil
}
lhs = lhs.LHS().AsExpr()
if !lhs.Eq(receiver) {
return x, nil
}
// Check if the bytes available is >= the bytes needed. If so, update
// rcv to be the bytes remaining. If not, discard the fact x.
if op == t.IDXBinaryGreaterThan {
op = t.IDXBinaryGreaterEq
rcv = big.NewInt(0).Add(rcv, one)
}
if rcv.Cmp(advance) < 0 {
if !update {
return x, nil
}
return nil, nil
}
retOK = true
if !update {
return x, nil
}
if rcv.Cmp(advance) == 0 {
// TODO: delete the (adjusted) fact, as newRCV will be zero, and
// "foo.length() >= 0" is redundant.
}
// Create a new a.Expr to hold the adjusted RHS constant value, newRCV.
newRCV := big.NewInt(0).Sub(rcv, advance)
o, err := makeConstValueExpr(q.tm, newRCV)
if err != nil {
return nil, err
}
return a.NewExpr(x.AsNode().AsRaw().Flags(),
t.IDXBinaryGreaterEq, 0, x.LHS(), nil, o.AsNode(), nil), nil
})
return retOK, retErr
}
func (q *checker) optimizeIOMethodAdvanceExpr(receiver *a.Expr, advanceExpr *a.Expr, update bool) (retOK bool, retErr error) {
retErr = q.facts.update(func(x *a.Expr) (*a.Expr, error) {
// TODO: update (discard?) any facts that merely mention
// receiver.length(), even if they aren't an exact match.
op := x.Operator()
if op != t.IDXBinaryGreaterEq && op != t.IDXBinaryGreaterThan {
return x, nil
}
// Check that lhs is "receiver.length()".
lhs := x.LHS().AsExpr()
if lhs.Operator() != t.IDOpenParen || len(lhs.Args()) != 0 {
return x, nil
}
lhs = lhs.LHS().AsExpr()
if lhs.Operator() != t.IDDot || lhs.Ident() != t.IDLength {
return x, nil
}
lhs = lhs.LHS().AsExpr()
if !lhs.Eq(receiver) {
return x, nil
}
// Check that rhs is "advanceExpr as base.u64".
rhs := x.RHS().AsExpr()
if rhs.Operator() != t.IDXBinaryAs ||
!rhs.LHS().AsExpr().Eq(advanceExpr) ||
!rhs.RHS().AsTypeExpr().Eq(typeExprU64) {
return x, nil
}
retOK = true
if !update {
return x, nil
}
return nil, nil
})
return retOK, retErr
}