diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2010-08-07 16:41:53 -0400 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2010-08-07 16:41:53 -0400 |
commit | a6a92b9b3d2bfd9a56e1e5e9cfba706d1aeeaae5 (patch) | |
tree | f1f3da9887dc2dc557df3282b0bcbd4d701ec593 /modules/mmap | |
parent | e7930c85290f77815cdb00a60604feedfa2d0194 (diff) |
Split all namespaces into sub-modules.
* Examples and tests have not been copied over.
* Clojure test/compile phases are commented out in parent POM.
* May require installing parent POM before full build.
Diffstat (limited to 'modules/mmap')
-rw-r--r-- | modules/mmap/pom.xml | 16 | ||||
-rw-r--r-- | modules/mmap/src/main/clojure/clojure/contrib/mmap.clj | 90 |
2 files changed, 106 insertions, 0 deletions
diff --git a/modules/mmap/pom.xml b/modules/mmap/pom.xml new file mode 100644 index 00000000..f64a04cd --- /dev/null +++ b/modules/mmap/pom.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.clojure.contrib</groupId> + <artifactId>parent</artifactId> + <version>1.3.0-SNAPSHOT</version> + <relativePath>../parent</relativePath> + </parent> + <artifactId>mmap</artifactId> + <dependencies> + </dependencies> +</project>
\ No newline at end of file diff --git a/modules/mmap/src/main/clojure/clojure/contrib/mmap.clj b/modules/mmap/src/main/clojure/clojure/contrib/mmap.clj new file mode 100644 index 00000000..0adbf38f --- /dev/null +++ b/modules/mmap/src/main/clojure/clojure/contrib/mmap.clj @@ -0,0 +1,90 @@ +; Copyright (c) Chris Houser, April 2008. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +; Functions for memory-mapping files, plus some functions that use a +; mmaped file for "normal" activies -- slurp, load-file, etc. + +(ns + ^{:author "Chris Houser", + :doc "Functions for memory-mapping files, plus some functions that use a +mmaped file for \"normal\" activies -- slurp, load-file, etc."} + clojure.contrib.mmap + (:refer-clojure :exclude (slurp load-file)) + (:import (java.nio ByteBuffer CharBuffer) + (java.io PushbackReader InputStream InputStreamReader + FileInputStream))) + +;(set! *warn-on-reflection* true) + +(def READ_ONLY ^{:private true} + (java.nio.channels.FileChannel$MapMode/READ_ONLY)) + +(defn mmap + "Memory-map the file named f. Returns a ByteBuffer." + [f] + (let [channel (.getChannel (FileInputStream. f))] + (.map channel READ_ONLY 0 (.size channel)))) + +(defn slurp + "Reads the file named by f and returns it as a string." + [^String f] + (.. java.nio.charset.Charset (forName "UTF-8") + (newDecoder) (decode (mmap f)))) + +(defn buffer-stream + "Returns an InputStream for a ByteBuffer, such as returned by mmap." + [^ByteBuffer buf] + (proxy [InputStream] [] + (available [] (.remaining buf)) + (read + ([] (if (.hasRemaining buf) (.get buf) -1)) + ([dst offset len] (let [actlen (min (.remaining buf) len)] + (.get buf dst offset actlen) + (if (< actlen 1) -1 actlen)))))) + +(defn load-file [f] + "Like clojure.lang/load-file, but uses mmap internally." + (with-open [rdr (-> f mmap buffer-stream InputStreamReader. PushbackReader.)] + (load-reader rdr))) + + +(comment + +(alias 'mmap 'clojure.contrib.mmap) +(alias 'core 'clojure.core) + +;--- +; zip_filter.clj is 95KB +(def tf "/home/chouser/build/clojure/src/clj/clojure/core.clj") +(println "\nload-file" tf) +(time (dotimes [_ 5] (core/load-file tf))) ; 5420.177813 msecs +(time (dotimes [_ 5] (mmap/load-file tf))) ; 7946.854434 msecs -- not so good + +;--- +; kern.log.0 is 961KB +(def tf "/var/log/kern.log.0") +(println "\nslurp" tf) +(time (dotimes [_ 10] (.length (core/slurp tf)))) ; 435.767226 msecs +(time (dotimes [_ 10] (.length (mmap/slurp tf)))) ; 93.176858 msecs + +;--- +; kern.log.0 is 961KB +(def tf "/var/log/kern.log.0") +(println "\nregex slurp large" tf) +(time (dotimes [_ 10] (count (re-seq #"EXT3.*" (core/slurp tf))))) ; 416 +(time (dotimes [_ 10] (count (re-seq #"EXT3.*" (mmap/slurp tf))))) ; 101 + +;--- +; mmap.clj is about 3.1KB +(def tf "/home/chouser/proj/clojure-contrib/src/clojure/contrib/mmap.clj") +(println "\nregex slurp small" tf) + +(time (dotimes [_ 1000] (count (re-seq #"defn \S*" (core/slurp tf))))) ; 308 +(time (dotimes [_ 1000] (count (re-seq #"defn \S*" (mmap/slurp tf))))) ; 198 + +) |