{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/3662","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/3662/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/3662/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/3662/events","html_url":"https://github.com/Microsoft/TypeScript/issues/3662","id":91648138,"number":3662,"title":"Advanced DocumentRegistry: what's expected of SourceFile","user":{"login":"mihailik","id":4041967,"avatar_url":"https://avatars.githubusercontent.com/u/4041967?v=3","gravatar_id":"","url":"https://api.github.com/users/mihailik","html_url":"https://github.com/mihailik","followers_url":"https://api.github.com/users/mihailik/followers","following_url":"https://api.github.com/users/mihailik/following{/other_user}","gists_url":"https://api.github.com/users/mihailik/gists{/gist_id}","starred_url":"https://api.github.com/users/mihailik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mihailik/subscriptions","organizations_url":"https://api.github.com/users/mihailik/orgs","repos_url":"https://api.github.com/users/mihailik/repos","events_url":"https://api.github.com/users/mihailik/events{/privacy}","received_events_url":"https://api.github.com/users/mihailik/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Question","name":"Question","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-28T22:01:48Z","updated_at":"2015-06-29T21:56:09Z","closed_at":null,"body":"I want to speed up the startup of language service. The biggest hit by far is parsing of `lib.d.ts`, which may easily take many seconds on underpowered devices.\r\n\r\nWiki page [Using the Language Service API](https://github.com/Microsoft/TypeScript/wiki/Using-the-Language-Service-API#document-registry) suggests I can implement a custom document service, which would cache `SourceFile`s.\r\n\r\nSounds good: `lib.d.ts` virtually never changes and can be cached safely. Hopefully, eliminating parsing and boosting startup speeds.\r\n\r\nBut what is the nature of the data that should be cached? I can see [SourceFile interface](https://github.com/Microsoft/TypeScript/blob/01e1b14457bfd7952fac393783225323f6b43187/src/services/services.ts#L61) is not quite clear on what it is holding. Note the `/* @internal */` members, are those expected to be cached/restored too?\r\n\r\n``` typescript\r\n    export interface SourceFile {\r\n        /* @internal */ version: string;\r\n        /* @internal */ scriptSnapshot: IScriptSnapshot;\r\n        /* @internal */ nameTable: Map<string>;\r\n\r\n        /* @internal */ getNamedDeclarations(): Map<Declaration[]>;\r\n\r\n        getLineAndCharacterOfPosition(pos: number): LineAndCharacter;\r\n        getLineStarts(): number[];\r\n        getPositionOfLineAndCharacter(line: number, character: number): number;\r\n        update(newText: string, textChangeRange: TextChangeRange): SourceFile;\r\n    }\r\n```\r\n\r\nComments on [DocumentService.acquireDocument](https://github.com/Microsoft/TypeScript/blob/01e1b14457bfd7952fac393783225323f6b43187/src/services/services.ts#L1373) give a slight hint that the main piece of information `SourceFile` holds is `IScriptSnapshot`. Is that right? Or is it more to it?\r\n\r\nMany thanks!"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/116858898","html_url":"https://github.com/Microsoft/TypeScript/issues/3662#issuecomment-116858898","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/3662","id":116858898,"user":{"login":"mhegazy","id":8000722,"avatar_url":"https://avatars.githubusercontent.com/u/8000722?v=3","gravatar_id":"","url":"https://api.github.com/users/mhegazy","html_url":"https://github.com/mhegazy","followers_url":"https://api.github.com/users/mhegazy/followers","following_url":"https://api.github.com/users/mhegazy/following{/other_user}","gists_url":"https://api.github.com/users/mhegazy/gists{/gist_id}","starred_url":"https://api.github.com/users/mhegazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhegazy/subscriptions","organizations_url":"https://api.github.com/users/mhegazy/orgs","repos_url":"https://api.github.com/users/mhegazy/repos","events_url":"https://api.github.com/users/mhegazy/events{/privacy}","received_events_url":"https://api.github.com/users/mhegazy/received_events","type":"User","site_admin":false},"created_at":"2015-06-29T21:56:09Z","updated_at":"2015-06-29T21:56:09Z","body":"then that is an uncharted territory:) the interface you want to look at is the one types.ts, this is the actual object that the compiler creates. the LS code augments that with some additional helpers by setting the prototype at: https://github.com/Microsoft/TypeScript/blob/master/src/services/services.ts#L7348\r\n\r\nI think it should just work if you do not set the parent pointers when you parse the file (i.e. ts.createSoruceFile). you will need to filter out diagnotics, as they can have circular references, but it might be safe to say do not serialize if there are any parse errors. I do not think you need to care about the table either. \r\n\r\nlet us know how this goes, and the perf characteristics you get at the end. this sounds like an interesting investigation."}}