Skip to content

Commit 10ab525

Browse files
authored
Feat: optimizing GetExportFunctionByRVA by using a map instead of walking the array each time. (#134)
1 parent 90ab550 commit 10ab525

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

exports.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type Export struct {
8181
Functions []ExportFunction `json:"functions"`
8282
Struct ImageExportDirectory `json:"struct"`
8383
Name string `json:"name"`
84+
functionsByRVA map[uint32]int
8485
}
8586

8687
/*
@@ -313,18 +314,24 @@ func (pe *File) parseExportDirectory(rva, size uint32) error {
313314
exp.Functions = append(exp.Functions, newExport)
314315
}
315316

317+
// Populating the map linking every RVA to its index in the exported functions array.
318+
exp.functionsByRVA = make(map[uint32]int, len(exp.Functions))
319+
for i, fn := range exp.Functions {
320+
if _, ok := exp.functionsByRVA[fn.FunctionRVA]; !ok {
321+
exp.functionsByRVA[fn.FunctionRVA] = i
322+
}
323+
}
316324
pe.Export = exp
325+
317326
pe.HasExport = true
318327
return nil
319328
}
320329

321330
// GetExportFunctionByRVA return an export function given an RVA.
322331
func (pe *File) GetExportFunctionByRVA(rva uint32) ExportFunction {
323-
for _, exp := range pe.Export.Functions {
324-
if exp.FunctionRVA == rva {
325-
return exp
326-
}
327-
}
332+
if i, ok := pe.Export.functionsByRVA[rva]; ok {
333+
return pe.Export.Functions[i]
334+
}
328335

329336
return ExportFunction{}
330337
}

0 commit comments

Comments
 (0)