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

Cache file system operations #803

Merged
merged 3 commits into from
Apr 15, 2019

Conversation

ksamborski
Copy link
Contributor

@ksamborski ksamborski commented Mar 22, 2019

Hello!

I'm using ts-node within docker with sources and node_modules mounted as volumes. I noticed that it's a bit slower than running it directly on host. That's on linux but on mac it's drastically worse so I investigated the issue a little bit more.

First thing to do was to enable debug mode via TS_NODE_DEBUG env variable. It showed multiple fileExists and directoryExists operations. Those operations are more costly when using in docker mounted volumes. And much more costly when using it on mac. But when I sorted the output I was able to see that many of those operations are repeated several times for the same file/directories, for example:

ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableIndex.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/Table.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.ts
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/schema-builder/table/TableUnique.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/BroadcasterResult.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/BroadcasterResult.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/BroadcasterResult.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/Broadcaster.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/EntitySubscriberInterface.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/InsertEvent.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/RemoveEvent.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.d.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.ts
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.tsx
ts-node fileExists /home/node/app/node_modules/typeorm/subscriber/event/UpdateEvent.tsx

So I started to play with it a little bit. I thought I could cache the results of those functions and I wrote a simple cached function to do it. Here are benchmarks for my project (on linux):

  1. without caching (plain ts-node 8.0.3):
    docker-compose down
    time docker-compose up app > /dev/null
    docker-compose up app > /dev/null
    2,30s user 0,39s system 20% cpu 13,091 total
    
  2. with caching:
    docker-compose down
    time docker-compose up app > /dev/null
    docker-compose up app > /dev/null
    1,22s user 0,24s system 11% cpu 12,207 total
    

I run it multiple times. docker-compose down is needed to actually remove existing containers in order to get rid of any temporary files. As you can see both total time and time spent in system calls was decreased. When using on mac it can save you literally minutes and don't force you to use solutions like docker-sync.

@coveralls
Copy link

coveralls commented Mar 22, 2019

Coverage Status

Coverage decreased (-0.5%) to 85.536% when pulling 42bfc9b on ksamborski:feature/file-exists-cache into 8706c31 on TypeStrong:master.

@blakeembrey blakeembrey merged commit 66d54af into TypeStrong:master Apr 15, 2019
@blakeembrey
Copy link
Member

@ksamborski I'm not 100% sure this is a good idea, but it does seem more benefit than drawback. The major thing I've tried to avoid is caching too heavily against possible file system changes in a long running process.

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.

3 participants