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

refactor(rubygems): Extract copystring function to the utils #22322

Merged
merged 2 commits into from May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 3 additions & 24 deletions lib/modules/datasource/rubygems/versions-datasource.ts
Expand Up @@ -6,6 +6,7 @@ import { getElapsedMinutes } from '../../../util/date';
import { HttpError } from '../../../util/http';
import { newlineRegex } from '../../../util/regex';
import { LooseArray } from '../../../util/schema-utils';
import { copystr } from '../../../util/string';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';

Expand Down Expand Up @@ -51,8 +52,6 @@ const Lines = z
type Lines = z.infer<typeof Lines>;

export class VersionsDatasource extends Datasource {
private isInitialFetch = true;

constructor(override readonly id: string) {
super(id);
}
Expand Down Expand Up @@ -97,31 +96,12 @@ export class VersionsDatasource extends Datasource {
return { releases };
}

/**
* Since each `/versions` reponse exceed 10MB,
* there is potential for a memory leak if we construct slices
* of the response body and cache them long-term:
*
* https://bugs.chromium.org/p/v8/issues/detail?id=2869
*
* This method meant to be called for `version` and `packageName`
* before storing them in the cache.
*/
private copystr(x: string): string {
const len = Buffer.byteLength(x, 'utf8');
const buf = this.isInitialFetch
? Buffer.allocUnsafe(len) // allocate from pre-allocated buffer
: Buffer.allocUnsafeSlow(len); // allocate standalone buffer
buf.write(x, 'utf8');
return buf.toString('utf8');
}

private updatePackageReleases(
packageReleases: PackageReleases,
lines: Lines
): void {
for (const line of lines) {
const packageName = this.copystr(line.packageName);
const packageName = copystr(line.packageName);
let versions = packageReleases.get(packageName) ?? [];

const { deletedVersions, addedVersions } = line;
Expand All @@ -134,7 +114,7 @@ export class VersionsDatasource extends Datasource {
const existingVersions = new Set(versions);
for (const addedVersion of addedVersions) {
if (!existingVersions.has(addedVersion)) {
const version = this.copystr(addedVersion);
const version = copystr(addedVersion);
versions.push(version);
}
}
Expand Down Expand Up @@ -183,7 +163,6 @@ export class VersionsDatasource extends Datasource {

const lines = Lines.parse(newLines);
this.updatePackageReleases(regCache.packageReleases, lines);
this.isInitialFetch = false;
}

private updateRubyGemsVersionsPromise: Promise<void> | null = null;
Expand Down
15 changes: 15 additions & 0 deletions lib/util/string.ts
Expand Up @@ -67,3 +67,18 @@ export function titleCase(input: string): string {

return words.join(' ');
}

/**
* Sometimes we extract small strings from a multi-megabyte files.
* If we then save them in the in-memory cache, V8 may not free
* the initial buffer, which can lead to memory leaks:
*
* https://bugs.chromium.org/p/v8/issues/detail?id=2869
*
*/
export function copystr(x: string): string {
const len = Buffer.byteLength(x, 'utf8');
const buf = Buffer.allocUnsafeSlow(len);
buf.write(x, 'utf8');
return buf.toString('utf8');
}