Skip to content

Optimize prefetch#917

Open
MakinoharaShoko wants to merge 3 commits intodevfrom
optimize-prefetch
Open

Optimize prefetch#917
MakinoharaShoko wants to merge 3 commits intodevfrom
optimize-prefetch

Conversation

@MakinoharaShoko
Copy link
Member

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive overhaul of the game's resource prefetching system. By implementing a more sophisticated, progress-aware prefetching strategy and integrating service worker capabilities, the changes aim to drastically improve loading times and overall responsiveness. The update ensures that assets and scenes are loaded intelligently and efficiently as the player progresses, leading to a smoother and more seamless gameplay experience.

Highlights

  • Optimized Prefetching Mechanism: The prefetching logic for both assets and scenes has been significantly refactored to use a queued and throttled approach, preventing resource contention and improving loading efficiency.
  • Progress-Based Prefetching: Introduced a new module for intelligent, progress-based prefetching that analyzes upcoming sentences and sub-scenes within a lookahead window to proactively load necessary resources.
  • Service Worker Integration for Assets: Assets can now be prefetched and cached directly by the service worker, offering better control over caching, improved performance, and potential offline capabilities.
  • Line Number Tracking for Assets: The lineNumber is now propagated through the parsing pipeline, allowing assets to be associated with their exact location in the script, which is crucial for the new progress-based prefetching.
  • Improved Data Structures: The SceneManager now utilizes Set data structures for settledScenes and settledAssets, enhancing performance for checking and managing already loaded or queued resources.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@cloudflare-workers-and-pages
Copy link

Deploying webgal-dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: da58391
Status: ✅  Deploy successful!
Preview URL: https://5cf24e2b.webgal-dev.pages.dev
Branch Preview URL: https://optimize-prefetch.webgal-dev.pages.dev

View logs

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly refactors the asset and scene prefetching mechanisms. It introduces a new progressive prefetching system that queues asset and scene requests, utilizing service workers for asset prefetching and applying a line-number-based lookahead to optimize resource loading. The SceneManager now uses Set objects for settledScenes and settledAssets for improved performance. Existing scene loading and game state restoration functions have been updated to integrate with this new system, removing previous ad-hoc prefetching logic. Additionally, lineNumber is now correctly passed and utilized when parsing assets, which is essential for the new line-gated prefetching. A review comment suggested improving the assetsPrefetcher by using document.head for cleaner code and refining error handling for appendChild to ensure proper retry logic.

Comment on lines +54 to +62
const head = document.getElementsByTagName('head');
if (!head.length) {
return;
}
try {
head[0].appendChild(newLink);
} catch (e) {
logger.warn('预加载资源挂载 link 失败:', e);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This implementation can be improved in two ways:

  1. Use document.head for a more modern and direct way to access the <head> element.
  2. The try...catch block here prevents proper error handling. If appendChild fails, the error is caught and logged, but the calling function runAssetsPrefetchQueue is not notified. This prevents the logic that allows for retrying failed prefetches from running. The try...catch in runAssetsPrefetchQueue is sufficient to handle this.

By removing the local try...catch and using document.head, the code becomes cleaner and more robust.

  const head = document.head;
  if (!head) {
    return;
  }
  head.appendChild(newLink);

* 清理 <head> 中的 prefetch link 元素,在切换场景时调用以避免累积。
*/
export const clearPrefetchLinks = () => {
const head = document.getElementsByTagName('head')[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability and to use a more modern API, you can use document.head directly instead of document.getElementsByTagName('head')[0].

Suggested change
const head = document.getElementsByTagName('head')[0];
const head = document.head;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant