Skip to content

Commit

Permalink
use xxHash instead of SHA1 for speed (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw authored Apr 3, 2021
1 parent 7c156df commit ac54d0f
Show file tree
Hide file tree
Showing 11 changed files with 466 additions and 133 deletions.
11 changes: 6 additions & 5 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundler

import (
"bytes"
"crypto/sha1"
"encoding/base32"
"encoding/base64"
"fmt"
Expand Down Expand Up @@ -31,6 +30,7 @@ import (
"github.com/evanw/esbuild/internal/resolver"
"github.com/evanw/esbuild/internal/runtime"
"github.com/evanw/esbuild/internal/sourcemap"
"github.com/evanw/esbuild/internal/xxhash"
)

type entryPointKind uint8
Expand Down Expand Up @@ -342,8 +342,9 @@ func parseFile(args parseArgs) {
// but different contents from colliding
var hash string
if config.HasPlaceholder(args.options.AssetPathTemplate, config.HashPlaceholder) {
hashBytes := sha1.Sum([]byte(source.Contents))
hash = hashForFileName(hashBytes)
h := xxhash.New()
h.Write([]byte(source.Contents))
hash = hashForFileName(h.Sum(nil))
}
dir := "/"
relPath := config.TemplateToString(config.SubstituteTemplate(args.options.AssetPathTemplate, config.PathPlaceholders{
Expand Down Expand Up @@ -967,8 +968,8 @@ func lowerCaseAbsPathForWindows(absPath string) string {
return strings.ToLower(absPath)
}

func hashForFileName(hashBytes [sha1.Size]byte) string {
return base32.StdEncoding.EncodeToString(hashBytes[:])[:8]
func hashForFileName(hashBytes []byte) string {
return base32.StdEncoding.EncodeToString(hashBytes)[:8]
}

type scanner struct {
Expand Down
10 changes: 5 additions & 5 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundler

import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/evanw/esbuild/internal/resolver"
"github.com/evanw/esbuild/internal/runtime"
"github.com/evanw/esbuild/internal/sourcemap"
"github.com/evanw/esbuild/internal/xxhash"
)

type bitSet struct {
Expand Down Expand Up @@ -641,17 +641,17 @@ func (c *linkerContext) generateChunksInParallel(chunks []chunkInfo) []OutputFil
// parallel but it probably doesn't matter so much because we're not hashing
// that much data.
visited := make([]uint32, len(chunks))
var finalBytes [sha1.Size]byte
var finalBytes []byte
for chunkIndex := range chunks {
chunk := &chunks[chunkIndex]
var hashSubstitution *string

// Only wait for the hash if necessary
if config.HasPlaceholder(chunk.finalTemplate, config.HashPlaceholder) {
// Compute the final hash using the isolated hashes of the dependencies
hash := sha1.New()
hash := xxhash.New()
appendIsolatedHashesForImportedChunks(hash, chunks, uint32(chunkIndex), visited, ^uint32(chunkIndex))
hash.Sum(finalBytes[:0])
finalBytes = hash.Sum(finalBytes[:0])
finalString := hashForFileName(finalBytes)
hashSubstitution = &finalString
}
Expand Down Expand Up @@ -4909,7 +4909,7 @@ func (c *linkerContext) generateIsolatedHashInParallel(chunk *chunkInfo) {
}

func (c *linkerContext) generateIsolatedHash(chunk *chunkInfo, channel chan []byte) {
hash := sha1.New()
hash := xxhash.New()

// Mix the file names and part ranges of all of the files in this chunk into
// the hash. Objects that appear identical but that live in separate files or
Expand Down
14 changes: 7 additions & 7 deletions internal/bundler/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ TestCSSAndJavaScriptCodeSplittingIssue1064
---------- /out/a.js ----------
import {
shared_default
} from "./chunk-JWQFE3AN.js";
} from "./chunk-JHSNRTG6.js";

// a.js
console.log(shared_default() + 1);

---------- /out/b.js ----------
import {
shared_default
} from "./chunk-JWQFE3AN.js";
} from "./chunk-JHSNRTG6.js";

// b.js
console.log(shared_default() + 2);

---------- /out/chunk-JWQFE3AN.js ----------
---------- /out/chunk-JHSNRTG6.js ----------
// shared.js
function shared_default() {
return 3;
Expand All @@ -53,7 +53,7 @@ body {
color: blue;
}

---------- /out/chunk-Y6KZESIP.css ----------
---------- /out/chunk-C7T4PGKL.css ----------
/* shared.css */
body {
background: black;
Expand Down Expand Up @@ -183,17 +183,17 @@ path {

================================================================================
TestFileImportURLInCSS
---------- /out/example-RPS4CMHF.data ----------
---------- /out/example-GDKWWYFY.data ----------
This is some data.
---------- /out/entry.css ----------
/* one.css */
a {
background: url(./example-RPS4CMHF.data);
background: url(./example-GDKWWYFY.data);
}

/* two.css */
b {
background: url(./example-RPS4CMHF.data);
background: url(./example-GDKWWYFY.data);
}

/* entry.css */
Expand Down
32 changes: 16 additions & 16 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1320,14 +1320,14 @@ console.log(a, b);

================================================================================
TestLoaderFileWithQueryParameter
---------- /out/file-JAWLBT6L.txt ----------
---------- /out/file-UEHVHXRQ.txt ----------
This is some text
---------- /out/entry.js ----------
// file.txt?foo
var file_default = "./file-JAWLBT6L.txt?foo";
var file_default = "./file-UEHVHXRQ.txt?foo";

// file.txt?bar
var file_default2 = "./file-JAWLBT6L.txt?bar";
var file_default2 = "./file-UEHVHXRQ.txt?bar";

// entry.js
console.log(file_default, file_default2);
Expand Down Expand Up @@ -2650,26 +2650,26 @@ var import_es6_ns_export_abstract_class = __toModule(require_es6_ns_export_abstr
TestTopLevelAwaitAllowedImportWithSplitting
---------- /out/entry.js ----------
// entry.js
import("./a-4AEJWCHS.js");
import("./b-6EB7BLIM.js");
import("./c-VSFX3WL4.js");
import("./a-ZK7H2YZU.js");
import("./b-MPIVDXIR.js");
import("./c-F7O26T5D.js");
require_entry();
await 0;

---------- /out/a-4AEJWCHS.js ----------
import "./chunk-K5FAGUED.js";
import "./chunk-7Y4CCLAO.js";
---------- /out/a-ZK7H2YZU.js ----------
import "./chunk-BYXBJQAS.js";
import "./chunk-34XDKUTO.js";

---------- /out/b-6EB7BLIM.js ----------
import "./chunk-K5FAGUED.js";
import "./chunk-7Y4CCLAO.js";
---------- /out/b-MPIVDXIR.js ----------
import "./chunk-BYXBJQAS.js";
import "./chunk-34XDKUTO.js";

---------- /out/chunk-K5FAGUED.js ----------
---------- /out/chunk-BYXBJQAS.js ----------

---------- /out/c-VSFX3WL4.js ----------
import "./chunk-7Y4CCLAO.js";
---------- /out/c-F7O26T5D.js ----------
import "./chunk-34XDKUTO.js";

---------- /out/chunk-7Y4CCLAO.js ----------
---------- /out/chunk-34XDKUTO.js ----------
// c.js
await 0;

Expand Down
18 changes: 9 additions & 9 deletions internal/bundler/snapshots/snapshots_loader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,49 @@ console.log(x_url, y_default);

================================================================================
TestLoaderFile
---------- /out/test-T3K5TRK4.svg ----------
---------- /out/test-IPILGNO5.svg ----------
<svg></svg>
---------- /out/entry.js ----------
// test.svg
var require_test = __commonJS((exports, module) => {
module.exports = "./test-T3K5TRK4.svg";
module.exports = "./test-IPILGNO5.svg";
});

// entry.js
console.log(require_test());

================================================================================
TestLoaderFileCommonJSAndES6
---------- /y-SXFQX7JJ.txt ----------
---------- /y-YE5AYNFB.txt ----------
y
---------- /x-CH3K3DWF.txt ----------
---------- /x-LSAMBFUD.txt ----------
x
---------- /out.js ----------
// x.txt
var require_x = __commonJS((exports, module) => {
module.exports = "./x-CH3K3DWF.txt";
module.exports = "./x-LSAMBFUD.txt";
});

// y.txt
var y_default = "./y-SXFQX7JJ.txt";
var y_default = "./y-YE5AYNFB.txt";

// entry.js
var x_url = require_x();
console.log(x_url, y_default);

================================================================================
TestLoaderFileMultipleNoCollision
---------- /dist/test-VFFI7ZOM.txt ----------
---------- /dist/test-J7OMUXO3.txt ----------
test
---------- /dist/out.js ----------
// a/test.txt
var require_test = __commonJS((exports, module) => {
module.exports = "./test-VFFI7ZOM.txt";
module.exports = "./test-J7OMUXO3.txt";
});

// b/test.txt
var require_test2 = __commonJS((exports, module) => {
module.exports = "./test-VFFI7ZOM.txt";
module.exports = "./test-J7OMUXO3.txt";
});

// entry.js
Expand Down
Loading

0 comments on commit ac54d0f

Please sign in to comment.