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

feat: implement the solidity compilation cache #6129

Open
wants to merge 50 commits into
base: v-next
Choose a base branch
from

Conversation

galargh
Copy link
Member

@galargh galargh commented Jan 9, 2025

  • Because this PR includes a bug fix, relevant tests have been included.
  • Because this PR includes a new feature, the change was previously discussed on an Issue or with someone from the team.
  • I didn't do anything of this.

In this PR, I implement the solidity compilation cache as outlined in the associated design doc.

The implementation diverges from the design doc in 1 areas:

Copy link

changeset-bot bot commented Jan 9, 2025

⚠️ No Changeset found

Latest commit: bb1d79f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package "@nomicfoundation/ignition-core" depends on the ignored package "hardhat", but "@nomicfoundation/ignition-core" is not being ignored. Please add "@nomicfoundation/ignition-core" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "hardhat", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "@nomicfoundation/hardhat-ethers", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/hardhat-network-helpers", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/hardhat-verify", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "@nomicfoundation/hardhat-viem", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/ignition-core" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/ignition-core" is not being ignored. Please add "@nomicfoundation/ignition-core" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-ethers" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition-ethers" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-ethers" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition-viem" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition-viem" is not being ignored. Please add "@nomicfoundation/hardhat-ignition-viem" to the `ignore` option.
The package "@nomicfoundation/hardhat-ignition" depends on the ignored package "@nomicfoundation/eslint-plugin-hardhat-internal-rules", but "@nomicfoundation/hardhat-ignition" is not being ignored. Please add "@nomicfoundation/hardhat-ignition" to the `ignore` option.

Copy link

vercel bot commented Jan 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 3, 2025 9:12pm

Copy link
Contributor

github-actions bot commented Jan 10, 2025

hardhat

Total size of the bundle: 213M
Total number of dependencies (including transitive): 53

List of dependencies (sorted by size)
208M	total
29M	@ignored/edr-optimism-linux-x64-musl
29M	@ignored/edr-optimism-linux-x64-gnu
26M	@ignored/edr-optimism-linux-arm64-musl
26M	@ignored/edr-optimism-linux-arm64-gnu
22M	@ignored/edr-optimism-win32-x64-msvc
20M	esbuild
20M	@ignored/edr-optimism-darwin-x64
19M	@ignored/edr-optimism-darwin-arm64
2.8M	@sentry/tracing
2.5M	micro-eth-signer
1.9M	@noble/curves
1.7M	undici
1.2M	@sentry/types
1.2M	@noble/hashes
932K	@sentry/node
920K	@sentry/utils
856K	zod
856K	@ignored/hardhat-vnext-utils
624K	micro-packed
576K	tsx
548K	@sentry/core
544K	fast-equals
492K	@scure/bip39
460K	@ignored/edr
368K	ethereum-cryptography
344K	@sentry/hub
324K	@ignored/hardhat-vnext-errors
320K	enquirer
284K	semver
264K	@ignored/edr-optimism
192K	ws
180K	adm-zip
168K	@scure/base
136K	get-tsconfig
96K	@scure/bip32
92K	chalk
88K	tslib
88K	@sentry/minimal
76K	agent-base
72K	@nomicfoundation/solidity-analyzer
68K	debug
64K	lru_map
64K	https-proxy-agent
60K	@ignored/hardhat-vnext-zod-utils
56K	rfdc
48K	ansi-colors
40K	resolve-pkg-maps
36K	p-map
32K	cookie
24K	strip-ansi
24K	env-paths
24K	ansi-regex
20K	ms

