Library directory structure in Jenkins - Time & Space Complexity
We want to understand how the time to load or access files in a Jenkins library grows as the number of files or folders increases.
How does the structure size affect the time Jenkins takes to find and use library code?
Analyze the time complexity of the following Jenkins pipeline snippet that loads a shared library and calls a function.
@Library('my-shared-lib') _
pipeline {
agent any
stages {
stage('Use Library') {
steps {
script {
myFunction()
}
}
}
}
}
This code loads a shared library named 'my-shared-lib' and calls a function from it during the pipeline run.
Look at what Jenkins does when loading the library:
- Primary operation: Searching through the library directory structure to find the requested function or script.
- How many times: Once per pipeline run, but internally Jenkins may scan multiple folders and files depending on the library size.
As the number of files and folders in the library grows, Jenkins spends more time searching for the requested code.
| Input Size (number of files/folders) | Approx. Operations (search steps) |
|---|---|
| 10 | About 10 search steps |
| 100 | About 100 search steps |
| 1000 | About 1000 search steps |
Pattern observation: The search time grows roughly in direct proportion to the number of files and folders.
Time Complexity: O(n)
This means the time Jenkins takes to find library code grows linearly with the number of files and folders in the library.
[X] Wrong: "Loading a library is instant no matter how big it is."
[OK] Correct: Jenkins must search through the directory structure, so bigger libraries take more time to load and find code.
Understanding how Jenkins handles library directories helps you explain build times and troubleshoot slow pipelines, a useful skill in real projects.
What if the library used caching to remember file locations? How would that change the time complexity?