-
Notifications
You must be signed in to change notification settings - Fork 2
use mmap syscall instead of open to reduce baseline memory footprint on startup #14
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,13 +2,16 @@ package proxydb | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||
| "syscall" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| "github.com/etf1/ip2proxy" | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type Client struct { | ||||||||||||||||||||||||||||||||||
| dbpath string | ||||||||||||||||||||||||||||||||||
| DB *ip2proxy.DB | ||||||||||||||||||||||||||||||||||
| dbpath string | ||||||||||||||||||||||||||||||||||
| DB *ip2proxy.DB | ||||||||||||||||||||||||||||||||||
| mmapData []byte | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
11
to
15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing The Proposed fix: Add a Close method+func (c *Client) Close() error {
+ if c.mmapData != nil {
+ err := syscall.Munmap(c.mmapData)
+ c.mmapData = nil
+ c.DB = nil
+ return err
+ }
+ return nil
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type Data struct { | ||||||||||||||||||||||||||||||||||
|
|
@@ -22,16 +25,52 @@ func NewClient(dbpath string) *Client { | |||||||||||||||||||||||||||||||||
| return client | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func (c *Client) Open() error { | ||||||||||||||||||||||||||||||||||
| var err error | ||||||||||||||||||||||||||||||||||
| c.DB, err = ip2proxy.Open(c.dbpath) | ||||||||||||||||||||||||||||||||||
| func openMmap(c *Client) error { | ||||||||||||||||||||||||||||||||||
| file, err := os.Open(c.dbpath) | ||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| defer file.Close() // Safe to close the file descriptor after mapping | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| stat, err := file.Stat() | ||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Map the file directly into virtual memory (Bypassing Go's Heap) | ||||||||||||||||||||||||||||||||||
| c.mmapData, err = syscall.Mmap( | ||||||||||||||||||||||||||||||||||
| int(file.Fd()), | ||||||||||||||||||||||||||||||||||
| 0, | ||||||||||||||||||||||||||||||||||
| int(stat.Size()), | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential integer overflow on 32-bit systems.
Proposed fix: Add size validation+ size := stat.Size()
+ if size > int64(^uint(0)>>1) { // max int
+ return errors.New("file too large to mmap on this platform")
+ }
+
// Map the file directly into virtual memory (Bypassing Go's Heap)
c.mmapData, err = syscall.Mmap(
int(file.Fd()),
0,
- int(stat.Size()),
+ int(size),
syscall.PROT_READ, // Read-only memory
syscall.MAP_SHARED, // Share with OS page cache
)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| syscall.PROT_READ, // Read-only memory | ||||||||||||||||||||||||||||||||||
| syscall.MAP_SHARED, // Share with OS page cache | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| c.DB, err = ip2proxy.FromBytes(c.mmapData) | ||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||
| c.DB = nil | ||||||||||||||||||||||||||||||||||
| // Clean up the OS memory if parsing fails | ||||||||||||||||||||||||||||||||||
| syscall.Munmap(c.mmapData) | ||||||||||||||||||||||||||||||||||
| c.mmapData = nil | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func (c *Client) Open() error { | ||||||||||||||||||||||||||||||||||
| err := openMmap(c) | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| // var err error | ||||||||||||||||||||||||||||||||||
| // c.DB, err = ip2proxy.Open(c.dbpath) | ||||||||||||||||||||||||||||||||||
| // if err != nil { | ||||||||||||||||||||||||||||||||||
| // c.DB = nil | ||||||||||||||||||||||||||||||||||
| // return err | ||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||
| // return nil | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func (c *Client) proxyName(proxyType ip2proxy.ProxyType) string { | ||||||||||||||||||||||||||||||||||
| if proxyType == ip2proxy.ProxyVPN { | ||||||||||||||||||||||||||||||||||
| return "vpn" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 3814
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 87
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 1091
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 193
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 151
🏁 Script executed:
Repository: CloudStuffTech/go-utils
Length of output: 85
Platform portability:
syscall.Mmapandsyscall.Munmapare Unix-specific and will fail on Windows.The code directly uses
syscall.Mmapandsyscall.Munmap(lines 41 and 55) without any build constraints or platform-specific handling. These functions are unavailable on Windows and will cause compilation failures. If cross-platform support is required, usegolang.org/x/exp/mmaporgithub.com/edsrzf/mmap-goto provide a portable abstraction.🤖 Prompt for AI Agents