Comment on lines +134 to +138
await Promise.all(
compilationJobs.map(async (compilationJob) =>
compilationJob.getBuildId(),
),
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing build IDs is time consuming as it involves hashing all the compiler input sources. That's why we do it asynchronously. Once a compilation job build ID is computed, it will be returned straight away on the subsequent requests.

Comment on lines +185 to +196
// NOTE: We're not waiting for the writes and clean to finish because we
// will only care about the result of these operations in subsequent runs
void Promise.all(
uncachedSuccessfulResults.map(async (result) => {
return this.#compilerOutputCache.set(
await result.compilationJob.getBuildId(),
result.compilerOutput,
);
}),
).then(() => {
return this.#compilerOutputCache.clean();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When should we clean up the cache was one of the open questions stated in the design doc.

To do so from the cleanArtifacts method was suggested. I do like this suggestion as then all the "clean" operations would be grouped together. However, when working on the final implementation, I realised it might be more beneficial to call the cache clean from build after all.

As you can see in the snippet, this allows us to ensure that clean is only ever called after all the writes have finished. That way we can avoid (or at least postpone) implementing synchronisation mechanisms between writes and cleans in the cache component.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

This is the implementation of the caching component.

It is expected to support caching compiler outputs to begin with. We can extend/change it as needed in the future.

Comment on lines +22 to +23
defaultMaxAgeMs: number = 7 * 24 * 60 * 60 * 1000, // 1 week
defaultMaxSize: number = 2 * 1024 * 1024 * 1024, // 2 GB
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the default values we agreed upon here. They are not exposed to the user at the moment.

return (await exists(filePath)) ? readJsonFile<T>(filePath) : undefined;
}

public async clean(maxAgeMs?: number, maxSize?: number): Promise<void> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being able to override the default max age and size is really useful for testing purposes.

Comment on lines +63 to +70
for (const fileInfo of sortedFileInfos) {
if (fileInfo.atimeMs < minAtimeMs || size > maxSize) {
filesToRemove.push(fileInfo.file);
size -= fileInfo.size;
} else {
break;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We remove the files starting from the front of the list (the "oldest" entries) until the total size of the cache is smaller than the maximum cache size and the oldest entry is newer than the maximum age of a cache entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

The changes in this file are concerned with the getBuildId optimisations. They are related to the effort to make the getBuildId asynchronous and to cache resolved file content hashes.

Comment on lines +24 to +25
#solcInputWithoutSources: Omit<CompilerInput, "sources"> | undefined;
#resolvedFiles: ResolvedFile[] | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding these two new properties on the compilation job, we ensure they are only ever computed once per compilation job.

Comment on lines +173 to +175
const sortedSources = Object.fromEntries(
Object.entries(sources).sort((a, b) => a[0].localeCompare(b[0])),
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to sort the sources because the sources map might be populated out of order which does affect serialisation.

buildId: string;
contractArtifactsGenerated: string[];
warnings: CompilerOutputError[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're storing the entire compiler outputs in the cache, we're able to extra warnings out of them even when they're retrieved from cache.

@galargh galargh changed the title [WIP: not ready for review] build system cache exploration feat: implement the solidity compilation cache Jan 30, 2025
Comment on lines +91 to +92
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We don't care about hooks in this context
hooks.setContext({} as HookContext);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we call build() it is asserted that the hook context is not undefined. We don't actually use hooks in this tests.

A proper HookContext contains hre. I didn't want to create a full hre for this test. Hence, the type cast.

@galargh galargh requested a review from alcuadrado January 30, 2025 12:59
@galargh galargh marked this pull request as ready for review January 30, 2025 12:59
Comment on lines +29 to 36
export async function createNonCryptographicHashId(
data: string,
): Promise<string> {
const message = new TextEncoder().encode(data);
const buffer = await crypto.subtle.digest("SHA-1", message);
const array = Array.from(new Uint8Array(buffer));
return array.map((b) => b.toString(16).padStart(2, "0")).join("");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

The changes to this file are related to the getBuildId optimisations.

After the changes, createNonCryptographicHashId is an asynchronous function. I also changed the hashing algorithm from MD5 to SHA-1. This is OK because the only place where we currently use this function is the getBuildId method of the CompilationJobImplementation.

Comment on lines +418 to +430
export async function getAccessTime(absolutePath: string): Promise<Date> {
try {
const stats = await fsPromises.stat(absolutePath);
return stats.atime;
} catch (e) {
ensureError<NodeJS.ErrnoException>(e);
if (e.code === "ENOENT") {
throw new FileNotFoundError(absolutePath, e);
}

throw new FileSystemAccessError(e.message, e);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

Here, I add two new helper functions, getAccessTime and getSize which return last access time and size of a file respectively.

They are needed to implement the clean part of the caching component which removes cache entries based on file size and "age".

@@ -104,9 +104,9 @@ declare module "@ignored/hardhat-vnext/types/artifacts" {
}`;
}

export function getBuildInfo(
export async function getBuildInfo(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

It is necessary to change the signatures of getBuildInfo and getBuildInfoOutput because they both use getBuildId which turned asynchronous as part of the getBuildId optimisations.


import { ResolvedFileType } from "../../../../types/solidity.js";

export class ProjectResolvedFileImplementation implements ProjectResolvedFile {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

I add implementations of ProjectResolvedFile and NpmPackageResolvedFile interfaces here. These are necessary in order to lazily store content hashes on them, as proposed as part of the getBuildId optimisations.

@@ -35,14 +35,15 @@ export interface ResolvedNpmPackage {
*/
export enum ResolvedFileType {
PROJECT_FILE = "PROJECT_FILE",
NPM_PACKGE_FILE = "NPM_PACKAGE_FILE",
NPM_PACKAGE_FILE = "NPM_PACKAGE_FILE",
}

/**
* A file that's part of the Hardhat project (i.e. not installed through npm).
*/
export interface ProjectResolvedFile {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context

I add getContentHash() methods on the ProjectResolvedFile and NpmPackageResolvedFile interfaces here. These functions are needed to implement the getBuildId optimisations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

Compilation caching in the local file system
2 participants