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

Improve build time #823

Closed
razonyang opened this issue May 14, 2024 · 10 comments · Fixed by #856
Closed

Improve build time #823

razonyang opened this issue May 14, 2024 · 10 comments · Fixed by #856

Comments

@razonyang
Copy link
Member

No description provided.

@razonyang
Copy link
Member Author

@TetraTheta made some minor improvement yesterday, now it is 1s faster than before on your site, takes 4s in total on my WSL2, didn't test on Windows, please upgrade modules via hugo mod get -u.

image

@TetraTheta
Copy link
Contributor

I can confirm that overall (re)build time decreased on my machine.
I didn't moved to Dev Drive or WSL2 yet1, so I'm working on Windows, SSD. But initial build time decreased to 5.7 seconds, and rebuild time decreased from 18 seconds to 12 seconds.

Thanks for your heavy effort! I really appreciate it. You really saved my time :)

Footnotes

  1. WSL2 isn't compatible with software I use. Dev Drive doesn't mount automatically when reboot.

@razonyang
Copy link
Member Author

razonyang commented Jun 2, 2024

I made some improvement an hr ago, you can update modules one more time if you havent upgrade yet.

image

Btw, what does rebuild time mean? I'm confused about why it took more time than initial build time.

@TetraTheta
Copy link
Contributor

Every modules are up to date at the time I write this comment.

Hugo rebuilds site when any 'source file' of site is changed, and it takes some time.

Change detected, rebuilding site (#1).
2024-06-04 04:xx:xx.xxx +0900
Source changed /b/...
Web Server is available at https://localhost:80/ (bind address 127.0.0.1)
Total in 12750 ms

That's what I call 'rebuild time'.

I don't know why 'rebuild time' takes more time than 'initial build time'. The script I use for running test server is like this:

[console]::WindowWidth=120
[console]::WindowHeight=30
[console]::BufferWidth=[console]::WindowWidth

# Prevent Quick Edit
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class Win32 {
  [DllImport("kernel32.dll", SetLastError = true)]
  public static extern IntPtr GetStdHandle(int nStdHandle);
  [DllImport("kernel32.dll", SetLastError = true)]
  public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
  [DllImport("kernel32.dll", SetLastError = true)]
  public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
"@

$consoleHandle = [Win32]::GetStdHandle(-10) # STD_INPUT_HANDLE: -10
$currentMode = 0
[Win32]::GetConsoleMode($consoleHandle, [ref]$currentMode) | Out-Null
$newMode = ($currentMode -band -bnot 0x0040) -bor 0x0080 # ENABLE_QUICK_EDIT_MODE: 0x0040 | ENABLE_EXTENDED_FLAGS: 0x0080
[Win32]::SetConsoleMode($consoleHandle, $newMode) | Out-Null

# Set Title
$host.ui.RawUI.WindowTitle = "TetraLog Test Server"

Set-Location -LiteralPath (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path))
Remove-Item "public" -Recurse -ErrorAction "Ignore"
Remove-Item "resources" -Recurse -ErrorAction "Ignore"
# Fast Render: Home page + Content page last edited (if any) + Content page last visited (up to 10)
hugo server --renderToMemory --printMemoryUsage --disableFastRender -D -p 80

Oops, I just found that -renderToMemory was used so far, so I changed it to --renderToMemory and retried.
Now rebuild time become 6.7 seconds. But this still doesn't tell me exactly why it was 12.7 seconds before. Maybe it was slow because Hugo tried to write to SSD instead of RAM.

@razonyang
Copy link
Member Author

razonyang commented Jun 2, 2024

No idea, but you can remove the --disableFastRender for speed up re-rendering (about to 1s).

--disableFastRender enables full re-renders on changes, remove it to render the changes only.

@TetraTheta
Copy link
Contributor

In my case, 'Fast Render' isn't fast. Same script without --disableFastRender takes 25.6 seconds to rebuild. I really don't know why this happens to me, but current script is fastest configuration I can have.

@razonyang
Copy link
Member Author

Same script without --disableFastRender takes 25.6 seconds to rebuild

Seems weird, fast render should be much more faster than re-render fully, you can use hugo command (hugo server --renderToMemory --printMemoryUsage -D -p 80) directly other than PS script to check if it's caused by the script.

@TetraTheta
Copy link
Contributor

TetraTheta commented Jun 4, 2024

Running only Hugo commands without --disableFastRender makes rebuild faster (about 1 sec). This is very strange behavior because other parts of the script are just these:

  • Set console window title via PowerShell
  • Set console window size via PowerShell
  • Disable quick edit via Windows API

These should not affect Hugo's performance.

Since these are crucial to me, I'll just use --disableFastRender until I can find any alternative to set console title/size, and disable quick edit.


EDIT:
It seems rebuild time heavily increases when web browser is opened for the localhost. I tested with same script with hugo server --renderToMemory --printMemoryUsage -D -p 80, and I got two different rebuild time result. Can you re-test it while opening page with your web browser?

# Difference: adding/removing space in a file
# When web browser is not opened for localhost
Total in 2033 ms
# When web browser is opened for localhost
Total in 22082 ms

I'm using Hugo v0.126.3.

@razonyang
Copy link
Member Author

Can you re-test it while opening page with your web browser?

I always testing this with browser openning, it just took 1s to re-render (without --disableFastRender).

I have no idea on this, I'd recommend using Hugo directly, since it works expected without PS script.
You can also debug your script by disabling lines one by one to locate the cause.

@TetraTheta
Copy link
Contributor

TetraTheta commented Jun 5, 2024

I guess I have to live with current script, because it seems that what matters is Hugo itself, not other parts of the script.

I searched SO for code for disabling Quick Edit and Insert Mode and replaced my previous code that was generated with ChatGPT to it. It didn't change anything though. Other parts of the script is from SO, so I guess there is no AI-generated code problem now. In my theory, other parts of the script should not affect Hugo because they only affects PowerShell and underlying conhost.exe-related stuff via Windows API.

I also ran Hugo directly without --disableFastRender and removed only one heading in single post to see if it is actually problem from my script. It took 22 seconds to rebuild the site (again), even though Hugo was running as Fast Render Mode. I'm not sure if it only happens when first site rebuilding after reboot. It took 6.5 seconds when Fast Render Mode is disabled, but this may be because resources directory is reused.

I did similar test with script yesterday, but I couldn't conclude that 'it always takes 22 seconds to rebuild site in Fast Render Mode' at that time because Hugo sometimes rebuilt the site in 1~2 seconds in Fast Render Mode. I think when the change is post content change, rather than just single space diff, rebuild time seems to fluctuate between 1~2 seconds to 22 seconds, mostly 22 seconds and above.

not that important thing

Strange thing is, if I remember correctly, Fast Render Mode was actually faster than non-Fast Render Mode for both initial site building and rebuilding before. I can't tell which version that this weird behavior started, but I can tell it started in this year. Not very helpful information, though...

I can't tell this is Hugo's problem or my environment's problem. My Windows installation is heavily modified. But I can definitely tell that this was not a problem until some point of this year.

TL;DR
From my tests, I think running Hugo from script is fine. Site (re)build times don't show significant differences between 'Running Hugo via script' and 'Running Hugo directly'. But site rebuilding time fluctuates between '1~2 seconds' and '>= 22 seconds', mostely latter, when on Fast Render Mode. But in non-Fast Render Mode, site rebuilding time is generally '>= 6 seconds'.

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 a pull request may close this issue.

2 participants